extern crate alloc;
mod error;
mod parser;
mod raw_postcard;
mod serialize;
mod shape_deser;
#[cfg(feature = "axum")]
mod axum;
#[cfg(feature = "axum")]
pub use axum::{Postcard, PostcardRejection, PostcardSerializeRejection};
pub use error::{PostcardError, SerializeError};
pub use parser::PostcardParser;
pub use raw_postcard::{RawPostcard, opaque_encoded_borrowed, opaque_encoded_owned};
pub use serialize::{
ScatterPlan, Segment, Writer, peek_to_scatter_plan, peek_to_vec, to_scatter_plan, to_vec,
to_vec_with_shape, to_writer_fallible,
};
pub use shape_deser::from_slice_with_shape;
pub use facet_format::DeserializeError;
pub const DEFAULT_MAX_COLLECTION_ELEMENTS: u64 = 1 << 24;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DeserializeConfig {
max_collection_elements: u64,
}
impl Default for DeserializeConfig {
fn default() -> Self {
Self {
max_collection_elements: DEFAULT_MAX_COLLECTION_ELEMENTS,
}
}
}
impl DeserializeConfig {
pub const fn new() -> Self {
Self {
max_collection_elements: DEFAULT_MAX_COLLECTION_ELEMENTS,
}
}
pub const fn max_collection_elements(mut self, max_collection_elements: u64) -> Self {
self.max_collection_elements = max_collection_elements;
self
}
pub const fn get_max_collection_elements(self) -> u64 {
self.max_collection_elements
}
}
#[derive(Debug, Clone, Copy)]
pub struct Deserializer<'input> {
input: &'input [u8],
config: DeserializeConfig,
}
impl<'input> Deserializer<'input> {
pub const fn new(input: &'input [u8]) -> Self {
Self {
input,
config: DeserializeConfig::new(),
}
}
pub const fn with_config(input: &'input [u8], config: DeserializeConfig) -> Self {
Self { input, config }
}
pub const fn config(mut self, config: DeserializeConfig) -> Self {
self.config = config;
self
}
pub const fn max_collection_elements(mut self, max_collection_elements: u64) -> Self {
self.config = self.config.max_collection_elements(max_collection_elements);
self
}
fn parser(self) -> PostcardParser<'input> {
PostcardParser::with_limits(self.input, self.config.get_max_collection_elements())
}
pub fn deserialize<T>(self) -> Result<T, DeserializeError>
where
T: facet_core::Facet<'static>,
{
use facet_format::FormatDeserializer;
let mut parser = self.parser();
let mut de = FormatDeserializer::new_owned(&mut parser);
de.deserialize()
}
pub fn deserialize_borrowed<'facet, T>(self) -> Result<T, DeserializeError>
where
T: facet_core::Facet<'facet>,
'input: 'facet,
{
use facet_format::FormatDeserializer;
let mut parser = self.parser();
let mut de = FormatDeserializer::new(&mut parser);
de.deserialize()
}
pub fn deserialize_with_shape(
self,
source_shape: &'static facet_core::Shape,
) -> Result<facet_value::Value, DeserializeError> {
use facet_format::FormatDeserializer;
let mut parser = self.parser();
let mut de = FormatDeserializer::new_owned(&mut parser);
de.deserialize_with_shape(source_shape)
}
pub fn deserialize_into<'facet>(
self,
partial: facet_reflect::Partial<'facet, false>,
) -> Result<facet_reflect::Partial<'facet, false>, DeserializeError> {
use facet_format::{FormatDeserializer, MetaSource};
let mut parser = self.parser();
let mut de = FormatDeserializer::new_owned(&mut parser);
#[allow(unsafe_code)]
let partial: facet_reflect::Partial<'_, false> = unsafe {
core::mem::transmute::<
facet_reflect::Partial<'facet, false>,
facet_reflect::Partial<'_, false>,
>(partial)
};
let partial = de.deserialize_into(partial, MetaSource::FromEvents)?;
#[allow(unsafe_code)]
let partial: facet_reflect::Partial<'facet, false> = unsafe {
core::mem::transmute::<
facet_reflect::Partial<'_, false>,
facet_reflect::Partial<'facet, false>,
>(partial)
};
Ok(partial)
}
pub fn deserialize_into_borrowed<'facet>(
self,
partial: facet_reflect::Partial<'facet, true>,
) -> Result<facet_reflect::Partial<'facet, true>, DeserializeError>
where
'input: 'facet,
{
use facet_format::{FormatDeserializer, MetaSource};
let mut parser = self.parser();
let mut de = FormatDeserializer::new(&mut parser);
de.deserialize_into(partial, MetaSource::FromEvents)
}
}
pub fn from_slice<T>(input: &[u8]) -> Result<T, DeserializeError>
where
T: facet_core::Facet<'static>,
{
Deserializer::new(input).deserialize()
}
pub fn from_slice_borrowed<'input, 'facet, T>(input: &'input [u8]) -> Result<T, DeserializeError>
where
T: facet_core::Facet<'facet>,
'input: 'facet,
{
Deserializer::new(input).deserialize_borrowed()
}
pub fn from_slice_into<'facet>(
input: &[u8],
partial: facet_reflect::Partial<'facet, false>,
) -> Result<facet_reflect::Partial<'facet, false>, DeserializeError> {
Deserializer::new(input).deserialize_into(partial)
}
pub fn from_slice_into_borrowed<'input, 'facet>(
input: &'input [u8],
partial: facet_reflect::Partial<'facet, true>,
) -> Result<facet_reflect::Partial<'facet, true>, DeserializeError>
where
'input: 'facet,
{
Deserializer::new(input).deserialize_into_borrowed(partial)
}