---
source: einstellung_derive/src/test.rs
expression: formatted
---
/// --- input ---
#[derive(Config)]
struct AppConfig {
app_name: String,
#[config(subconfig)]
database: DatabaseConfig,
#[config(subconfig)]
redis: RedisConfig,
}
/// --- output ---
#[derive(::core::default::Default, ::einstellung::serde::Deserialize)]
#[serde(crate = "::einstellung::serde")]
struct AppConfigPartial {
app_name: ::core::option::Option<String>,
database: ::core::option::Option<<DatabaseConfig as ::einstellung::Config>::Partial>,
redis: ::core::option::Option<<RedisConfig as ::einstellung::Config>::Partial>,
}
#[automatically_derived]
impl ::einstellung::PartialConfig for AppConfigPartial {
type Complete = AppConfig;
fn merge(
self,
next: Self,
) -> ::core::result::Result<Self, ::einstellung::ConfigError> {
::core::result::Result::Ok(Self {
app_name: next.app_name.or(self.app_name),
database: match (self.database, next.database) {
(Some(a), Some(b)) => Some(::einstellung::PartialConfig::merge(a, b)?),
(a, b) => a.or(b),
},
redis: match (self.redis, next.redis) {
(Some(a), Some(b)) => Some(::einstellung::PartialConfig::merge(a, b)?),
(a, b) => a.or(b),
},
})
}
fn build(
self,
) -> ::core::result::Result<Self::Complete, ::einstellung::ConfigError> {
::core::result::Result::Ok(AppConfig {
app_name: self
.app_name
.ok_or(
::einstellung::ConfigError::MissingField(
::einstellung::FieldPath::new("AppConfig", "app_name"),
),
)?,
database: self
.database
.map(|x| ::einstellung::build_with_context(x, "AppConfig", "database"))
.transpose()?
.ok_or(
::einstellung::ConfigError::MissingField(
::einstellung::FieldPath::new("AppConfig", "database"),
),
)?,
redis: self
.redis
.map(|x| ::einstellung::build_with_context(x, "AppConfig", "redis"))
.transpose()?
.ok_or(
::einstellung::ConfigError::MissingField(
::einstellung::FieldPath::new("AppConfig", "redis"),
),
)?,
})
}
}
#[automatically_derived]
impl ::einstellung::Config for AppConfig {
type Partial = AppConfigPartial;
}
// ---------------------------------
/// --- input ---
#[derive(Config)]
struct DatabaseConfig {
url: String,
pool_size: u32,
}
/// --- output ---
#[derive(::core::default::Default, ::einstellung::serde::Deserialize)]
#[serde(crate = "::einstellung::serde")]
struct DatabaseConfigPartial {
url: ::core::option::Option<String>,
pool_size: ::core::option::Option<u32>,
}
#[automatically_derived]
impl ::einstellung::PartialConfig for DatabaseConfigPartial {
type Complete = DatabaseConfig;
fn merge(
self,
next: Self,
) -> ::core::result::Result<Self, ::einstellung::ConfigError> {
::core::result::Result::Ok(Self {
url: next.url.or(self.url),
pool_size: next.pool_size.or(self.pool_size),
})
}
fn build(
self,
) -> ::core::result::Result<Self::Complete, ::einstellung::ConfigError> {
::core::result::Result::Ok(DatabaseConfig {
url: self
.url
.ok_or(
::einstellung::ConfigError::MissingField(
::einstellung::FieldPath::new("DatabaseConfig", "url"),
),
)?,
pool_size: self
.pool_size
.ok_or(
::einstellung::ConfigError::MissingField(
::einstellung::FieldPath::new("DatabaseConfig", "pool_size"),
),
)?,
})
}
}
#[automatically_derived]
impl ::einstellung::Config for DatabaseConfig {
type Partial = DatabaseConfigPartial;
}
// ---------------------------------
/// --- input ---
#[derive(Config)]
struct RedisConfig {
cluster_mode: bool,
}
/// --- output ---
#[derive(::core::default::Default, ::einstellung::serde::Deserialize)]
#[serde(crate = "::einstellung::serde")]
struct RedisConfigPartial {
cluster_mode: ::core::option::Option<bool>,
}
#[automatically_derived]
impl ::einstellung::PartialConfig for RedisConfigPartial {
type Complete = RedisConfig;
fn merge(
self,
next: Self,
) -> ::core::result::Result<Self, ::einstellung::ConfigError> {
::core::result::Result::Ok(Self {
cluster_mode: next.cluster_mode.or(self.cluster_mode),
})
}
fn build(
self,
) -> ::core::result::Result<Self::Complete, ::einstellung::ConfigError> {
::core::result::Result::Ok(RedisConfig {
cluster_mode: self
.cluster_mode
.ok_or(
::einstellung::ConfigError::MissingField(
::einstellung::FieldPath::new("RedisConfig", "cluster_mode"),
),
)?,
})
}
}
#[automatically_derived]
impl ::einstellung::Config for RedisConfig {
type Partial = RedisConfigPartial;
}