#![cfg(any(feature = "json", feature = "descriptive", feature = "value"))]
#![cfg_attr(doc_cfg, doc(cfg(feature = "value")))]
mod de;
mod en;
mod error;
mod type_hint;
mod value;
use core::marker;
#[cfg(feature = "alloc")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))]
pub type Result<T, E = Error> = core::result::Result<T, E>;
#[doc(inline)]
pub use self::value::{AsValueDecoder, IntoValueDecoder, Value};
use self::value::{Number, ValueKind};
#[doc(inline)]
pub use error::Error;
use crate::alloc::Allocator;
#[cfg(feature = "alloc")]
use crate::alloc::Global;
use crate::mode::{Binary, Text};
use crate::value::en::ValueEncoder;
use crate::{Context, Decode, Decoder, Encode, Encoder, Options};
const ENCODING_BINARY: Encoding<OPTIONS, Binary> = Encoding::new();
const ENCODING_TEXT: Encoding<OPTIONS, Text> = Encoding::new().with_mode();
pub const OPTIONS: Options = crate::options::new().build();
#[cfg(feature = "alloc")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))]
#[inline]
pub fn encode(value: impl Encode<Binary>) -> Result<Value<Global>, Error> {
ENCODING_BINARY.encode(value)
}
#[cfg(feature = "alloc")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))]
#[inline]
pub fn encode_text(value: impl Encode<Text>) -> Result<Value<Global>, Error> {
ENCODING_TEXT.encode(value)
}
#[inline]
pub fn encode_with<C>(cx: C, value: impl Encode<Binary>) -> Result<Value<C::Allocator>, C::Error>
where
C: Context,
{
ENCODING_BINARY.encode_with(cx, value)
}
#[inline]
pub fn encode_text_with<C>(cx: C, value: impl Encode<Text>) -> Result<Value<C::Allocator>, C::Error>
where
C: Context,
{
ENCODING_TEXT.encode_with(cx, value)
}
#[cfg(feature = "alloc")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))]
#[inline]
pub fn decode<'de, T>(value: &'de Value<impl Allocator>) -> Result<T, Error>
where
T: Decode<'de, Binary, Global>,
{
ENCODING_BINARY.decode(value)
}
#[cfg(feature = "alloc")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))]
#[inline]
pub fn decode_text<'de, T>(value: &'de Value<impl Allocator>) -> Result<T, Error>
where
T: Decode<'de, Text, Global>,
{
ENCODING_TEXT.decode(value)
}
#[inline]
pub fn decode_with<'de, C, T>(cx: C, value: &'de Value<impl Allocator>) -> Result<T, C::Error>
where
C: Context,
T: Decode<'de, Binary, C::Allocator>,
{
ENCODING_BINARY.decode_with(cx, value)
}
#[inline]
pub fn decode_text_with<'de, C, T>(cx: C, value: &'de Value<impl Allocator>) -> Result<T, C::Error>
where
C: Context,
T: Decode<'de, Text, C::Allocator>,
{
ENCODING_TEXT.decode_with(cx, value)
}
pub struct Encoding<const OPT: Options = OPTIONS, M = Binary>
where
M: 'static,
{
_marker: marker::PhantomData<M>,
}
impl Default for Encoding<OPTIONS, Binary> {
#[inline]
fn default() -> Self {
Self::new()
}
}
impl Encoding<OPTIONS, Binary> {
pub const fn new() -> Self {
Encoding {
_marker: marker::PhantomData,
}
}
}
impl<const OPT: Options, M> Encoding<OPT, M>
where
M: 'static,
{
pub const fn with_mode<T>(self) -> Encoding<OPT, T>
where
T: 'static,
{
Encoding {
_marker: marker::PhantomData,
}
}
pub const fn with_options<const U: Options>(self) -> Encoding<U, M> {
Encoding {
_marker: marker::PhantomData,
}
}
#[cfg(feature = "alloc")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))]
pub fn encode(&self, value: impl Encode<M>) -> Result<Value<Global>, Error> {
let mut output = Value::new(ValueKind::Empty);
let cx = crate::context::new().with_error();
ValueEncoder::<OPT, _, _, M>::new(&cx, 0, &mut output).encode(value)?;
Ok(output)
}
pub fn encode_with<C>(
&self,
cx: C,
value: impl Encode<M>,
) -> Result<Value<C::Allocator>, C::Error>
where
C: Context,
{
let mut output = Value::new(ValueKind::Empty);
ValueEncoder::<OPT, _, _, M>::new(cx, 0, &mut output).encode(value)?;
Ok(output)
}
#[cfg(feature = "alloc")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))]
pub fn decode<'de, T>(&self, value: &'de Value<impl Allocator>) -> Result<T, Error>
where
T: Decode<'de, M, Global>,
{
let cx = crate::context::new().with_error();
value.decoder::<OPT, _, M>(&cx).decode()
}
pub fn decode_with<'de, C, T>(
&self,
cx: C,
value: &'de Value<impl Allocator>,
) -> Result<T, C::Error>
where
C: Context,
T: Decode<'de, M, C::Allocator>,
{
cx.clear();
value.decoder::<OPT, _, M>(cx).decode()
}
}