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
pub mod v3 {
use ruma_common::{api::ruma_api, MilliSecondsSinceUnixEpoch};
use serde::Serialize;
use serde_json::value::{to_raw_value as to_raw_json_value, RawValue as RawJsonValue};
ruma_api! {
metadata: {
description: "Get a preview for a URL.",
name: "get_media_preview",
method: GET,
r0_path: "/_matrix/media/r0/preview_url",
stable_path: "/_matrix/media/v3/preview_url",
rate_limited: true,
authentication: AccessToken,
added: 1.0,
}
request: {
#[ruma_api(query)]
pub url: &'a str,
#[ruma_api(query)]
pub ts: MilliSecondsSinceUnixEpoch,
}
#[derive(Default)]
response: {
#[ruma_api(body)]
pub data: Option<Box<RawJsonValue>>,
}
error: crate::Error
}
impl<'a> Request<'a> {
pub fn new(url: &'a str, ts: MilliSecondsSinceUnixEpoch) -> Self {
Self { url, ts }
}
}
impl Response {
pub fn new() -> Self {
Self { data: None }
}
pub fn from_raw_value(data: Box<RawJsonValue>) -> Self {
Self { data: Some(data) }
}
pub fn from_serialize<T: Serialize>(data: &T) -> serde_json::Result<Self> {
Ok(Self { data: Some(to_raw_json_value(data)?) })
}
}
#[cfg(test)]
mod tests {
use serde_json::{
from_value as from_json_value, json,
value::{to_raw_value as to_raw_json_value, RawValue as RawJsonValue},
};
#[test]
fn raw_json_deserialize() {
type OptRawJson = Option<Box<RawJsonValue>>;
assert!(from_json_value::<OptRawJson>(json!(null)).unwrap().is_none());
assert!(from_json_value::<OptRawJson>(json!("test")).unwrap().is_some());
assert!(from_json_value::<OptRawJson>(json!({ "a": "b" })).unwrap().is_some());
}
#[test]
fn raw_json_serialize() {
assert!(to_raw_json_value(&json!(null)).is_ok());
assert!(to_raw_json_value(&json!("string")).is_ok());
assert!(to_raw_json_value(&json!({})).is_ok());
assert!(to_raw_json_value(&json!({ "a": "b" })).is_ok());
}
}
}