use garde::Validate;
use crate::types::{ConfigParseError, ShakrsConfig};
impl core::fmt::Display for ConfigParseError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::Schema(detail) => write!(f, "shakrs.json schema error: {detail}"),
Self::Semantic(detail) => write!(f, "shakrs.json semantic error: {detail}"),
}
}
}
impl core::error::Error for ConfigParseError {}
pub fn parse(bytes: &[u8]) -> Result<ShakrsConfig, ConfigParseError> {
#[expect(
clippy::disallowed_methods,
reason = "deserialize-then-garde-validate is the contract the disallowed-method rule enforces; no Validated<T> exists in this workspace and a generic one would be single-use."
)]
let config: ShakrsConfig =
serde_json::from_slice(bytes).map_err(|err| ConfigParseError::Schema(err.to_string()))?;
config
.validate()
.map_err(|err| ConfigParseError::Semantic(err.to_string()))?;
Ok(config)
}