1
0
Fork 0
ruma/crates/ruma-common/tests/it/api/ui/api-version-history-check.rs
Kévin Commaille 7459fbb173 api: Run rustfmt on trybuild tests
Because they are not properly included in the module hierarchy,
`cargo fmt` doesn't check them by default so we use `rustfmt` directly
to check the files by path.
2025-11-21 11:31:45 +01:00

129 lines
4.2 KiB
Rust

#![allow(unexpected_cfgs)]
use std::borrow::Cow;
use http::header::CONTENT_TYPE;
use ruma_common::{
api::{
MatrixVersion, Metadata, SupportedVersions,
auth_scheme::NoAuthentication,
path_builder::{PathBuilder, StablePathSelector},
request, response,
},
metadata,
serde::Raw,
};
metadata! {
method: POST, // An `http::Method` constant. No imports required.
rate_limited: false,
authentication: NoAuthentication,
history: {
unstable => "/_matrix/some/msc1234/endpoint/{baz}",
unstable("org.bar.msc1234") => "/_matrix/unstable/org.bar.msc1234/endpoint/{baz}",
stable("org.bar.msc1234.stable") => "/_matrix/some/r0/endpint/{baz}",
1.0 | stable("org.bar.msc1234.stable.v2") => "/_matrix/some/r0/endpoint/{baz}",
1.1 => "/_matrix/some/v3/endpoint/{baz}",
1.2 => deprecated,
1.3 => removed,
}
}
/// Request type for the `some_endpoint` endpoint.
#[request]
pub struct 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 type for the `some_endpoint` endpoint.
#[response]
pub struct 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<Event>,
// ... and to allow unknown events when the endpoint deals with event collections.
pub list_of_events: Vec<Raw<Event>>,
}
// Dummy type to avoid circular dev-dependency that rust-analyzer doesn't like
pub struct Event {}
fn main() {
assert_eq!(
Request::PATH_BUILDER.unstable_paths().collect::<Vec<_>>(),
&[
(None, "/_matrix/some/msc1234/endpoint/{baz}"),
(Some("org.bar.msc1234"), "/_matrix/unstable/org.bar.msc1234/endpoint/{baz}")
],
);
assert_eq!(
Request::PATH_BUILDER.stable_paths().collect::<Vec<_>>(),
&[
(
StablePathSelector::Feature("org.bar.msc1234.stable"),
"/_matrix/some/r0/endpint/{baz}"
),
(
StablePathSelector::FeatureAndVersion {
feature: "org.bar.msc1234.stable.v2",
version: MatrixVersion::V1_0
},
"/_matrix/some/r0/endpoint/{baz}"
),
(StablePathSelector::Version(MatrixVersion::V1_1), "/_matrix/some/v3/endpoint/{baz}")
],
);
assert_eq!(
Request::PATH_BUILDER.all_paths().collect::<Vec<_>>(),
&[
"/_matrix/some/msc1234/endpoint/{baz}",
"/_matrix/unstable/org.bar.msc1234/endpoint/{baz}",
"/_matrix/some/r0/endpint/{baz}",
"/_matrix/some/r0/endpoint/{baz}",
"/_matrix/some/v3/endpoint/{baz}"
],
);
assert_eq!(Request::PATH_BUILDER.added_in(), Some(MatrixVersion::V1_0));
assert_eq!(Request::PATH_BUILDER.deprecated_in(), Some(MatrixVersion::V1_2));
assert_eq!(Request::PATH_BUILDER.removed_in(), Some(MatrixVersion::V1_3));
let path = Request::PATH_BUILDER
.select_path(Cow::Owned(SupportedVersions {
versions: [
MatrixVersion::V1_0,
MatrixVersion::V1_1,
MatrixVersion::V1_2,
MatrixVersion::V1_3,
]
.into(),
features: Default::default(),
}))
.unwrap();
assert_eq!(path, "/_matrix/some/v3/endpoint/{baz}");
}