use std::io::{BufWriter, Cursor, Read, Write};
#[cfg(feature="async_tokio")]
pub use async_tokio_public::*;
#[cfg(feature="serde")]
pub use serde_public::*;
use crate::Element;
use crate::error::{TychoResult, TychoStatus};
use crate::read::element::read_element;
use crate::write::element::write_element;
pub fn marshall<W: Write, E: Into<Element>>(writer: &mut W, element: E) -> TychoStatus {
write_element(writer, &element.into())
}
pub fn marshall_vec<E: Into<Element>>(element: E) -> TychoResult<Vec<u8>> {
let mut buffer = BufWriter::new(Vec::new());
marshall(&mut buffer, element)?;
Ok(buffer.into_inner().unwrap()) }
pub fn unmarshall<R: Read>(reader: &mut R) -> TychoResult<Element> {
read_element(reader)
}
pub fn unmarshall_vec(data: Vec<u8>) -> TychoResult<Element> {
let mut buffer = Cursor::new(data);
unmarshall(&mut buffer)
}
#[cfg(feature="async_tokio")]
mod async_tokio_public {
use tokio::io::AsyncRead;
use crate::Element;
use crate::error::TychoResult;
use crate::read::async_::element::read_element_async;
pub async fn unmarshall_async<R: AsyncRead + Unpin + Send>(reader: &mut R) -> TychoResult<Element> {
read_element_async(reader).await
}
}
#[cfg(feature="serde")]
mod serde_public {
use serde::de::DeserializeOwned;
use serde::Serialize;
use crate::{Element, marshall_vec, unmarshall_vec};
use crate::error::TychoResult;
use crate::serde::de::TychoDeserializer;
use crate::serde::ser::TychoSerializer;
pub fn to_element<S: Serialize>(o: S) -> TychoResult<Element> {
o.serialize(TychoSerializer)
}
pub fn to_bytes<S: Serialize>(o: S) -> TychoResult<Vec<u8>> {
marshall_vec(to_element(o)?)
}
pub fn from_element<D: DeserializeOwned, E: Into<Element>>(e: E) -> TychoResult<D> {
D::deserialize(TychoDeserializer::new(e.into()))
}
pub fn from_bytes<D: DeserializeOwned>(b: &[u8]) -> TychoResult<D> {
from_element(unmarshall_vec(b.to_vec())?)
}
}
pub use crate::into::ident::Ident;
pub use crate::into::value::ValueType;
#[cfg(feature="compression")]
pub use crate::into::compression;