yew_stdweb/format/
mod.rs

1//! Utility module to convert data to types and back by
2//! specific formats like: JSON, BSON, TOML, YAML, XML.
3//!
4//! All types here are lazy and it's necessary to
5//! use `Into` and `From` traits to get (convert) the data.
6
7use anyhow::Error;
8use thiserror::Error as ThisError;
9
10#[macro_use]
11mod macros;
12
13#[cfg(feature = "bincode")]
14#[doc(hidden)]
15pub mod bincode;
16
17#[cfg(feature = "cbor")]
18#[doc(hidden)]
19pub mod cbor;
20
21#[doc(hidden)]
22pub mod json;
23
24#[doc(hidden)]
25#[cfg(feature = "msgpack")]
26pub mod msgpack;
27
28#[doc(hidden)]
29pub mod nothing;
30
31#[cfg(feature = "toml")]
32#[doc(hidden)]
33pub mod toml;
34
35#[cfg(feature = "yaml")]
36#[doc(hidden)]
37pub mod yaml;
38
39#[cfg(feature = "bincode")]
40#[doc(inline)]
41pub use self::bincode::Bincode;
42
43#[cfg(feature = "cbor")]
44#[doc(inline)]
45pub use self::cbor::Cbor;
46
47#[doc(inline)]
48pub use self::json::Json;
49
50#[cfg(feature = "msgpack")]
51#[doc(inline)]
52pub use self::msgpack::MsgPack;
53
54#[doc(inline)]
55pub use self::nothing::Nothing;
56
57#[cfg(feature = "toml")]
58#[doc(inline)]
59pub use self::toml::Toml;
60
61#[cfg(feature = "yaml")]
62#[doc(inline)]
63pub use self::yaml::Yaml;
64
65/// A representation of a value which can be stored and restored as a text.
66///
67/// Some formats are binary only and can't be serialized to or deserialized
68/// from Text.  Attempting to do so will return an Err(FormatError).
69pub type Text = Result<String, Error>;
70
71/// A representation of a value which can be stored and restored as a binary.
72pub type Binary = Result<Vec<u8>, Error>;
73
74/// A helper which represents a specific format.
75#[doc(hidden)]
76pub type Format<T> = Result<T, Error>;
77
78/// Represents formatting errors.
79#[derive(Debug, ThisError)]
80pub enum FormatError {
81    /// Received text for a binary format, e.g. someone sending text
82    /// on a WebSocket that is using a binary serialization format, like Cbor.
83    #[error("received text for a binary format")]
84    ReceivedTextForBinary,
85    /// Received binary for a text format, e.g. someone sending binary
86    /// on a WebSocket that is using a text serialization format, like Json.
87    #[error("received binary for a text format")]
88    ReceivedBinaryForText,
89    /// Trying to encode a binary format as text", e.g., trying to
90    /// store a Cbor encoded value in a String.
91    #[error("trying to encode a binary format as Text")]
92    CantEncodeBinaryAsText,
93}