messagepack_core/encode/
mod.rs1pub mod array;
2pub mod bin;
3pub mod bool;
4pub mod extension;
5pub mod float;
6pub mod int;
7pub mod map;
8pub mod nil;
9pub mod str;
10
11pub use array::{ArrayDataEncoder, ArrayEncoder, ArrayFormatEncoder};
12pub use bin::BinaryEncoder;
13pub use extension::ExtensionEncoder;
14pub use map::{MapDataEncoder, MapEncoder, MapFormatEncoder, MapSliceEncoder};
15pub use nil::NilEncoder;
16
17use crate::{Format, io::IoWrite};
18
19#[derive(Debug, Copy, Clone, PartialOrd, Ord, PartialEq, Eq)]
21pub enum Error<T> {
22 Io(T),
24 InvalidFormat,
26}
27
28impl<T> From<T> for Error<T> {
29 fn from(value: T) -> Self {
30 Error::Io(value)
31 }
32}
33
34impl<T: core::fmt::Display> core::fmt::Display for Error<T> {
35 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
36 match self {
37 Error::Io(e) => write!(f, "{}", e),
38 Error::InvalidFormat => write!(f, "Cannot encode value"),
39 }
40 }
41}
42
43impl<T: core::error::Error> core::error::Error for Error<T> {}
44
45type Result<T, E> = ::core::result::Result<T, Error<E>>;
46
47pub trait Encode<W>
49where
50 W: IoWrite,
51{
52 fn encode(&self, writer: &mut W) -> Result<usize, W::Error>;
54}
55
56impl<V, W> Encode<W> for &V
57where
58 V: Encode<W>,
59 W: IoWrite,
60{
61 fn encode(&self, writer: &mut W) -> Result<usize, <W as IoWrite>::Error> {
62 Encode::encode(*self, writer)
63 }
64}
65
66impl<V, W> Encode<W> for &mut V
67where
68 V: Encode<W>,
69 W: IoWrite,
70{
71 fn encode(&self, writer: &mut W) -> Result<usize, <W as IoWrite>::Error> {
72 Encode::encode(*self, writer)
73 }
74}
75
76impl<W> Encode<W> for Format
77where
78 W: IoWrite,
79{
80 fn encode(&self, writer: &mut W) -> Result<usize, <W as IoWrite>::Error> {
81 writer.write_bytes(&self.as_slice())?;
82 Ok(1)
83 }
84}