api: Rework VersionHistory path accessors

* Return iterators
* Rename all_unstable_paths to unstable_paths
* Rename all_versioned_stable_paths to stable_paths
This commit is contained in:
Jonas Platte 2022-11-10 10:43:23 +01:00
parent 94990f60f2
commit 848ca9b225
No known key found for this signature in database
GPG Key ID: AAA7A61F696C3E0C
2 changed files with 11 additions and 10 deletions

View File

@ -391,20 +391,18 @@ impl VersionHistory {
}
/// Returns all path variants in canon form, for use in server routers.
pub fn all_paths(&self) -> Vec<&'static str> {
let unstable = self.unstable_paths.iter().copied();
let stable = self.stable_paths.iter().map(|(_, y)| *y);
unstable.chain(stable).collect()
pub fn all_paths(&self) -> impl Iterator<Item = &'static str> {
self.unstable_paths().chain(self.stable_paths().map(|(_, path)| path))
}
/// Returns all unstable path variants in canon form.
pub fn all_unstable_paths(&self) -> Vec<&'static str> {
self.unstable_paths.to_owned()
pub fn unstable_paths(&self) -> impl Iterator<Item = &'static str> {
self.unstable_paths.iter().copied()
}
/// Returns all stable path variants in canon form, with corresponding Matrix version.
pub fn all_versioned_stable_paths(&self) -> Vec<(MatrixVersion, &'static str)> {
self.stable_paths.iter().map(|(version, data)| (*version, *data)).collect()
pub fn stable_paths(&self) -> impl Iterator<Item = (MatrixVersion, &'static str)> {
self.stable_paths.iter().map(|(version, data)| (*version, *data))
}
/// The path that should be used to query the endpoint, given a series of versions.

View File

@ -61,9 +61,12 @@ ruma_api! {
fn main() {
use ruma_common::api::MatrixVersion;
assert_eq!(METADATA.history.all_unstable_paths(), &["/_matrix/some/msc1234/endpoint/:baz"],);
assert_eq!(
METADATA.history.all_versioned_stable_paths(),
METADATA.history.unstable_paths().collect::<Vec<_>>(),
&["/_matrix/some/msc1234/endpoint/:baz"],
);
assert_eq!(
METADATA.history.stable_paths().collect::<Vec<_>>(),
&[
(MatrixVersion::V1_0, "/_matrix/some/r0/endpoint/:baz"),
(MatrixVersion::V1_1, "/_matrix/some/v3/endpoint/:baz")