52 lines
1.5 KiB
Rust
52 lines
1.5 KiB
Rust
use fader_core::ProfileId;
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use crate::{ConfigError, SCHEMA_VERSION};
|
|
|
|
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
|
|
#[serde(deny_unknown_fields)]
|
|
struct RawActiveConfig {
|
|
schema_version: u16,
|
|
active_profile: String,
|
|
}
|
|
|
|
/// Durable pointer to the profile selected for the next server start.
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
|
pub struct ActiveConfig {
|
|
active_profile: ProfileId,
|
|
}
|
|
|
|
impl ActiveConfig {
|
|
pub fn new(active_profile: &str) -> Result<Self, ConfigError> {
|
|
let active_profile = ProfileId::new(active_profile)
|
|
.map_err(|error| ConfigError::validation("active_profile", error.to_string()))?;
|
|
Ok(Self { active_profile })
|
|
}
|
|
|
|
pub fn parse(input: &str) -> Result<Self, ConfigError> {
|
|
let raw: RawActiveConfig = toml::from_str(input)?;
|
|
if raw.schema_version != SCHEMA_VERSION {
|
|
return Err(ConfigError::validation(
|
|
"schema_version",
|
|
format!(
|
|
"unsupported version {}; expected {SCHEMA_VERSION}",
|
|
raw.schema_version
|
|
),
|
|
));
|
|
}
|
|
Self::new(&raw.active_profile)
|
|
}
|
|
|
|
#[must_use]
|
|
pub fn active_profile(&self) -> &ProfileId {
|
|
&self.active_profile
|
|
}
|
|
|
|
pub fn to_toml(&self) -> Result<String, ConfigError> {
|
|
Ok(toml::to_string_pretty(&RawActiveConfig {
|
|
schema_version: SCHEMA_VERSION,
|
|
active_profile: self.active_profile.as_str().to_owned(),
|
|
})?)
|
|
}
|
|
}
|