48 lines
1.7 KiB
Rust
48 lines
1.7 KiB
Rust
//! Typed readers for object members: lookup + type check, with the full path in errors.
|
|
|
|
use super::field::{optional, required};
|
|
use super::path::join;
|
|
use super::scalar::{flag, items, number, text, whole_u64, whole_u8};
|
|
use super::Value;
|
|
use crate::fault::Fault;
|
|
|
|
pub fn number_member(object: &Value, path: &str, key: &str) -> Result<f64, Fault> {
|
|
number(required(object, path, key)?, &join(path, key))
|
|
}
|
|
|
|
pub fn text_member<'a>(object: &'a Value, path: &str, key: &str) -> Result<&'a str, Fault> {
|
|
text(required(object, path, key)?, &join(path, key))
|
|
}
|
|
|
|
pub fn flag_member(object: &Value, path: &str, key: &str) -> Result<bool, Fault> {
|
|
flag(required(object, path, key)?, &join(path, key))
|
|
}
|
|
|
|
pub fn items_member<'a>(object: &'a Value, path: &str, key: &str) -> Result<&'a [Value], Fault> {
|
|
items(required(object, path, key)?, &join(path, key))
|
|
}
|
|
|
|
pub fn u8_member(object: &Value, path: &str, key: &str) -> Result<u8, Fault> {
|
|
whole_u8(required(object, path, key)?, &join(path, key))
|
|
}
|
|
|
|
pub fn opt_number_member(object: &Value, path: &str, key: &str) -> Result<Option<f64>, Fault> {
|
|
optional(object, path, key)?.map(|v| number(v, &join(path, key))).transpose()
|
|
}
|
|
|
|
pub fn opt_u8_member(object: &Value, path: &str, key: &str) -> Result<Option<u8>, Fault> {
|
|
optional(object, path, key)?.map(|v| whole_u8(v, &join(path, key))).transpose()
|
|
}
|
|
|
|
pub fn opt_u64_member(object: &Value, path: &str, key: &str) -> Result<Option<u64>, Fault> {
|
|
optional(object, path, key)?.map(|v| whole_u64(v, &join(path, key))).transpose()
|
|
}
|
|
|
|
pub fn opt_items_member<'a>(
|
|
object: &'a Value,
|
|
path: &str,
|
|
key: &str,
|
|
) -> Result<Option<&'a [Value]>, Fault> {
|
|
optional(object, path, key)?.map(|v| items(v, &join(path, key))).transpose()
|
|
}
|