-
Notifications
You must be signed in to change notification settings - Fork 6
Automatically generate rustdocs links to references in markdown #74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
b927bd3
8fa958a
db17cb7
f2f1f3c
2c403fb
7c3592c
f7c3eb0
ef9e7f6
8fa15e7
da6c724
0fc763d
cb5d292
355a37e
1029ebd
5d021aa
3d0d9f2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,7 @@ | ||
| pub mod md_pages; | ||
| mod utils; | ||
| pub use utils::{Version, VersionError}; | ||
|
|
||
| pub const MASTER_VERSION: &str = "master"; | ||
| pub const LATEST_VERSION: &str = "v0.5"; | ||
| pub const ALL_VERSIONS: &[&str] = &[MASTER_VERSION, "v0.5", "v0.4", "v0.3", "v0.2", "v0.1"]; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| use std::fmt::Display; | ||
| use std::str::FromStr; | ||
|
|
||
| use semver::Version as SemverVersion; | ||
| use thiserror::Error; | ||
|
|
||
| use crate::{LATEST_VERSION, MASTER_VERSION}; | ||
|
|
||
| /// Errors related to version parsing and handling. | ||
| #[derive(Debug, Error)] | ||
| pub enum VersionError { | ||
| /// Error parsing version string. | ||
| #[error("invalid version string: {0}")] | ||
| InvalidVersion(#[from] semver::Error), | ||
| } | ||
|
|
||
| /// A semver version type. | ||
| #[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)] | ||
| pub struct Version(SemverVersion); | ||
|
|
||
| impl Version { | ||
| /// Creates a new version from major, minor, and patch numbers. | ||
| /// | ||
| /// # Example | ||
| /// ``` | ||
| /// let v = Version::new(0, 5, 0); | ||
| /// assert_eq!(v.to_string(), "0.5.0"); | ||
| /// ``` | ||
| pub fn new(major: u64, minor: u64, patch: u64) -> Self { | ||
| Version(SemverVersion::new(major, minor, patch)) | ||
| } | ||
|
|
||
| /// Returns the major version number. | ||
| /// | ||
| /// # Example | ||
| /// ``` | ||
| /// let v = Version::new(0, 5, 0); | ||
| /// assert_eq!(v.major(), 0); | ||
| /// ``` | ||
| pub fn major(&self) -> u64 { | ||
| self.0.major | ||
| } | ||
|
|
||
| /// Returns the minor version number. | ||
| /// | ||
| /// # Example | ||
| /// ``` | ||
| /// let v = Version::new(0, 5, 0); | ||
| /// assert_eq!(v.minor(), 5); | ||
| /// ``` | ||
| pub fn minor(&self) -> u64 { | ||
| self.0.minor | ||
| } | ||
|
|
||
| /// Returns the patch version number. | ||
| /// | ||
| /// # Example | ||
| /// ``` | ||
| /// let v = Version::new(0, 5, 0); | ||
| /// assert_eq!(v.patch(), 0); | ||
| /// ``` | ||
| pub fn patch(&self) -> u64 { | ||
| self.0.patch | ||
| } | ||
| } | ||
|
|
||
| impl Display for Version { | ||
| fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
| write!(f, "{}", self.0.to_string()) | ||
| } | ||
| } | ||
|
|
||
| impl FromStr for Version { | ||
| type Err = VersionError; | ||
|
|
||
| fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
| // replace "master" with latest version | ||
| let s = if s == MASTER_VERSION || s == "" { | ||
| LATEST_VERSION | ||
| } else { | ||
| s | ||
| }; | ||
|
|
||
| // canonicalize version string by adding ".0" for missing minor/patch | ||
| let s = canonicalize_version_string(s); | ||
|
|
||
| let semver_version = SemverVersion::parse(s.as_str())?; | ||
| Ok(Version(semver_version)) | ||
| } | ||
| } | ||
|
|
||
| fn canonicalize_version_string(s: &str) -> String { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not exactly sure if this is the way we want to handle this. Suppose a user sees 0.5 version of the framework, but the latest version is 0.5.31. The URL generated will point specifically to 0.5.0, possibly omitting the changes made in the patch versions. docs.rs is capable of redirecting to the latest patch version by itself, maybe we should use that instead? For instance, see that https://docs.rs/cot/0.3/cot/ redirects to 0.3.1, not 0.3.0. I'm not even sure we need to parse the version. The content changes rarely and it's easy to spot the version might be wrong. Therefore, I think keeping it as
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I probably must have missed something initially while playing around the rustdocs link structure using the semver versioning which didnt work initially and influenced this function. But, you're right. I tried this again and it works. Which means we dont need this at all
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, I just remembered another reason for this function. The |
||
| let s = s.trim_start_matches('v'); | ||
| let parts: Vec<&str> = s.split('.').collect(); | ||
| match parts.len() { | ||
| 1 => format!("{}.0.0", parts[0]), | ||
| 2 => format!("{}.{}.0", parts[0], parts[1]), | ||
| _ => s.to_string(), | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Won't
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The minimum number of parts that Semver crate supports is 3. This is only a best effort to canonicalize any format less than 3 to have the standard major, minor, and patch. For cases > than 3, the idea is to route them to the |
||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| #[test] | ||
| fn test_version_parsing() { | ||
| let v = Version::from_str("v0.5").unwrap(); | ||
| assert_eq!(v.to_string(), "0.5.0"); | ||
|
|
||
| let v = Version::from_str("0.5").unwrap(); | ||
| assert_eq!(v.to_string(), "0.5.0"); | ||
|
|
||
| let v = Version::from_str("master").unwrap(); | ||
| assert_eq!(v.to_string(), canonicalize_version_string(LATEST_VERSION)); | ||
|
|
||
| let v = Version::from_str("").unwrap(); | ||
| assert_eq!(v.to_string(), canonicalize_version_string(LATEST_VERSION)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_canonicalize_version_string() { | ||
| assert_eq!(canonicalize_version_string("v0.5"), "0.5.0"); | ||
| assert_eq!(canonicalize_version_string("0.5"), "0.5.0"); | ||
| assert_eq!(canonicalize_version_string("v0.5.1"), "0.5.1"); | ||
| assert_eq!(canonicalize_version_string("0.5.1"), "0.5.1"); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we use these methods anywhere? If not, there's no point in keeping dead code on the repo.