ruma_common/push/condition/
flattened_json.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
use std::collections::BTreeMap;

use as_variant::as_variant;
use js_int::Int;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde_json::{to_value as to_json_value, value::Value as JsonValue};
use thiserror::Error;
use tracing::{instrument, warn};

use crate::serde::Raw;

/// The flattened representation of a JSON object.
#[derive(Clone, Debug)]
pub struct FlattenedJson {
    /// The internal map containing the flattened JSON as a pair path, value.
    map: BTreeMap<String, FlattenedJsonValue>,
}

impl FlattenedJson {
    /// Create a `FlattenedJson` from `Raw`.
    pub fn from_raw<T>(raw: &Raw<T>) -> Self {
        let mut s = Self { map: BTreeMap::new() };
        s.flatten_value(to_json_value(raw).unwrap(), "".into());
        s
    }

    /// Flatten and insert the `value` at `path`.
    #[instrument(skip(self, value))]
    fn flatten_value(&mut self, value: JsonValue, path: String) {
        match value {
            JsonValue::Object(fields) => {
                if fields.is_empty() {
                    if self.map.insert(path.clone(), FlattenedJsonValue::EmptyObject).is_some() {
                        warn!("Duplicate path in flattened JSON: {path}");
                    }
                } else {
                    for (key, value) in fields {
                        let key = escape_key(&key);
                        let path = if path.is_empty() { key } else { format!("{path}.{key}") };
                        self.flatten_value(value, path);
                    }
                }
            }
            value => {
                if let Some(v) = FlattenedJsonValue::from_json_value(value) {
                    if self.map.insert(path.clone(), v).is_some() {
                        warn!("Duplicate path in flattened JSON: {path}");
                    }
                }
            }
        }
    }

    /// Get the value associated with the given `path`.
    pub fn get(&self, path: &str) -> Option<&FlattenedJsonValue> {
        self.map.get(path)
    }

    /// Get the value associated with the given `path`, if it is a string.
    pub fn get_str(&self, path: &str) -> Option<&str> {
        self.map.get(path).and_then(|v| v.as_str())
    }

    /// Whether this flattened JSON contains an `m.mentions` property under the `content` property.
    pub fn contains_mentions(&self) -> bool {
        self.map
            .keys()
            .any(|s| s == r"content.m\.mentions" || s.starts_with(r"content.m\.mentions."))
    }
}

/// Escape a key for path matching.
///
/// This escapes the dots (`.`) and backslashes (`\`) in the key with a backslash.
fn escape_key(key: &str) -> String {
    key.replace('\\', r"\\").replace('.', r"\.")
}

/// The set of possible errors when converting to a JSON subset.
#[derive(Debug, Error)]
#[allow(clippy::exhaustive_enums)]
enum IntoJsonSubsetError {
    /// The numeric value failed conversion to js_int::Int.
    #[error("number found is not a valid `js_int::Int`")]
    IntConvert,

    /// The JSON type is not accepted in this subset.
    #[error("JSON type is not accepted in this subset")]
    NotInSubset,
}

/// Scalar (non-compound) JSON values.
#[derive(Debug, Clone, Default, Eq, PartialEq)]
#[allow(clippy::exhaustive_enums)]
pub enum ScalarJsonValue {
    /// Represents a `null` value.
    #[default]
    Null,

    /// Represents a boolean.
    Bool(bool),

    /// Represents an integer.
    Integer(Int),

    /// Represents a string.
    String(String),
}

impl ScalarJsonValue {
    fn try_from_json_value(val: JsonValue) -> Result<Self, IntoJsonSubsetError> {
        Ok(match val {
            JsonValue::Bool(b) => Self::Bool(b),
            JsonValue::Number(num) => Self::Integer(
                Int::try_from(num.as_i64().ok_or(IntoJsonSubsetError::IntConvert)?)
                    .map_err(|_| IntoJsonSubsetError::IntConvert)?,
            ),
            JsonValue::String(string) => Self::String(string),
            JsonValue::Null => Self::Null,
            _ => Err(IntoJsonSubsetError::NotInSubset)?,
        })
    }

