use std::fmt::Display;
use rskit_errors::{AppError, AppResult};
pub fn supported_schema<T>(field: &str, configured: Option<T>, supported: T) -> AppResult<T>
where
T: Copy + Eq + Display,
{
let schema = configured.unwrap_or(supported);
if schema != supported {
return Err(AppError::invalid_input(
field,
format!("unsupported schema {schema}; supported schema is {supported}"),
));
}
Ok(schema)
}
#[cfg(test)]
mod tests {
use rskit_errors::ErrorCode;
use super::supported_schema;
#[test]
fn defaults_absent_value() {
assert_eq!(supported_schema("schema", None, 1).unwrap(), 1);
}
#[test]
fn accepts_supported_value() {
assert_eq!(supported_schema("schema", Some(1), 1).unwrap(), 1);
}
#[test]
fn rejects_unsupported_value() {
let error = supported_schema("schema", Some(2), 1).unwrap_err();
assert_eq!(error.code(), ErrorCode::InvalidInput);
assert!(error.message().contains("unsupported schema 2"));
}
}