einstellung_derive 0.1.2

Proc-Macro crate for einstellung
Documentation
---
source: einstellung_derive/src/test.rs
expression: formatted
---
/// --- helper ---
pub mod validators {
    pub fn validate_cert_path(_: &String) -> Result<(), Box<dyn std::error::Error>> {
        Ok(())
    }
    pub fn validate_port(_: &u16) -> Result<(), Box<dyn std::error::Error>> {
        Ok(())
    }
}

// ---------------------------------

/// --- input ---
#[derive(Config)]
struct TlsConfig {
    #[config(validate = validators::validate_cert_path)]
    cert_path: String,
    #[config(validate = validators::validate_port)]
    port: u16,
}

/// --- output ---
#[derive(::core::default::Default, ::einstellung::serde::Deserialize)]
#[serde(crate = "::einstellung::serde")]
struct TlsConfigPartial {
    cert_path: Option<String>,
    port: Option<u16>,
}
impl ::einstellung::PartialConfig for TlsConfigPartial {
    type Complete = TlsConfig;
    fn merge(
        self,
        next: Self,
    ) -> ::core::result::Result<Self, ::einstellung::ConfigError> {
        ::core::result::Result::Ok(Self {
            cert_path: next.cert_path.or(self.cert_path),
            port: next.port.or(self.port),
        })
    }
    fn build(
        self,
    ) -> ::core::result::Result<Self::Complete, ::einstellung::ConfigError> {
        let cert_path: String = self
            .cert_path
            .ok_or(
                ::einstellung::ConfigError::MissingField(
                    ::einstellung::FieldPath::new("TlsConfig", "cert_path"),
                ),
            )?;
        let _: ::einstellung::ValidationFunction<String, _> = validators::validate_cert_path;
        if let Err(e) = (validators::validate_cert_path)(&cert_path) {
            return Err(::einstellung::ConfigError::Validation {
                field: ::einstellung::FieldPath::new("TlsConfig", "cert_path"),
                #[allow(clippy::useless_conversion)]
                reason: e.into(),
            });
        }
        let port: u16 = self
            .port
            .ok_or(
                ::einstellung::ConfigError::MissingField(
                    ::einstellung::FieldPath::new("TlsConfig", "port"),
                ),
            )?;
        let _: ::einstellung::ValidationFunction<u16, _> = validators::validate_port;
        if let Err(e) = (validators::validate_port)(&port) {
            return Err(::einstellung::ConfigError::Validation {
                field: ::einstellung::FieldPath::new("TlsConfig", "port"),
                #[allow(clippy::useless_conversion)]
                reason: e.into(),
            });
        }
        ::core::result::Result::Ok(TlsConfig { cert_path, port })
    }
}
impl ::einstellung::Config for TlsConfig {
    type Partial = TlsConfigPartial;
}