fader_master/config/src/device.rs

64 lines
1.9 KiB
Rust

use fader_core::SurfaceId;
use serde::{Deserialize, Serialize};
use crate::{ConfigError, SCHEMA_VERSION, profile::parse_id};
/// Output and motion features explicitly supported by a device profile.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct DeviceCapabilities {
/// A mute indicator can be driven.
pub mute_led: bool,
/// A numeric or bar-style value display can be driven.
pub value_display: bool,
/// A motorized fader can accept position feedback.
pub motorized_fader: bool,
}
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
struct RawDeviceProfile {
schema_version: u16,
id: String,
label: String,
capabilities: DeviceCapabilities,
}
/// A reusable, validated MIDI device capability document.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct DeviceProfile {
raw: RawDeviceProfile,
id: SurfaceId,
}
impl DeviceProfile {
/// Parses and validates a device profile.
pub fn parse(input: &str) -> Result<Self, ConfigError> {
let raw: RawDeviceProfile = 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
),
));
}
let id = parse_id(&raw.id, "device.id")?;
if raw.label.trim().is_empty() {
return Err(ConfigError::validation("device.label", "must not be empty"));
}
Ok(Self { raw, id })
}
/// Serializes a canonical representation.
pub fn to_toml(&self) -> Result<String, ConfigError> {
Ok(toml::to_string_pretty(&self.raw)?)
}
/// Stable device-profile identifier.
pub fn id(&self) -> &SurfaceId {
&self.id
}
}