cfgmatic-source 5.0.1

Configuration sources (file, env, memory) for cfgmatic framework
Documentation
//! Builder for [`Loader`](super::Loader).

use crate::config::{ErrorMode, LoadOptions, MergeStrategy};
use crate::domain::Format;

use super::Loader;

/// Builder for [`Loader`].
#[derive(Debug, Clone, Default)]
pub struct LoaderBuilder {
    options: Option<LoadOptions>,
    default_format: Option<Format>,
}

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

    /// Set the load options.
    #[must_use]
    pub fn options(mut self, options: LoadOptions) -> Self {
        self.options = Some(options);
        self
    }

    /// Set the merge strategy.
    #[must_use]
    pub fn merge_strategy(mut self, strategy: MergeStrategy) -> Self {
        let mut options = self.options.unwrap_or_default();
        options.merge_strategy = strategy;
        self.options = Some(options);
        self
    }

    /// Set whether to fail fast on errors.
    #[must_use]
    pub fn fail_fast(mut self, fail_fast: bool) -> Self {
        let mut options = self.options.unwrap_or_default();
        options.error_mode = ErrorMode::from_fail_fast(fail_fast);
        self.options = Some(options);
        self
    }

    /// Set the default format when detection fails.
    #[must_use]
    pub const fn default_format(mut self, format: Format) -> Self {
        self.default_format = Some(format);
        self
    }

    /// Build the Loader.
    #[must_use]
    pub fn build(self) -> Loader {
        Loader {
            options: self.options.unwrap_or_default(),
            default_format: self.default_format,
        }
    }
}