File: ruma_api_macros.rs

package info (click to toggle)
rust-ruma-common 0.10.5-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,208 kB
  • sloc: makefile: 2
file content (140 lines) | stat: -rw-r--r-- 3,895 bytes parent folder | download | duplicates (2)
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
#![allow(clippy::exhaustive_structs)]

pub mod some_endpoint {
    use ruma_common::{
        api::ruma_api,
        events::{tag::TagEvent, AnyTimelineEvent},
        serde::Raw,
        OwnedUserId,
    };

    ruma_api! {
        metadata: {
            description: "Does something.",
            method: POST, // An `http::Method` constant. No imports required.
            name: "some_endpoint",
            unstable_path: "/_matrix/some/endpoint/:user",
            rate_limited: false,
            authentication: None,
        }

        request: {
            // With no attribute on the field, it will be put into the body of the request.
            pub a_field: String,

            // This value will be put into the "Content-Type" HTTP header.
            #[ruma_api(header = CONTENT_TYPE)]
            pub content_type: String,

            // This value will be put into the query string of the request's URL.
            #[ruma_api(query)]
            pub bar: String,

            // This value will be inserted into the request's URL in place of the
            // ":user" path component.
            #[ruma_api(path)]
            pub user: OwnedUserId,
        }

        response: {
            // This value will be extracted from the "Content-Type" HTTP header.
            #[ruma_api(header = CONTENT_TYPE)]
            pub content_type: String,

            // With no attribute on the field, it will be extracted from the body of the response.
            pub value: String,

            // You can use serde attributes on any kind of field
            #[serde(skip_serializing_if = "Option::is_none")]
            pub optional_flag: Option<bool>,

            // Use `Raw` instead of the actual event to allow additional fields to be sent...
            pub event: Raw<TagEvent>,

            // ... and to allow unknown events when the endpoint deals with event collections.
            pub list_of_events: Vec<Raw<AnyTimelineEvent>>,
        }
    }
}

pub mod newtype_body_endpoint {
    use ruma_common::api::ruma_api;

    #[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
    pub struct MyCustomType {
        pub a_field: String,
    }

    ruma_api! {
        metadata: {
            description: "Does something.",
            method: PUT,
            name: "newtype_body_endpoint",
            unstable_path: "/_matrix/some/newtype/body/endpoint",
            rate_limited: false,
            authentication: None,
        }

        request: {
            #[ruma_api(body)]
            pub list_of_custom_things: Vec<MyCustomType>,
        }

        response: {
            #[ruma_api(body)]
            pub my_custom_thing: MyCustomType,
        }
    }
}

pub mod raw_body_endpoint {
    use ruma_common::api::ruma_api;

    #[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
    pub struct MyCustomType {
        pub a_field: String,
    }

    ruma_api! {
        metadata: {
            description: "Does something.",
            method: PUT,
            name: "newtype_body_endpoint",
            unstable_path: "/_matrix/some/newtype/body/endpoint",
            rate_limited: false,
            authentication: None,
        }

        request: {
            #[ruma_api(raw_body)]
            pub file: &'a [u8],
        }

        response: {
            #[ruma_api(raw_body)]
            pub file: Vec<u8>,
        }
    }
}

pub mod query_map_endpoint {
    use ruma_common::api::ruma_api;

    ruma_api! {
        metadata: {
            description: "Does something.",
            method: GET,
            name: "newtype_body_endpoint",
            unstable_path: "/_matrix/some/query/map/endpoint",
            rate_limited: false,
            authentication: None,
        }

        request: {
            #[ruma_api(query_map)]
            pub fields: Vec<(String, String)>,
        }

        response: {}
    }
}