use json::{ JsonValue, iterators::Members };
pub trait JsonValueExt {
fn checked_string(&self) -> String;
fn checked_bytes(&self) -> Vec<u8>;
fn checked_array_iter(&self) -> Members;
fn optional_usize(&self, def: usize) -> usize;
fn optional_string(&self, def: impl ToString) -> String;
}
impl JsonValueExt for JsonValue {
fn checked_string(&self) -> String {
self.as_str().unwrap().to_string()
}
fn checked_bytes(&self) -> Vec<u8> {
let encoded = self.as_str().unwrap();
hex::decode(encoded).unwrap()
}
fn checked_array_iter(&self) -> Members {
assert!(self.is_array());
self.members()
}
fn optional_usize(&self, def: usize) -> usize {
match self.is_number() {
true => self.as_usize().unwrap(),
false => def
}
}
fn optional_string(&self, def: impl ToString) -> String {
match self.is_string() {
true => self.as_str().unwrap().to_string(),
false => def.to_string()
}
}
}
pub trait ResultExt<T, E> {
fn error_or(self, msg: impl ToString) -> E;
}
impl<T, E> ResultExt<T, E> for Result<T, E> {
fn error_or(self, m: impl ToString) -> E {
match self {
Err(e) => e,
_ => panic!("{}", m.to_string())
}
}
}