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§
Sourcefn load(&self, source: &str) -> Result<RawConfig>
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)?;Sourcefn format_name(&self) -> &'static str
fn format_name(&self) -> &'static str
Provided Methods§
Sourcefn load_from_file(&self, path: impl AsRef<Path>) -> Result<RawConfig>
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
FernError::Ioif the file cannot be readConfigErrorif the content cannot be parsed
§Example
let raw = adapter.load_from_file("~/.config/fern/config.toml")?;Sourcefn file_extension(&self) -> &'static str
fn file_extension(&self) -> &'static str
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.