    /// If the `ScalarJsonValue` is a `Bool`, return the inner value.
    pub fn as_bool(&self) -> Option<bool> {
        as_variant!(self, Self::Bool).copied()
    }

    /// If the `ScalarJsonValue` is an `Integer`, return the inner value.
    pub fn as_integer(&self) -> Option<Int> {
        as_variant!(self, Self::Integer).copied()
    }

    /// If the `ScalarJsonValue` is a `String`, return a reference to the inner value.
    pub fn as_str(&self) -> Option<&str> {
        as_variant!(self, Self::String)
    }
}

impl Serialize for ScalarJsonValue {
    #[inline]
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        match self {
            Self::Null => serializer.serialize_unit(),
            Self::Bool(b) => serializer.serialize_bool(*b),
            Self::Integer(n) => n.serialize(serializer),
            Self::String(s) => serializer.serialize_str(s),
        }
    }
}

impl<'de> Deserialize<'de> for ScalarJsonValue {
    #[inline]
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let val = JsonValue::deserialize(deserializer)?;
        ScalarJsonValue::try_from_json_value(val).map_err(serde::de::Error::custom)
    }
}

impl From<bool> for ScalarJsonValue {
    fn from(value: bool) -> Self {
        Self::Bool(value)
    }
}

impl From<Int> for ScalarJsonValue {
    fn from(value: Int) -> Self {
        Self::Integer(value)
    }
}

impl From<String> for ScalarJsonValue {
    fn from(value: String) -> Self {
        Self::String(value)
    }
}

impl From<&str> for ScalarJsonValue {
    fn from(value: &str) -> Self {
        value.to_owned().into()
    }
}

impl PartialEq<FlattenedJsonValue> for ScalarJsonValue {
    fn eq(&self, other: &FlattenedJsonValue) -> bool {
        match self {
            Self::Null => *other == FlattenedJsonValue::Null,
            Self::Bool(b) => other.as_bool() == Some(*b),
            Self::Integer(i) => other.as_integer() == Some(*i),
            Self::String(s) => other.as_str() == Some(s),
        }
    }
}

/// Possible JSON values after an object is flattened.
#[derive(Debug, Clone, Default, Eq, PartialEq)]
#[allow(clippy::exhaustive_enums)]
pub enum FlattenedJsonValue {
    /// Represents a `null` value.
    #[default]
    Null,

    /// Represents a boolean.
    Bool(bool),

    /// Represents an integer.
    Integer(Int),

    /// Represents a string.
    String(String),

    /// Represents an array.
    Array(Vec<ScalarJsonValue>),

    /// Represents an empty object.
    EmptyObject,
}

impl FlattenedJsonValue {
    fn from_json_value(val: JsonValue) -> Option<Self> {
        Some(match val {
            JsonValue::Bool(b) => Self::Bool(b),
            JsonValue::Number(num) => Self::Integer(Int::try_from(num.as_i64()?).ok()?),
            JsonValue::String(string) => Self::String(string),
            JsonValue::Null => Self::Null,
            JsonValue::Array(vec) => Self::Array(
                // Drop values we don't need instead of throwing an error.
                vec.into_iter()
                    .filter_map(|v| ScalarJsonValue::try_from_json_value(v).ok())
                    .collect::<Vec<_>>(),
            ),
            _ => None?,
        })
    }

    /// If the `FlattenedJsonValue` is a `Bool`, return the inner value.
    pub fn as_bool(&self) -> Option<bool> {
        as_variant!(self, Self::Bool).copied()
    }

    /// If the `FlattenedJsonValue` is an `Integer`, return the inner value.
    pub fn as_integer(&self) -> Option<Int> {
        as_variant!(self, Self::Integer).copied()
    }

    /// If the `FlattenedJsonValue` is a `String`, return a reference to the inner value.
    pub fn as_str(&self) -> Option<&str> {
        as_variant!(self, Self::String)
    }

    /// If the `FlattenedJsonValue` is an `Array`, return a reference to the inner value.
    pub fn as_array(&self) -> Option<&[ScalarJsonValue]> {
        as_variant!(self, Self::Array)
    }
}

