13 lines
458 B
Rust
13 lines
458 B
Rust
use crate::fault::Fault;
|
|
use crate::json::member::text_member;
|
|
use crate::json::Value;
|
|
|
|
/// Reads a string member and maps it through a closed vocabulary (`Variant::parse`, …).
|
|
pub fn named<T>(root: &Value, key: &str, parse: fn(&str) -> Option<T>) -> Result<T, Fault> {
|
|
let given = text_member(root, "", key)?;
|
|
parse(given).ok_or_else(|| Fault::Invalid {
|
|
field: key.to_string(),
|
|
detail: format!("unknown value {given:?}"),
|
|
})
|
|
}
|