ruma_identity_service_api/discovery/get_supported_versions.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
//! `GET /_matrix/identity/versions` ([spec])
//!
//! Get the versions of the identity service API supported by this endpoint.
//!
//! Note: This endpoint was only implemented in/after 1.1, so a 404 could indicate the server only
//! supports 1.0 endpoints. Please use [`server_status`](super::get_server_status) to
//! double-check.
//!
//! Note: This endpoint does not contain an unstable variant for 1.0.
//!
//! [spec]: https://spec.matrix.org/latest/identity-service-api/#get_matrixidentityversions
use std::collections::BTreeMap;
use ruma_common::{
api::{request, response, MatrixVersion, Metadata},
metadata,
};
const METADATA: Metadata = metadata! {
method: GET,
rate_limited: false,
authentication: None,
history: {
1.1 => "/_matrix/identity/versions",
}
};
/// Request type for the `versions` endpoint.
#[request]
#[derive(Default)]
pub struct Request {}
/// Response type for the `versions` endpoint.
#[response]
pub struct Response {
/// A list of Matrix client API protocol versions supported by the endpoint.
pub versions: Vec<String>,
}
impl Request {
/// Creates an empty `Request`.
pub fn new() -> Self {
Self {}
}
}
impl Response {
/// Creates a new `Response` with the given `versions`.
pub fn new(versions: Vec<String>) -> Self {
Self { versions }
}
/// Extracts known Matrix versions from this response.
///
/// Matrix versions that Ruma cannot parse, or does not know about, are discarded.
///
/// The versions returned will be sorted from oldest to latest. Use [`.find()`][Iterator::find]
/// or [`.rfind()`][DoubleEndedIterator::rfind] to look for a minimum or maximum version to use
/// given some constraint.
pub fn known_versions(&self) -> impl DoubleEndedIterator<Item = MatrixVersion> {
self.versions
.iter()
// Parse, discard unknown versions
.flat_map(|s| s.parse::<MatrixVersion>())
// Map to key-value pairs where the key is the major-minor representation
// (which can be used as a BTreeMap unlike MatrixVersion itself)
.map(|v| (v.into_parts(), v))
// Collect to BTreeMap
.collect::<BTreeMap<_, _>>()
// Return an iterator over just the values (`MatrixVersion`s)
.into_values()
}
}