impl From<bool> for FlattenedJsonValue {
    fn from(value: bool) -> Self {
        Self::Bool(value)
    }
}

impl From<Int> for FlattenedJsonValue {
    fn from(value: Int) -> Self {
        Self::Integer(value)
    }
}

impl From<String> for FlattenedJsonValue {
    fn from(value: String) -> Self {
        Self::String(value)
    }
}

impl From<&str> for FlattenedJsonValue {
    fn from(value: &str) -> Self {
        value.to_owned().into()
    }
}

impl From<Vec<ScalarJsonValue>> for FlattenedJsonValue {
    fn from(value: Vec<ScalarJsonValue>) -> Self {
        Self::Array(value)
    }
}

impl PartialEq<ScalarJsonValue> for FlattenedJsonValue {
    fn eq(&self, other: &ScalarJsonValue) -> bool {
        match self {
            Self::Null => *other == ScalarJsonValue::Null,
            Self::Bool(b) => other.as_bool() == Some(*b),
            Self::Integer(i) => other.as_integer() == Some(*i),
            Self::String(s) => other.as_str() == Some(s),
            Self::Array(_) | Self::EmptyObject => false,
        }
    }
}

#[cfg(test)]
mod tests {
    use js_int::int;
    use maplit::btreemap;
    use serde_json::Value as JsonValue;

    use super::{FlattenedJson, FlattenedJsonValue};
    use crate::serde::Raw;

    #[test]
    fn flattened_json_values() {
        let raw = serde_json::from_str::<Raw<JsonValue>>(
            r#"{
                "string": "Hello World",
                "number": 10,
                "array": [1, 2],
                "boolean": true,
                "null": null,
                "empty_object": {}
            }"#,
        )
        .unwrap();

        let flattened = FlattenedJson::from_raw(&raw);
        assert_eq!(
            flattened.map,
            btreemap! {
                "string".into() => "Hello World".into(),
                "number".into() => int!(10).into(),
                "array".into() => vec![int!(1).into(), int!(2).into()].into(),
                "boolean".into() => true.into(),
                "null".into() => FlattenedJsonValue::Null,
                "empty_object".into() => FlattenedJsonValue::EmptyObject,
            }
        );
    }

    #[test]
    fn flattened_json_nested() {
        let raw = serde_json::from_str::<Raw<JsonValue>>(
            r#"{
                "desc": "Level 0",
                "desc.bis": "Level 0 bis",
                "up": {
                    "desc": 1,
                    "desc.bis": null,
                    "up": {
                        "desc": ["Level 2a", "Level 2b"],
                        "desc\\bis": true
                    }
                }
            }"#,
        )
        .unwrap();

        let flattened = FlattenedJson::from_raw(&raw);
        assert_eq!(
            flattened.map,
            btreemap! {
                "desc".into() => "Level 0".into(),
                r"desc\.bis".into() => "Level 0 bis".into(),
                "up.desc".into() => int!(1).into(),
                r"up.desc\.bis".into() => FlattenedJsonValue::Null,
                "up.up.desc".into() => vec!["Level 2a".into(), "Level 2b".into()].into(),
                r"up.up.desc\\bis".into() => true.into(),
            },
        );
    }

    #[test]
    fn contains_mentions() {
        let raw = serde_json::from_str::<Raw<JsonValue>>(
            r#"{
                "m.mentions": {},
                "content": {
                    "body": "Text"
                }
            }"#,
        )
        .unwrap();

        let flattened = FlattenedJson::from_raw(&raw);
        assert!(!flattened.contains_mentions());

        let raw = serde_json::from_str::<Raw<JsonValue>>(
            r#"{
                "content": {
                    "body": "Text",
                    "m.mentions": {}
                }
            }"#,
        )
        .unwrap();

        let flattened = FlattenedJson::from_raw(&raw);
        assert!(flattened.contains_mentions());

        let raw = serde_json::from_str::<Raw<JsonValue>>(
            r#"{
                "content": {
                    "body": "Text",
                    "m.mentions": {
                        "room": true
                    }
                }
            }"#,
        )
        .unwrap();

        let flattened = FlattenedJson::from_raw(&raw);
        assert!(flattened.contains_mentions());
    }
}