pub struct Encoding<M = Text> { /* private fields */ }Expand description
Setting up encoding with parameters.
Implementations§
Source§impl Encoding<Text>
impl Encoding<Text>
Sourcepub const fn new() -> Self
pub const fn new() -> Self
Construct a new Encoding.
You can modify this using the available factory methods:
use musli_json::Encoding;
use musli::{Encode, Decode};
const CONFIG: Encoding<Json> = Encoding::new().with_mode();
// Mode marker indicating that some attributes should
// only apply when we're decoding in a JSON mode.
enum Json {}
#[derive(Debug, PartialEq, Encode, Decode)]
#[musli(mode = Json, name_all = "name")]
struct Struct<'a> {
name: &'a str,
age: u32,
}
let expected = Struct {
name: "Aristotle",
age: 61,
};
let out = CONFIG.to_vec(&expected).unwrap();
println!("{}", core::str::from_utf8(out.as_slice()).unwrap());
let out = musli_json::to_vec(&expected).unwrap();
println!("{}", core::str::from_utf8(out.as_slice()).unwrap());
let actual = musli_json::from_slice(out.as_slice()).unwrap();
assert_eq!(expected, actual);Source§impl<M> Encoding<M>
impl<M> Encoding<M>
Sourcepub const fn with_mode<T>(self) -> Encoding<T>
pub const fn with_mode<T>(self) -> Encoding<T>
Change the mode of the encoding.
§Examples
use musli_json::Encoding;
enum Custom {}
const CONFIG: Encoding<Custom> = Encoding::new().with_mode();Sourcepub fn encode_with<C, W, T>(
self,
cx: &C,
writer: W,
value: &T,
) -> Result<(), C::Error>
pub fn encode_with<C, W, T>( self, cx: &C, writer: W, value: &T, ) -> Result<(), C::Error>
Encode the given value to the given Writer using the current
configuration.
This is the same as Encoding::encode but allows for using a
configurable Context.
Sourcepub fn to_string<T>(self, value: &T) -> Result<String, Error>
pub fn to_string<T>(self, value: &T) -> Result<String, Error>
Encode the given value to a String using the current configuration.
Sourcepub fn to_string_with<T, C>(self, cx: &C, value: &T) -> Result<String, C::Error>
pub fn to_string_with<T, C>(self, cx: &C, value: &T) -> Result<String, C::Error>
Encode the given value to a String using the current configuration.
This is the same as Encoding::to_string but allows for using a
configurable Context.
Sourcepub fn decode<'de, P, T>(self, parser: P) -> Result<T, Error>
pub fn decode<'de, P, T>(self, parser: P) -> Result<T, Error>
Decode the given type T from the given Parser using the current
configuration.
Sourcepub fn decode_with<'de, C, P, T>(self, cx: &C, parser: P) -> Result<T, C::Error>
pub fn decode_with<'de, C, P, T>(self, cx: &C, parser: P) -> Result<T, C::Error>
Decode the given type T from the given Parser using the current
configuration.
This is the same as Encoding::decode but allows for using a
configurable Context.
Sourcepub fn from_str<'de, T>(self, string: &'de str) -> Result<T, Error>where
T: Decode<'de, M>,
pub fn from_str<'de, T>(self, string: &'de str) -> Result<T, Error>where
T: Decode<'de, M>,
Decode the given type T from the given string using the current
configuration.
Sourcepub fn from_str_with<'de, C, T>(
self,
cx: &C,
string: &'de str,
) -> Result<T, C::Error>
pub fn from_str_with<'de, C, T>( self, cx: &C, string: &'de str, ) -> Result<T, C::Error>
Decode the given type T from the given string using the current
configuration.
This is the same as Encoding::from_str but allows for using a
configurable Context.
Sourcepub fn from_slice<'de, T>(self, bytes: &'de [u8]) -> Result<T, Error>where
T: Decode<'de, M>,
pub fn from_slice<'de, T>(self, bytes: &'de [u8]) -> Result<T, Error>where
T: Decode<'de, M>,
Decode the given type T from the given slice using the current
configuration.
Sourcepub fn from_slice_with<'de, C, T>(
self,
cx: &C,
bytes: &'de [u8],
) -> Result<T, C::Error>
pub fn from_slice_with<'de, C, T>( self, cx: &C, bytes: &'de [u8], ) -> Result<T, C::Error>
Decode the given type T from the given slice using the current
configuration.
This is the same as Encoding::from_slice but allows for using a
configurable Context.
Sourcepub fn encode<W, T>(self, writer: W, value: &T) -> Result<(), Error>
pub fn encode<W, T>(self, writer: W, value: &T) -> Result<(), Error>
Encode the given value to the given Writer using the current
configuration.
Sourcepub fn to_writer<W, T>(self, write: W, value: &T) -> Result<(), Error>
pub fn to_writer<W, T>(self, write: W, value: &T) -> Result<(), Error>
Encode the given value to the given Write using the current configuration.
Sourcepub fn to_writer_with<C, W, T>(
self,
cx: &C,
write: W,
value: &T,
) -> Result<(), C::Error>
pub fn to_writer_with<C, W, T>( self, cx: &C, write: W, value: &T, ) -> Result<(), C::Error>
Encode the given value to the given Write using the current
configuration and context C.
Sourcepub fn to_vec<T>(self, value: &T) -> Result<Vec<u8>, Error>
pub fn to_vec<T>(self, value: &T) -> Result<Vec<u8>, Error>
Encode the given value to a Vec using the current configuration.
Sourcepub fn to_vec_with<C, T>(self, cx: &C, value: &T) -> Result<Vec<u8>, C::Error>
pub fn to_vec_with<C, T>(self, cx: &C, value: &T) -> Result<Vec<u8>, C::Error>
Encode the given value to a Vec using the current configuration.
This is the same as Encoding::to_vec, but allows for using a
configurable Context.
Sourcepub fn to_fixed_bytes<const N: usize, T>(
self,
value: &T,
) -> Result<FixedBytes<N>, Error>
pub fn to_fixed_bytes<const N: usize, T>( self, value: &T, ) -> Result<FixedBytes<N>, Error>
Encode the given value to a fixed-size bytes using the current configuration.