1use serde::{de, Deserialize, Deserializer};
9use serde_json::{value::RawValue as RawJsonValue, Value as JsonValue};
10
11pub mod base64;
12mod buf;
13pub mod can_be_empty;
14mod cow;
15pub mod duration;
16pub mod json_string;
17mod raw;
18pub mod single_element_seq;
19mod strings;
20pub mod test;
21
22pub use self::{
23 base64::{Base64, Base64DecodeError},
24 buf::{json_to_buf, slice_to_buf},
25 can_be_empty::{is_empty, CanBeEmpty},
26 cow::deserialize_cow_str,
27 raw::Raw,
28 strings::{
29 btreemap_deserialize_v1_powerlevel_values, deserialize_as_number_or_string,
30 deserialize_as_optional_number_or_string, deserialize_v1_powerlevel, empty_string_as_none,
31 none_as_empty_string,
32 },
33};
34
35pub type JsonObject = serde_json::Map<String, JsonValue>;
37
38pub fn is_default<T: Default + PartialEq>(val: &T) -> bool {
40 *val == T::default()
41}
42
43pub fn none_as_default<'de, D, T>(deserializer: D) -> Result<T, D::Error>
45where
46 D: Deserializer<'de>,
47 T: Default + Deserialize<'de>,
48{
49 Ok(Option::deserialize(deserializer)?.unwrap_or_default())
50}
51
52pub fn default_true() -> bool {
56 true
57}
58
59#[allow(clippy::trivially_copy_pass_by_ref)]
63pub fn is_true(b: &bool) -> bool {
64 *b
65}
66
67pub 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
76pub use ruma_macros::{
77 AsRefStr, AsStrAsRefStr, DebugAsRefStr, DeserializeFromCowStr, DisplayAsRefStr, FromString,
78 OrdAsRefStr, PartialEqAsRefStr, PartialOrdAsRefStr, SerializeAsRefStr, StringEnum,
79 _FakeDeriveSerde,
80};