use super::*;
use derive_more::{Deref, DerefMut};
use unstructured::{Unstructured, UnstructuredDataTrait};
pub type InnerData = Unstructured<Data>;
#[derive(Clone, Debug)]
pub enum OtherData {
}
impl fmt::Display for OtherData {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "<OtherData>")
}
}
#[derive(Debug, Clone, Deref, DerefMut)]
pub struct Data {
#[deref]
inner: InnerData,
}
impl fmt::Display for Data {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "<Node>")
}
}
impl UnstructuredDataTrait for Data {
type ErrorType = TemplarError;
type OtherType = OtherData;
}
impl<'a> Data {
#[inline]
pub fn empty() -> Data {
Data {
inner: InnerData::Unassigned,
}
}
pub fn new<T: Into<InnerData>>(inner: T) -> Self {
Self {
inner: inner.into(),
}
}
pub fn inner_data(&self) -> &InnerData {
&self.inner
}
pub fn inner_data_mut(&mut self) -> &mut InnerData {
&mut self.inner
}
pub fn into_inner(self) -> InnerData {
self.inner
}
pub fn render(self) -> Result<String> {
match self.inner {
InnerData::Err(e) => Err(e),
InnerData::Null => Ok("null".into()),
InnerData::Unassigned => Ok("".into()),
doc => Ok(doc.to_string()),
}
}
#[inline]
pub fn is_failed(&self) -> bool {
matches!(self.inner, InnerData::Err(_))
}
#[inline]
pub fn is_empty(&self) -> bool {
matches!(self.inner, InnerData::Unassigned)
}
#[inline]
pub fn unwrap(self) -> Self {
self
}
#[inline]
pub fn unwrap_err(self) -> TemplarError {
match self.inner {
InnerData::Err(e) => e,
_ => panic!("Not an error!"),
}
}
pub fn into_result(self) -> Result<Self> {
match self.inner {
InnerData::Err(e) => Err(e),
dat => Ok(Self { inner: dat }),
}
}
pub fn clone_result(&self) -> Result<Self> {
self.clone().into_result()
}
pub fn to_result(&'a self) -> Result<&'a InnerData> {
match &self.inner {
InnerData::Err(e) => Err(e.clone()),
ref dat => Ok(dat),
}
}
pub fn from_result(result: Result<InnerData>) -> Data {
match result {
Ok(inner) => Data { inner },
Err(e) => Data {
inner: InnerData::Err(e),
},
}
}
pub(crate) fn check<T: std::fmt::Debug>(to_check: Result<T>) -> Data {
match to_check {
Err(e) => Data::new(InnerData::Err(e)),
_ => Data::empty(),
}
}
pub(crate) fn from_vec(seq: Vec<Data>) -> Self {
let result: Result<Vec<InnerData>> = seq.into_iter().map(|d| Ok(d.inner)).collect();
match result {
Ok(docs) => Data::new(docs),
Err(e) => e.into(),
}
}
}
impl<T: Into<InnerData>> From<T> for Data {
#[inline]
fn from(doc: T) -> Self {
Data { inner: doc.into() }
}
}
impl From<TemplarError> for Data {
#[inline]
fn from(error: TemplarError) -> Self {
Data {
inner: InnerData::Err(error),
}
}
}