Repository: typst/typst
File: crates/typst-syntax/src/package.rs (Rust)

Code before:
/// A package's version.
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct PackageVersion {
    /// The package's major version.
    pub major: u32,
    /// The package's minor version.
    pub minor: u32,
    /// The package's patch version.
    pub patch: u32,
}

impl PackageVersion {
    /// The current compiler version.
    pub fn compiler() -> Self {
        Self {
            major: env!("CARGO_PKG_VERSION_MAJOR").parse().unwrap(),
            minor: env!("CARGO_PKG_VERSION_MINOR").parse().unwrap(),
            patch: env!("CARGO_PKG_VERSION_PATCH").parse().unwrap(),
        }
    }
}

impl PackageVersion {
    // the code in this impl block is based on https://github.com/dtolnay/semver/blob/1.0.23/src/eval.rs
    // author: David Tolnay <https://github.com/dtolnay>
    // licensed under Apache 2.0

    /// Performs an `==` match with the given version bound. Version elements missing in the bound
    /// are ignored.
    ///
    /// ```
    /// # use typst_syntax::package::{PackageVersion, SemVerBound};
    /// # use std::str::FromStr;
    /// let v1_1_1 = PackageVersion::from_str("1.1.1").unwrap();
    ///
    /// assert!(!v1_1_1.matches_eq(&SemVerBound::from_str("0").unwrap()));
    /// assert!(v1_1_1.matches_eq(&SemVerBound::from_str("1").unwrap()));
    /// assert!(!v1_1_1.matches_eq(&SemVerBound::from_str("2").unwrap()));
    ///
    /// assert!(!v1_1_1.matches_eq(&SemVerBound::from_str("1.0").unwrap()));
    /// assert!(v1_1_1.matches_eq(&SemVerBound::from_str("1.1").unwrap()));
    /// assert!(!v1_1_1.matches_eq(&SemVerBound::from_str("1.2").unwrap()));
    ///
    /// assert!(!v1_1_1.matches_eq(&SemVerBound::from_str("1.1.0").unwrap()));
    /// assert!(v1_1_1.matches_eq(&SemVerBound::from_str("1.1.1").unwrap()));
    /// assert!(!v1_1_1.matches_eq(&SemVerBound::from_str("1.1.2").unwrap()));
    /// ```
    pub fn matches_eq(&self, bound: &SemVerBound) -> bool {
        if self.major != bound.major {
            return false;
        }

Review comment (suggestion):
If the code is really close enough to the original, then we'd also need an entry in the NOTICE file. though to me it looks fairly different.

Code after:
/// A package's version.
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct PackageVersion {
    /// The package's major version.
    pub major: u32,
    /// The package's minor version.
    pub minor: u32,
    /// The package's patch version.
    pub patch: u32,
}

impl PackageVersion {
    /// The current compiler version.
    pub fn compiler() -> Self {
        Self {
            major: env!("CARGO_PKG_VERSION_MAJOR").parse().unwrap(),
            minor: env!("CARGO_PKG_VERSION_MINOR").parse().unwrap(),
            patch: env!("CARGO_PKG_VERSION_PATCH").parse().unwrap(),
        }
    }

    /// Performs an `==` match with the given version bound. Version elements
    /// missing in the bound are ignored.
    pub fn matches_eq(&self, bound: &VersionBound) -> bool {
        self.major == bound.major
            && bound.minor.map_or(true, |minor| self.minor == minor)
            && bound.patch.map_or(true, |patch| self.patch == patch)
    }

    /// Performs a `>` match with the given version bound. The match only
    /// succeeds if some version element in the bound is actually greater than
    /// that of the version.
    pub fn matches_gt(&self, bound: &VersionBound) -> bool {
        if self.major != bound.major {
            return self.major > bound.major;
        }
        let Some(minor) = bound.minor else { return false };
        if self.minor != minor {
            return self.minor > minor;
        }
        let Some(patch) = bound.patch else { return false };
        if self.patch != patch {
            return self.patch > patch;
        }
        false
    }

    /// Performs a `<` match with the given version bound. The match only
    /// succeeds if some version element in the bound is actually less than that
    /// of the version.
    pub fn matches_lt(&self, bound: &VersionBound) -> bool {