From e727121bc08be004a8bf4e9c29409c4244ce1a8b Mon Sep 17 00:00:00 2001 From: arctic_hen7 Date: Sat, 2 Jul 2022 16:55:25 +1000 Subject: [PATCH] feat(website): added auto-versioning to docs.rs shorthand links --- docs/manifest.json | 15 +++++++---- website/src/templates/docs/generation.rs | 32 ++++++++++++++++-------- 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/docs/manifest.json b/docs/manifest.json index 13d9e9697e..cb7365cea7 100644 --- a/docs/manifest.json +++ b/docs/manifest.json @@ -1,22 +1,27 @@ { "0.1.x": { "state": "outdated", - "git": "v0.1.4" + "git": "v0.1.4", + "docs_rs": "0.1" }, "0.2.x": { "state": "outdated", - "git": "v0.2.3" + "git": "v0.2.3", + "docs_rs": "0.2" }, "0.3.0-0.3.3": { "state": "outdated", - "git": "v0.3.3" + "git": "v0.3.3", + "docs_rs": "0.3.3" }, "0.3.4": { "state": "stable", - "git": "eccf137032fbe8e6507be9e9317edc16e7576a4f" + "git": "eccf137032fbe8e6507be9e9317edc16e7576a4f", + "docs_rs": "0.3" }, "0.4.x": { "state": "beta", - "git": "HEAD" + "git": "HEAD", + "docs_rs": "0.4.0-beta" } } diff --git a/website/src/templates/docs/generation.rs b/website/src/templates/docs/generation.rs index 1e8fd7b697..6369665a57 100644 --- a/website/src/templates/docs/generation.rs +++ b/website/src/templates/docs/generation.rs @@ -102,23 +102,21 @@ impl DocsVersionStatus { } /// Information about the current state of the documentation, including which versions are outdated and the like. pub type DocsManifest = HashMap; -// pub struct DocsManifest { -// pub stable: String, -// pub outdated: Vec, -// pub beta: Vec, -// /// A map of versions to points in the Git version history. -// pub history_map: HashMap, -// } /// Information about a single version in the documentation manifest. -#[derive(Serialize, Deserialize, Clone)] +#[derive(Serialize, Deserialize, Clone, PartialEq, PartialOrd)] pub struct VersionManifest { + /// The state of this version. pub state: VersionState, + /// The location in the Git history to get examples from for this version. pub git: String, + /// The version to use on docs.rs for this version. This will be interpolated into all docs.rs links + /// in this version's docs. + pub docs_rs: String, } /// The possible states a version can be in. Note that there can only be one stable version at a time, and that the special `next` /// version is not accounted for here. -#[derive(Serialize, Deserialize, Clone, PartialEq, Eq)] +#[derive(Serialize, Deserialize, Clone, PartialEq, Eq, PartialOrd)] #[serde(rename_all = "snake_case")] pub enum VersionState { /// The version is outdated, and should no longer be used if possible. @@ -344,7 +342,21 @@ pub async fn get_build_state( // Parse any links to docs.rs (of the form `[`Error`](=enum.Error@perseus)`, where `perseus` is the package name) // Versions are interpolated automatically - let docs_rs_version = "latest"; + let docs_rs_version = if version == "next" { + // Unfortunately, `latest` doesn't take account of beta versions, so we use either the latest beta version or the stable version + let mut beta_versions = BETA_VERSIONS.values().collect::>(); + beta_versions.sort_by(|a, b| b.partial_cmp(a).unwrap()); + if beta_versions.is_empty() { + get_stable_version(&DOCS_MANIFEST).1.docs_rs + } else { + beta_versions[0].docs_rs.to_string() + } + } else { + match &DOCS_MANIFEST.get(version) { + Some(version) => version.docs_rs.to_string(), + None => panic!("docs version '{}' not present in manifest", version), + } + }; let contents = Regex::new(r#"\]\(=(?P.*?)@(?P.*?)\)"#) .unwrap() .replace_all(