Trait ConfigPort

Source
pub trait ConfigPort: Send + Sync {
    // Required methods
    fn load(&self, source: &str) -> Result<RawConfig>;
    fn format_name(&self) -> &'static str;

    // Provided methods
    fn load_from_file(&self, path: impl AsRef<Path>) -> Result<RawConfig> { ... }
    fn file_extension(&self) -> &'static str { ... }
}
Expand description

Port for loading configuration from external sources.

ConfigPort is the primary inbound port for configuration loading. It abstracts over different configuration formats (TOML, JSON, etc.) and sources (files, environment, in-memory).

§Implementing ConfigPort

Adapters implement this trait to add support for new configuration formats:

ⓘ
use fernctl::ports::inbound::{ConfigPort, RawConfig, SourceFormat};
use fernctl::error::Result;

struct TomlConfigAdapter;

impl ConfigPort for TomlConfigAdapter {
    fn load(&self, source: &str) -> Result<RawConfig> {
        let value: toml::Value = toml::from_str(source)?;
        let json = serde_json::to_value(value)?;
        Ok(RawConfig::new(json, SourceFormat::Toml))
    }

    fn format_name(&self) -> &'static str {
        "TOML"
    }
}

§Object Safety

This trait is object-safe and can be used as a trait object:

ⓘ
let adapter: Box<dyn ConfigPort> = Box::new(TomlConfigAdapter::new());
let config = adapter.load(content)?;

§Error Handling

All loading operations return Result<RawConfig>. Errors should be wrapped in ConfigError variants for rich diagnostics.

Required Methods§

Source

fn load(&self, source: &str) -> Result<RawConfig>

Loads configuration from a string source.

The source string should contain the full configuration in the format supported by this adapter (e.g., TOML content for a TOML adapter).

§Arguments
  • source — The configuration content as a string
§Returns

A RawConfig that can be validated into a Theme.

§Errors

Returns ConfigError if the source cannot be parsed.

§Example
ⓘ
let toml_content = r#"
    [appearance]
    variant = "dark"
"#;

let raw = adapter.load(toml_content)?;
Source

fn format_name(&self) -> &'static str

Returns the human-readable name of this configuration format.

Used in error messages and logging.

§Example
ⓘ
println!("Loading {} configuration...", adapter.format_name());
// Prints: "Loading TOML configuration..."

Provided Methods§

Source

fn load_from_file(&self, path: impl AsRef<Path>) -> Result<RawConfig>

Loads configuration from a file path.

This is a convenience method that reads the file and calls [load]. The default implementation handles file I/O and provides appropriate error messages.

§Arguments
  • path — Path to the configuration file
§Returns

A RawConfig that can be validated into a Theme.

§Errors
§Example
ⓘ
let raw = adapter.load_from_file("~/.config/fern/config.toml")?;
Source

fn file_extension(&self) -> &'static str

Returns the typical file extension for this format.

Used for auto-detecting the appropriate adapter based on file path.

§Example
ⓘ
assert_eq!(toml_adapter.file_extension(), "toml");

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§