chore: Fix URLs to old spec

This commit is contained in:
Kévin Commaille 2023-05-04 12:32:54 +02:00 committed by Kévin Commaille
parent d98903e46c
commit e97e46e623
2 changed files with 34 additions and 66 deletions

View File

@ -651,7 +651,7 @@ impl MatrixVersion {
/// Get the default [`RoomVersionId`] for this `MatrixVersion`. /// Get the default [`RoomVersionId`] for this `MatrixVersion`.
pub fn default_room_version(&self) -> RoomVersionId { pub fn default_room_version(&self) -> RoomVersionId {
match self { match self {
// <https://matrix.org/docs/spec/index.html#complete-list-of-room-versions> // <https://spec.matrix.org/historical/index.html#complete-list-of-room-versions>
MatrixVersion::V1_0 MatrixVersion::V1_0
// <https://spec.matrix.org/v1.1/rooms/#complete-list-of-room-versions> // <https://spec.matrix.org/v1.1/rooms/#complete-list-of-room-versions>
| MatrixVersion::V1_1 | MatrixVersion::V1_1

View File

@ -12,45 +12,29 @@ use isahc::ReadResponseExt;
use crate::Result; use crate::Result;
/// A Matrix spec. /// Authorized URLs pointing to the old specs.
#[derive(Debug, Clone, Copy)] const OLD_URL_WHITELIST: &[&str] =
enum Spec { &["https://spec.matrix.org/historical/index.html#complete-list-of-room-versions"];
/// The old Matrix spec.
Old,
/// The new Matrix spec.
New,
}
impl Spec { /// Authorized versions in URLs pointing to the new specs.
/// Authorized URLs pointing to the old specs. const NEW_VERSION_WHITELIST: &[&str] = &[
const OLD_URL_WHITELIST: &'static [&'static str] =
&["https://matrix.org/docs/spec/index.html#complete-list-of-room-versions"];
/// Authorized versions in URLs pointing to the new specs.
const NEW_VERSION_WHITELIST: &'static [&'static str] = &[
"v1.1", "v1.2", "v1.3", "v1.4", "v1.5", "v1.6", "v1.1", "v1.2", "v1.3", "v1.4", "v1.5", "v1.6",
"latest", "latest",
// This should only be enabled if a legitimate use case is found. // This should only be enabled if a legitimate use case is found.
// "unstable", // "unstable",
]; ];
/// Get the start of the URLs pointing to this `Spec`. /// The version of URLs pointing to the old spec.
const fn url_prefix(&self) -> &'static str { const OLD_VERSION: &str = "historical";
match self {
Spec::Old => "https://matrix.org/docs/spec/", /// The start of the URLs pointing to the spec.
Spec::New => "https://spec.matrix.org/", const URL_PREFIX: &str = "https://spec.matrix.org/";
}
}
}
/// A link to the spec. /// A link to the spec.
struct SpecLink { struct SpecLink {
/// The URL of the link. /// The URL of the link.
url: String, url: String,
/// The spec variant of the link.
spec: Spec,
/// The path of the file containing the link. /// The path of the file containing the link.
path: PathBuf, path: PathBuf,
@ -60,15 +44,8 @@ struct SpecLink {
impl SpecLink { impl SpecLink {
/// Create a new `SpecLink`. /// Create a new `SpecLink`.
fn new(url: String, spec: Spec, path: PathBuf, line: u16) -> Self { fn new(url: String, path: PathBuf, line: u16) -> Self {
Self { url, spec, path, line } Self { url, path, line }
}
/// Get the minimum length of the link.
///
/// That is the length of the start of the URL used to detect the link.
const fn min_len(&self) -> usize {
self.spec.url_prefix().len()
} }
} }
@ -111,16 +88,9 @@ fn collect_links(path: &Path) -> Result<Vec<SpecLink>> {
while content.read_line(&mut buf)? > 0 { while content.read_line(&mut buf)? > 0 {
line += 1; line += 1;
for spec in [Spec::Old, Spec::New] {
// If for some reason a line has 2 spec links. // If for some reason a line has 2 spec links.
for (start_idx, _) in buf.match_indices(spec.url_prefix()) { for (start_idx, _) in buf.match_indices(URL_PREFIX) {
links.push(SpecLink::new( links.push(SpecLink::new(get_full_link(&buf[start_idx..]), path.to_owned(), line));
get_full_link(&buf[start_idx..]),
spec,
path.to_owned(),
line,
));
}
} }
buf.clear(); buf.clear();
@ -151,24 +121,22 @@ fn check_whitelist(links: &[SpecLink]) -> Result<()> {
let mut err_nb: u16 = 0; let mut err_nb: u16 = 0;
for link in links { for link in links {
match link.spec { let url_without_prefix = &link.url[URL_PREFIX.len()..];
Spec::Old => {
if !Spec::OLD_URL_WHITELIST.contains(&link.url.as_str()) { if url_without_prefix.starts_with(OLD_VERSION) {
// Only old spec links in the whitelist are allowed.
if !OLD_URL_WHITELIST.contains(&link.url.as_str()) {
err_nb += 1; err_nb += 1;
print_link_err("Old spec link not in whitelist", link); print_link_err("Old spec link not in whitelist", link);
} }
} } else if !NEW_VERSION_WHITELIST
Spec::New => {
if !Spec::NEW_VERSION_WHITELIST
.iter() .iter()
.any(|version| link.url[link.min_len()..].starts_with(version)) .any(|version| url_without_prefix.starts_with(version))
{ {
err_nb += 1; err_nb += 1;
print_link_err("New spec link with wrong version", link); print_link_err("New spec link with wrong version", link);
} }
} }
}
}
if err_nb > 0 { if err_nb > 0 {
// Visual aid to separate the end error message. // Visual aid to separate the end error message.
@ -196,7 +164,7 @@ fn check_targets(links: &[SpecLink]) -> Result<()> {
// Don't allow links to the latest spec with duplicate IDs, they might point // Don't allow links to the latest spec with duplicate IDs, they might point
// to another part of the spec in a new version. // to another part of the spec in a new version.
if *has_duplicates == HasDuplicates::Yes if *has_duplicates == HasDuplicates::Yes
&& link.url[link.min_len()..].starts_with("latest/") && link.url[URL_PREFIX.len()..].starts_with("latest/")
{ {
err_nb += 1; err_nb += 1;
print_link_err("Spec link to latest version with non-unique ID", link); print_link_err("Spec link to latest version with non-unique ID", link);