pub mod error;
use serde_json::{Map, Value};
use crate::output_parsing::error::WTBlkError;
#[derive(Debug)]
pub struct WTBlk {
inner: Value,
}
impl WTBlk {
pub fn new(str: &str) -> Result<Self, WTBlkError> {
let inner: Value = serde_json::from_str(str)?;
Ok(Self {
inner,
})
}
pub fn pointer<'a>(&'a self, pointer: &'a str) -> Result<&Value, WTBlkError> {
self.inner.pointer(pointer).ok_or_else(||WTBlkError::NoSuchValue(pointer.to_owned()))
}
pub fn inner(&mut self) -> &mut Value {
&mut self.inner
}
pub fn bool<'a>(&'a self, pointer: &'a str) -> Result<bool, WTBlkError> {
let res = self.pointer(pointer)?;
res.as_bool().ok_or_else(||WTBlkError::Parse(pointer.to_owned(), res.to_string()))
}
pub fn float<'a>(&'a self, pointer: &'a str) -> Result<f64, WTBlkError> {
let res = self.pointer(pointer)?;
res.as_f64().ok_or_else(||WTBlkError::Parse(pointer.to_owned(), res.to_string()))
}
pub fn int<'a>(&'a self, pointer: &'a str) -> Result<i64, WTBlkError> {
let res = self.pointer(pointer)?;
res.as_i64().ok_or_else(||WTBlkError::Parse(pointer.to_owned(), res.to_string()))
}
pub fn str<'a>(&'a self, pointer: &'a str) -> Result<&str, WTBlkError> {
let res = self.pointer(pointer)?;
res.as_str().ok_or_else(||WTBlkError::Parse(pointer.to_owned(), res.to_string()))
}
pub fn objects<'a>(&'a self, pointer: &'a str) -> Result<&Map<String, Value>, WTBlkError> {
let res = self.pointer(pointer)?;
res.as_object().ok_or_else(||WTBlkError::Parse(pointer.to_owned(), res.to_string()))
}
pub fn array<'a>(&'a self, pointer: &'a str) -> Result<&Vec<Value>, WTBlkError> {
let res = self.pointer(pointer)?;
res.as_array().ok_or_else(||WTBlkError::Parse(pointer.to_owned(), res.to_string()))
}
}