diff --git a/ruma-api/Cargo.toml b/ruma-api/Cargo.toml index fd02984c..29b7c425 100644 --- a/ruma-api/Cargo.toml +++ b/ruma-api/Cargo.toml @@ -27,3 +27,4 @@ strum = "0.18.0" [dev-dependencies] ruma-events = { version = "0.21.3", path = "../ruma-events" } +trybuild = "1.0.30" diff --git a/ruma-api/tests/ruma_api.rs b/ruma-api/tests/ruma_api.rs new file mode 100644 index 00000000..f56f5a1e --- /dev/null +++ b/ruma-api/tests/ruma_api.rs @@ -0,0 +1,5 @@ +#[test] +fn ui() { + let t = trybuild::TestCases::new(); + t.pass("tests/ui/01-api-sanity-check.rs"); +} diff --git a/ruma-api/tests/ui/01-api-sanity-check.rs b/ruma-api/tests/ui/01-api-sanity-check.rs new file mode 100644 index 00000000..29ffcdaa --- /dev/null +++ b/ruma-api/tests/ui/01-api-sanity-check.rs @@ -0,0 +1,52 @@ +use ruma_api::ruma_api; +use ruma_events::{tag::TagEvent, AnyRoomEvent, EventJson}; + +ruma_api! { + metadata: { + description: "Does something.", + method: POST, // An `http::Method` constant. No imports required. + name: "some_endpoint", + path: "/_matrix/some/endpoint/:baz", + rate_limited: false, + requires_authentication: false, + } + + request: { + // With no attribute on the field, it will be put into the body of the request. + pub foo: 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 + // ":baz" path component. + #[ruma_api(path)] + pub baz: String, + } + + 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, + + // Use `EventJson` instead of the actual event to allow additional fields to be sent... + pub event: EventJson, + + // ... and to allow unknown events when the endpoint deals with event collections. + pub list_of_events: Vec>, + } +} + +fn main() {}