use serde::de::DeserializeOwned;
use serde_json::Value;
pub(crate) fn object_into<T: DeserializeOwned>(value: Value) -> Result<T, String> {
if !value.is_object() {
return Err(format!("expected an object, got {}", kind(&value)));
}
serde_json::from_value(value).map_err(|e| e.to_string())
}
fn kind(value: &Value) -> &'static str {
match value {
Value::Null => "null",
Value::Bool(_) => "a boolean",
Value::Number(_) => "a number",
Value::String(_) => "a string",
Value::Array(_) => "an array",
Value::Object(_) => "an object",
}
}
#[cfg(test)]
mod tests {
use serde_json::json;
use super::object_into;
use crate::protocol::{DeRecProtocolConfig, TimeoutsConfig};
#[test]
fn a_valid_config_object_is_read() {
let config: DeRecProtocolConfig = object_into(json!({
"threshold": 3,
"timeouts": { "sharing_round_secs": 30 },
"replica_id": "7"
}))
.expect("valid config");
assert_eq!(config.threshold, Some(3));
assert_eq!(config.replica_id, Some(7));
assert_eq!(config.timeouts.and_then(|t| t.sharing_round_secs), Some(30));
}
#[test]
fn a_misspelled_top_level_key_is_refused() {
let err = object_into::<DeRecProtocolConfig>(json!({ "treshold": 3 }))
.expect_err("misspelled key");
assert!(err.contains("unknown field `treshold`"), "{err}");
}
#[test]
fn a_misspelled_nested_key_is_refused() {
let err = object_into::<DeRecProtocolConfig>(json!({
"timeouts": { "sharing_round": 30 }
}))
.expect_err("misspelled nested key");
assert!(err.contains("unknown field `sharing_round`"), "{err}");
let err = object_into::<TimeoutsConfig>(json!({ "sharing_round": 30 }))
.expect_err("misspelled timeouts key");
assert!(err.contains("unknown field `sharing_round`"), "{err}");
}
#[test]
fn anything_but_an_object_is_refused() {
for value in [json!([3]), json!(null), json!("threshold"), json!(3)] {
let err = object_into::<DeRecProtocolConfig>(value.clone()).expect_err("not an object");
assert!(err.starts_with("expected an object"), "{value}: {err}");
}
}
}