---
source: einstellung_derive/src/test.rs
expression: formatted
---
/// --- helper ---
pub fn validate_port(_: u16) -> Result<(), Box<dyn std::error::Error>> {
Ok(())
}
// ---------------------------------
/// --- input ---
#[derive(Config)]
struct ServerConfig {
#[config(validate = validate_port)]
port: u16,
}
/// --- output ---
#[derive(::core::default::Default, ::einstellung::serde::Deserialize)]
#[serde(crate = "::einstellung::serde")]
struct ServerConfigPartial {
port: ::core::option::Option<u16>,
}
#[automatically_derived]
impl ::einstellung::PartialConfig for ServerConfigPartial {
type Complete = ServerConfig;
fn merge(
self,
next: Self,
) -> ::core::result::Result<Self, ::einstellung::ConfigError> {
::core::result::Result::Ok(Self {
port: next.port.or(self.port),
})
}
fn build(
self,
) -> ::core::result::Result<Self::Complete, ::einstellung::ConfigError> {
::core::result::Result::Ok(ServerConfig {
port: {
let port: u16 = self
.port
.ok_or(
::einstellung::ConfigError::MissingField(
::einstellung::FieldPath::new("ServerConfig", "port"),
),
)?;
let _: ::einstellung::ValidationFunction<u16, _> = validate_port;
if let Err(e) = (validate_port)(&port) {
return Err(::einstellung::ConfigError::Validation {
field: ::einstellung::FieldPath::new("ServerConfig", "port"),
#[allow(clippy::useless_conversion)]
reason: e.into(),
});
}
port
},
})
}
}
#[automatically_derived]
impl ::einstellung::Config for ServerConfig {
type Partial = ServerConfigPartial;
}