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

Code before:
    /// # 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;
        }
        if bound.minor.map_or(false, |minor| self.minor != minor) {
            return false;
        }
        if bound.patch.map_or(false, |patch| self.patch != patch) {
            return false;
        }
        true
    }

    /// 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.
    ///
    /// ```
    /// # 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_gt(&SemVerBound::from_str("0").unwrap()));
    /// assert!(!v1_1_1.matches_gt(&SemVerBound::from_str("1").unwrap()));
    /// assert!(!v1_1_1.matches_gt(&SemVerBound::from_str("2").unwrap()));
    ///
    /// assert!(v1_1_1.matches_gt(&SemVerBound::from_str("1.0").unwrap()));
    /// assert!(!v1_1_1.matches_gt(&SemVerBound::from_str("1.1").unwrap()));
    /// assert!(!v1_1_1.matches_gt(&SemVerBound::from_str("1.2").unwrap()));
    ///
    /// assert!(v1_1_1.matches_gt(&SemVerBound::from_str("1.1.0").unwrap()));
    /// assert!(!v1_1_1.matches_gt(&SemVerBound::from_str("1.1.1").unwrap()));
    /// assert!(!v1_1_1.matches_gt(&SemVerBound::from_str("1.1.2").unwrap()));
    /// ```
    pub fn matches_gt(&self, bound: &SemVerBound) -> bool {
        if self.major != bound.major {
            return self.major > bound.major;

Review comment (suggestion):
```suggestion
        self.major == bound.major
            && bound.minor.map_or(true, |minor| self.minor == minor)
            && bound.patch.map_or(true, |patch| self.patch == patch)
```

Code after:
    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 {
        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 versions. The match succeeds when
    /// either a `==` or `>` match does.
    pub fn matches_ge(&self, bound: &VersionBound) -> bool {
        self.matches_eq(bound) || self.matches_gt(bound)
    }

    /// Performs a `<=` match with the given versions. The match succeeds when
    /// either a `==` or `<` match does.
    pub fn matches_le(&self, bound: &VersionBound) -> bool {
        self.matches_eq(bound) || self.matches_lt(bound)
    }
}

impl FromStr for PackageVersion {
    type Err = EcoString;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let mut parts = s.split('.');