Skip to content

Conform Semver and Range to fmt::Display #10

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

Merged
merged 8 commits into from
Jan 5, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ jobs:
~/.cargo/registry/cache
target
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('Cargo.lock') }}
- name: Test
run: cargo test --all-features
coverage:
name: Coverage
runs-on: ubuntu-latest
Expand Down
44 changes: 43 additions & 1 deletion lib/src/range/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use crate::semver::Semver;
use std::hash::{Hash, Hasher};
use std::{
fmt,
hash::{Hash, Hasher},
};

pub mod intersect;
pub mod max;
Expand Down Expand Up @@ -43,3 +46,42 @@ impl Hash for Semver {
self.components.hash(state);
}
}

impl fmt::Display for Range {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let str = self
.set
.iter()
.map(|v| match v {
Constraint::Any => "*".to_string(),
Constraint::Single(v) => format!("={}", v),
Constraint::Contiguous(v1, v2) => {
if v2.major == v1.major + 1 && v2.minor == 0 && v2.patch == 0 {
let v = v1.chomp();
if v1.major == 0 {
if v1.components.len() == 1 {
"^0".to_string()
} else {
format!(">={}<1", v)
}
} else {
format!("^{}", v)
}
} else if v2.major == v1.major && v2.minor == v1.minor + 1 && v2.patch == 0 {
let v = v1.chomp();
format!("~{}", v)
} else if v2.major == usize::MAX {
let v = v1.chomp();
format!(">={}", v)
} else if v1.at(&v2.clone()) {
format!("@{}", v1)
} else {
format!(">={}<{}", v1.chomp(), v2.chomp())
}
}
})
.collect::<Vec<_>>()
.join(",");
write!(f, "{}", str)
}
}
32 changes: 32 additions & 0 deletions lib/src/semver/compare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,38 @@ impl Semver {

Ordering::Equal
}

/// checks @ syntax, eg. [email protected]
pub fn at(&self, at: &Semver) -> bool {
let mut cc1 = self.components.clone();
let cc2 = &at.components;

// helper function to get the last element of a slice
fn last(arr: &[usize]) -> usize {
*arr.last().unwrap()
}

if cc1.len() > cc2.len() {
return false;
}

// Ensure cc1 and cc2 have the same length by appending 0s to cc1
while cc1.len() < cc2.len() {
cc1.push(0);
}

if last(&cc1) + 1 != last(cc2) {
return false;
}

for i in 0..cc1.len() - 1 {
if cc1[i] != cc2[i] {
return false;
}
}

true
}
}

impl PartialEq for Semver {
Expand Down
40 changes: 40 additions & 0 deletions lib/src/semver/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::fmt;

pub mod bump;
pub mod compare;
pub mod parse;
pub mod utils;

#[derive(Default, Debug, Clone, Eq)]
pub struct Semver {
Expand Down Expand Up @@ -28,3 +31,40 @@ impl Semver {
}
}
}

impl fmt::Display for Semver {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"{}",
self.components
.iter()
.map(|c| c.to_string())
.collect::<Vec<_>>()
.join(".")
)?;
if !self.prerelease.is_empty() {
write!(
f,
"-{}",
self.prerelease
.iter()
.map(|c| c.to_string())
.collect::<Vec<_>>()
.join(".")
)?;
}
if !self.build.is_empty() {
write!(
f,
"+{}",
self.build
.iter()
.map(|c| c.to_string())
.collect::<Vec<_>>()
.join(".")
)?;
}
Ok(())
}
}
12 changes: 12 additions & 0 deletions lib/src/semver/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use super::Semver;

impl Semver {
pub fn chomp(&self) -> String {
let result = self.raw.trim_end_matches(".0");
if result.is_empty() {
"0".to_string()
} else {
result.to_string()
}
}
}
34 changes: 34 additions & 0 deletions lib/src/tests/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,37 @@ fn test_intersect() -> Result<()> {

Ok(())
}

#[test]
fn test_at() -> Result<()> {
let sa = Semver::parse("1")?;
let sb = Semver::parse("1.1")?;
let sc = Semver::parse("1.1.1")?;
let sd = Semver::parse("1.1.1.1")?;

assert!(!sa.at(&sb));
assert!(sb.at(&sa));
assert!(!sb.at(&sc));
assert!(sc.at(&sb));
assert!(!sc.at(&sd));
assert!(sd.at(&sc));
assert!(!sa.at(&sd));
assert!(sd.at(&sa));

Ok(())
}

#[test]
fn test_display() -> Result<()> {
let ra = Range::parse("^3.7")?;
let rb = Range::parse("=3.11")?;
let rc = Range::parse("^3.9")?;
let rd = Range::parse("*")?;

assert_eq!(ra.to_string(), "^3.7");
assert_eq!(rb.to_string(), "=3.11");
assert_eq!(rc.to_string(), "^3.9");
assert_eq!(rd.to_string(), "*");

Ok(())
}
21 changes: 21 additions & 0 deletions lib/src/tests/semver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,24 @@ fn test_infinty() {
assert_eq!(inf.components, [usize::MAX, usize::MAX, usize::MAX]);
assert_eq!(inf.raw, "Infinity.Infinity.Infinity");
}

#[test]
fn test_display() -> Result<()> {
let a = Semver::parse("1.2.3")?;
let b = Semver::parse("1.2.3-alpha")?;
let c = Semver::parse("1.2.0")?;
let d = Semver::parse("1.0.0")?;
let e = Semver::parse("1.2.3-alpha.1+b40")?;
let f = Semver::parse("1.2.3-alpha.1+build.40")?;
let g = Semver::parse("1")?;

assert_eq!(a.to_string(), "1.2.3");
assert_eq!(b.to_string(), "1.2.3-alpha");
assert_eq!(c.to_string(), "1.2.0");
assert_eq!(d.to_string(), "1.0.0");
assert_eq!(e.to_string(), "1.2.3-alpha.1+b40");
assert_eq!(f.to_string(), "1.2.3-alpha.1+build.40");
assert_eq!(g.to_string(), "1");

Ok(())
}
Loading