feat(M10.3): validate active profile pointers
Test: cargo test -p fader-config --all-targets --all-features Test: cargo clippy -p fader-config --all-targets --all-features -- -D warnings
This commit is contained in:
parent
cbaf2a9694
commit
1129cc9730
|
|
@ -0,0 +1,51 @@
|
|||
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(),
|
||||
})?)
|
||||
}
|
||||
}
|
||||
|
|
@ -7,6 +7,7 @@
|
|||
|
||||
#![forbid(unsafe_code)]
|
||||
|
||||
mod active;
|
||||
mod checkpoint;
|
||||
mod device;
|
||||
mod document;
|
||||
|
|
@ -31,3 +32,4 @@ pub use state::{
|
|||
};
|
||||
|
||||
pub(crate) const SCHEMA_VERSION: u16 = 1;
|
||||
pub use active::ActiveConfig;
|
||||
|
|
|
|||
|
|
@ -6,9 +6,27 @@ use std::{
|
|||
};
|
||||
|
||||
use fader_config::{
|
||||
AtomicFile, ConfigError, MigrationError, PersistenceError, ProfileDocument, StateLoad,
|
||||
XdgEnvironment, XdgPaths, migrate_profile,
|
||||
ActiveConfig, AtomicFile, ConfigError, MigrationError, PersistenceError, ProfileDocument,
|
||||
StateLoad, XdgEnvironment, XdgPaths, migrate_profile,
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn active_profile_pointer_is_strict_and_round_trips() {
|
||||
let active = ActiveConfig::new("desktop").unwrap();
|
||||
let encoded = active.to_toml().unwrap();
|
||||
assert_eq!(
|
||||
ActiveConfig::parse(&encoded)
|
||||
.unwrap()
|
||||
.active_profile()
|
||||
.as_str(),
|
||||
"desktop"
|
||||
);
|
||||
assert!(ActiveConfig::parse("schema_version = 1\nactive_profile = '../escape'").is_err());
|
||||
assert!(
|
||||
ActiveConfig::parse("schema_version = 1\nactive_profile = 'desktop'\nunknown = true")
|
||||
.is_err()
|
||||
);
|
||||
}
|
||||
use tempfile::tempdir;
|
||||
|
||||
const PROFILE: &str = include_str!("../../examples/profiles/desktop.toml");
|
||||
|
|
|
|||
Loading…
Reference in New Issue