bingus 0.10.0

databending made easy
Documentation
use std::borrow::Cow;

use derive_new::new;
pub use shiva::core::DocumentType;
use shiva::core::{bytes::Bytes, Document, Element};

use crate::{Bendable, IntoDataBytes, TryFromDataBytes};

#[derive(new)]
pub struct ShivaDocument {
    document: Document,
    output_format: DocumentType,
}

#[derive(new)]
pub struct ShivaFormat {
    input_format: DocumentType,
    output_format: DocumentType,
}

impl TryFromDataBytes for ShivaDocument {
    type Error = anyhow::Error;
    type Format = ShivaFormat;

    fn try_from_data_bytes(
        bytes: crate::Bytes,
        format: Self::Format,
        _crop: crate::Crop,
    ) -> Result<Self, Self::Error> {
        Ok(ShivaDocument::new(
            Document::parse(&Bytes::from(bytes), format.input_format)?,
            format.output_format,
        ))
    }
}

impl IntoDataBytes for ShivaDocument {
    fn into_data_bytes(self) -> crate::Bytes {
        self.document
            .generate(self.output_format)
            .expect("can't crash here! so close!")
            .to_vec()
    }
}

impl Bendable for ShivaDocument {
    type Unit = Element;

    fn map<F: Fn(Cow<Self::Unit>) -> Self::Unit + Sync>(self, f: F) -> Self {
        ShivaDocument::new(
            Document::new(
                self.document
                    .get_all_elements()
                    .into_iter()
                    .map(Cow::Borrowed)
                    .map(f)
                    .collect(),
            ),
            self.output_format,
        )
    }
}