pub enum Format {
Toml,
Json,
Unknown,
}Expand description
Supported configuration file formats.
Each variant corresponds to a specific file format and provides parsing capabilities.
Variants§
Toml
TOML format (requires toml feature).
Json
JSON format (requires json feature).
Unknown
Unknown or unsupported format.
Implementations§
Source§impl Format
impl Format
Sourcepub const fn alternate_extensions(&self) -> &'static [&'static str]
pub const fn alternate_extensions(&self) -> &'static [&'static str]
Get additional file extensions for this format.
Sourcepub fn from_path(path: &Path) -> Option<Self>
pub fn from_path(path: &Path) -> Option<Self>
Detect format from a file path.
Examines the file extension to determine the format.
§Example
use std::path::Path;
use cfgmatic_source::domain::Format;
let format = Format::from_path(Path::new("config.toml"));
assert_eq!(format, Some(Format::Toml));Sourcepub fn from_extension(ext: &str) -> Option<Self>
pub fn from_extension(ext: &str) -> Option<Self>
Detect format from a file extension.
§Example
use cfgmatic_source::domain::Format;
let format = Format::from_extension("toml");
assert_eq!(format, Some(Format::Toml));Sourcepub fn from_content(content: &str) -> Option<Self>
pub fn from_content(content: &str) -> Option<Self>
Detect format from content by examining the beginning.
This is a heuristic and may not always be accurate.
§Example
use cfgmatic_source::domain::Format;
let content = r#"{ "server": { "host": "localhost" } }"#;
let format = Format::from_content(content);
assert_eq!(format, Some(Format::Json));Sourcepub fn parse(&self, content: &str) -> Result<ParsedContent>
pub fn parse(&self, content: &str) -> Result<ParsedContent>
Parse content string into a generic value.
§Errors
Returns a SourceError::ParseFailed if parsing fails.
§Example
use cfgmatic_source::domain::Format;
let content = r#"host = "localhost""#;
let parsed = Format::Toml.parse(content).unwrap();
assert_eq!(parsed.get("host").and_then(|v| v.as_str()), Some("localhost"));Sourcepub fn parse_as<T: DeserializeOwned>(&self, content: &str) -> Result<T>
pub fn parse_as<T: DeserializeOwned>(&self, content: &str) -> Result<T>
Parse content into a specific type.
§Errors
Returns a SourceError::ParseFailed if parsing or deserialization fails.
§Example
use cfgmatic_source::domain::Format;
use serde::Deserialize;
#[derive(Deserialize)]
struct Config {
host: String,
}
let content = r#"host = "localhost""#;
let config: Config = Format::Toml.parse_as(content).unwrap();
assert_eq!(config.host, "localhost");Trait Implementations§
Source§impl<'de> Deserialize<'de> for Format
impl<'de> Deserialize<'de> for Format
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
impl Copy for Format
impl Eq for Format
impl StructuralPartialEq for Format
Auto Trait Implementations§
impl Freeze for Format
impl RefUnwindSafe for Format
impl Send for Format
impl Sync for Format
impl Unpin for Format
impl UnsafeUnpin for Format
impl UnwindSafe for Format
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