cfgmatic-source 5.0.1

Configuration sources (file, env, memory) for cfgmatic framework
Documentation
//! Loader service for loading configuration from sources.
//!
//! This module provides the [`Loader`] service which is responsible for
//! loading configuration from various sources and parsing it.
//!
//! # Example
//!
//! ```rust,no_run
//! use cfgmatic_source::application::Loader;
//! use cfgmatic_source::infrastructure::MemorySource;
//! use serde::Deserialize;
//!
//! #[derive(Debug, Deserialize, PartialEq)]
//! struct MyConfig {
//!     name: String,
//! }
//!
//! let loader = Loader::builder().fail_fast(false).build();
//! let source = MemorySource::builder().set("name", "demo").build();
//! let config: MyConfig = loader.load_as(&source)?;
//! assert_eq!(config, MyConfig { name: "demo".into() });
//! # Ok::<(), cfgmatic_source::SourceError>(())
//! ```

mod builder;
mod service;

use crate::config::LoadOptions;
use crate::domain::Format;
pub use builder::LoaderBuilder;

/// Service for loading configuration from sources.
///
/// The Loader is responsible for:
/// - Loading raw content from sources
/// - Detecting and parsing formats
/// - Converting to typed configuration
#[derive(Debug, Clone, Default)]
pub struct Loader {
    /// Loading options.
    pub(super) options: LoadOptions,

    /// Default format to use when detection fails.
    pub(super) default_format: Option<Format>,
}

impl Loader {
    /// Create a new Loader with default options.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Create a builder for constructing a Loader.
    #[must_use]
    pub fn builder() -> LoaderBuilder {
        LoaderBuilder::new()
    }

    /// Get the load options.
    #[must_use]
    pub const fn options(&self) -> &LoadOptions {
        &self.options
    }
}

#[cfg(test)]
mod tests;