---
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: Option<String>,
database: Option<<DatabaseConfig as ::einstellung::Config>::Partial>,
redis: Option<<RedisConfig as ::einstellung::Config>::Partial>,
}
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> {
let app_name = self
.app_name
.ok_or(
::einstellung::ConfigError::MissingField(
::einstellung::FieldPath::new("AppConfig", "app_name"),
),
)?;
let database = ::einstellung::build_with_context(
self.database.unwrap_or_default(),
"AppConfig",
"database",
)?;
let redis = ::einstellung::build_with_context(
self.redis.unwrap_or_default(),
"AppConfig",
"redis",
)?;
::core::result::Result::Ok(AppConfig {
app_name,
database,
redis,
})
}
}
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: Option<String>,
pool_size: Option<u32>,
}
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> {
let url = self
.url
.ok_or(
::einstellung::ConfigError::MissingField(
::einstellung::FieldPath::new("DatabaseConfig", "url"),
),
)?;
let pool_size = self
.pool_size
.ok_or(
::einstellung::ConfigError::MissingField(
::einstellung::FieldPath::new("DatabaseConfig", "pool_size"),
),
)?;
::core::result::Result::Ok(DatabaseConfig { url, pool_size })
}
}
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: Option<bool>,
}
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> {
let cluster_mode = self
.cluster_mode
.ok_or(
::einstellung::ConfigError::MissingField(
::einstellung::FieldPath::new("RedisConfig", "cluster_mode"),
),
)?;
::core::result::Result::Ok(RedisConfig { cluster_mode })
}
}
impl ::einstellung::Config for RedisConfig {
type Partial = RedisConfigPartial;
}