ruma_serde/
lib.rs

1#![doc(html_favicon_url = "https://www.ruma.io/favicon.ico")]
2#![doc(html_logo_url = "https://www.ruma.io/images/logo.png")]
3//! ⚠ **Deprecated**: this crate has been merged into [ruma-common]. ⚠
4//!
5//! [ruma-common]: https://crates.io/crates/ruma-common
6
7#![warn(missing_docs)]
8
9use serde::{de, Deserialize};
10use serde_json::{value::RawValue as RawJsonValue, Value as JsonValue};
11
12pub mod base64;
13mod buf;
14pub mod can_be_empty;
15mod canonical_json;
16mod cow;
17pub mod duration;
18mod empty;
19pub mod json_string;
20mod raw;
21pub mod single_element_seq;
22mod strings;
23pub mod test;
24pub mod urlencoded;
25
26pub use self::{
27    base64::{Base64, Base64DecodeError},
28    buf::{json_to_buf, slice_to_buf},
29    can_be_empty::{is_empty, CanBeEmpty},
30    canonical_json::{
31        to_canonical_value, try_from_json_map,
32        value::{CanonicalJsonValue, Object as CanonicalJsonObject},
33        Error as CanonicalJsonError,
34    },
35    cow::deserialize_cow_str,
36    empty::vec_as_map_of_empty,
37    raw::Raw,
38    strings::{
39        btreemap_deserialize_v1_powerlevel_values, deserialize_v1_powerlevel, empty_string_as_none,
40        none_as_empty_string,
41    },
42};
43
44/// The inner type of [`JsonValue::Object`].
45pub type JsonObject = serde_json::Map<String, JsonValue>;
46
47/// Check whether a value is equal to its default value.
48pub fn is_default<T: Default + PartialEq>(val: &T) -> bool {
49    *val == T::default()
50}
51
52/// Simply returns `true`.
53///
54/// Useful for `#[serde(default = ...)]`.
55pub fn default_true() -> bool {
56    true
57}
58
59/// Simply dereferences the given bool.
60///
61/// Useful for `#[serde(skip_serializing_if = ...)]`.
62#[allow(clippy::trivially_copy_pass_by_ref)]
63pub fn is_true(b: &bool) -> bool {
64    *b
65}
66
67/// Helper function for `serde_json::value::RawValue` deserialization.
68pub fn from_raw_json_value<'a, T, E>(val: &'a RawJsonValue) -> Result<T, E>
69where
70    T: Deserialize<'a>,
71    E: de::Error,
72{
73    serde_json::from_str(val.get()).map_err(E::custom)
74}
75
76/// A type that can be sent to another party that understands the matrix protocol.
77///
78/// If any of the fields of `Self` don't implement serde's `Deserialize`, you can derive this trait
79/// to generate a corresponding 'Incoming' type that supports deserialization. This is useful for
80/// things like ruma_events' `EventResult` type. For more details, see the
81/// [derive macro's documentation][doc].
82///
83/// [doc]: derive.Outgoing.html
84// TODO: Better explain how this trait relates to serde's traits
85pub trait Outgoing {
86    /// The 'Incoming' variant of `Self`.
87    type Incoming;
88}
89
90// -- Everything below is macro-related --
91
92pub use ruma_serde_macros::*;
93
94/// This module is used to support the generated code from ruma-serde-macros.
95/// It is not considered part of ruma-serde's public API.
96#[doc(hidden)]
97pub mod exports {
98    pub use serde;
99}