pub struct ConfigLoader;Expand description
Configuration loader with format detection and parsing.
Implementations§
Source§impl ConfigLoader
impl ConfigLoader
Sourcepub fn load<T: DeserializeOwned>(path: impl AsRef<Path>) -> Result<T>
pub fn load<T: DeserializeOwned>(path: impl AsRef<Path>) -> Result<T>
Load configuration from a file with automatic format detection.
Format is determined by file extension:
.yaml,.yml-> YAML.json-> JSON.toml-> TOML
§Errors
Returns error if:
- File cannot be read
- Format cannot be determined from extension
- Content fails to parse
Sourcepub fn load_with_format<T: DeserializeOwned>(
path: impl AsRef<Path>,
format: ConfigFormat,
) -> Result<T>
pub fn load_with_format<T: DeserializeOwned>( path: impl AsRef<Path>, format: ConfigFormat, ) -> Result<T>
Load configuration from a file with explicit format.
Sourcepub fn load_from_reader<T: DeserializeOwned, R: Read>(
reader: &mut R,
format: ConfigFormat,
) -> Result<T>
pub fn load_from_reader<T: DeserializeOwned, R: Read>( reader: &mut R, format: ConfigFormat, ) -> Result<T>
Load configuration from a reader with explicit format.
Sourcepub fn parse<T: DeserializeOwned>(
content: &str,
format: ConfigFormat,
) -> Result<T>
pub fn parse<T: DeserializeOwned>( content: &str, format: ConfigFormat, ) -> Result<T>
Parse configuration from a string with specified format.
Sourcepub fn serialize<T: Serialize>(
config: &T,
format: ConfigFormat,
) -> Result<String>
pub fn serialize<T: Serialize>( config: &T, format: ConfigFormat, ) -> Result<String>
Serialize configuration to a string.
Sourcepub fn save<T: Serialize>(config: &T, path: impl AsRef<Path>) -> Result<()>
pub fn save<T: Serialize>(config: &T, path: impl AsRef<Path>) -> Result<()>
Save configuration to a file with automatic format detection.
Sourcepub fn save_with_format<T: Serialize>(
config: &T,
path: impl AsRef<Path>,
format: ConfigFormat,
) -> Result<()>
pub fn save_with_format<T: Serialize>( config: &T, path: impl AsRef<Path>, format: ConfigFormat, ) -> Result<()>
Save configuration to a file with explicit format.
Sourcepub fn load_first<T: DeserializeOwned>(
paths: &[impl AsRef<Path>],
) -> Result<(T, PathBuf)>
pub fn load_first<T: DeserializeOwned>( paths: &[impl AsRef<Path>], ) -> Result<(T, PathBuf)>
Try to load configuration from multiple paths, returning the first success.
Useful for loading from default locations:
ⓘ
let config: EngineConfig = ConfigLoader::load_first(&[
"config.yaml",
"config.json",
"/etc/trap-sim/config.yaml",
])?;Sourcepub fn is_supported(path: impl AsRef<Path>) -> bool
pub fn is_supported(path: impl AsRef<Path>) -> bool
Check if a file format is supported.
Auto Trait Implementations§
impl Freeze for ConfigLoader
impl RefUnwindSafe for ConfigLoader
impl Send for ConfigLoader
impl Sync for ConfigLoader
impl Unpin for ConfigLoader
impl UnsafeUnpin for ConfigLoader
impl UnwindSafe for ConfigLoader
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more