mirror of
https://github.com/RGBCube/dix
synced 2025-07-28 12:17:45 +00:00
chore: inline Version::diff
This commit is contained in:
parent
07dd524d30
commit
4f0ee04e19
2 changed files with 47 additions and 68 deletions
64
src/diff.rs
64
src/diff.rs
|
@ -1,6 +1,9 @@
|
||||||
use std::fmt::{
|
use std::{
|
||||||
|
fmt::{
|
||||||
self,
|
self,
|
||||||
Write as _,
|
Write as _,
|
||||||
|
},
|
||||||
|
sync,
|
||||||
};
|
};
|
||||||
|
|
||||||
use itertools::EitherOrBoth;
|
use itertools::EitherOrBoth;
|
||||||
|
@ -128,29 +131,50 @@ pub fn diff<'a>(
|
||||||
match diff {
|
match diff {
|
||||||
// I have no idea why itertools is returning `versions.new` in `Left`.
|
// I have no idea why itertools is returning `versions.new` in `Left`.
|
||||||
EitherOrBoth::Left(new) => {
|
EitherOrBoth::Left(new) => {
|
||||||
write!(
|
let new = new.unwrap_or(Version::ref_cast("<none>"));
|
||||||
newacc,
|
|
||||||
" {new}",
|
|
||||||
new = new.unwrap_or(Version::ref_cast("<none>")).green()
|
|
||||||
)?;
|
|
||||||
},
|
|
||||||
|
|
||||||
EitherOrBoth::Both(old, new) => {
|
write!(newacc, " {new}", new = new.green())?;
|
||||||
let (old, new) = Version::diff(
|
|
||||||
old.unwrap_or(Version::ref_cast("<none>")),
|
|
||||||
new.unwrap_or(Version::ref_cast("<none>")),
|
|
||||||
);
|
|
||||||
|
|
||||||
write!(oldacc, " {old}")?;
|
|
||||||
write!(newacc, " {new}")?;
|
|
||||||
},
|
},
|
||||||
|
|
||||||
EitherOrBoth::Right(old) => {
|
EitherOrBoth::Right(old) => {
|
||||||
write!(
|
let old = old.unwrap_or(Version::ref_cast("<none>"));
|
||||||
oldacc,
|
|
||||||
" {old}",
|
write!(oldacc, " {old}", old = old.red())?;
|
||||||
old = old.unwrap_or(Version::ref_cast("<none>")).red()
|
},
|
||||||
)?;
|
|
||||||
|
EitherOrBoth::Both(old, new) => {
|
||||||
|
static NAME_SUFFIX_REGEX: sync::LazyLock<regex::Regex> =
|
||||||
|
sync::LazyLock::new(|| {
|
||||||
|
regex::Regex::new("(-man|-lib|-doc|-dev|-out|-terminfo)")
|
||||||
|
.expect("failed to compile regex for Nix store path versions")
|
||||||
|
});
|
||||||
|
|
||||||
|
let old = old.unwrap_or(Version::ref_cast("<none>"));
|
||||||
|
let new = new.unwrap_or(Version::ref_cast("<none>"));
|
||||||
|
|
||||||
|
let suffix = NAME_SUFFIX_REGEX.captures(old).map_or("", |matches| {
|
||||||
|
matches.get(0).map_or("", |capture| capture.as_str())
|
||||||
|
});
|
||||||
|
|
||||||
|
let old = old.strip_suffix(suffix).unwrap_or(old);
|
||||||
|
let new = new.strip_suffix(suffix).unwrap_or(new);
|
||||||
|
|
||||||
|
for diff in diff::chars(old, new) {
|
||||||
|
match diff {
|
||||||
|
diff::Result::Left(oldc) => {
|
||||||
|
write!(oldacc, "{oldc}", oldc = oldc.red()).unwrap();
|
||||||
|
},
|
||||||
|
|
||||||
|
diff::Result::Right(newc) => {
|
||||||
|
write!(newacc, "{newc}", newc = newc.green()).unwrap();
|
||||||
|
},
|
||||||
|
|
||||||
|
diff::Result::Both(oldc, newc) => {
|
||||||
|
write!(oldacc, "{oldc}", oldc = oldc.yellow()).unwrap();
|
||||||
|
write!(newacc, "{newc}", newc = newc.yellow()).unwrap();
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,4 @@
|
||||||
use std::{
|
use std::cmp;
|
||||||
cmp,
|
|
||||||
fmt::Write as _,
|
|
||||||
sync,
|
|
||||||
};
|
|
||||||
|
|
||||||
use derive_more::{
|
use derive_more::{
|
||||||
Deref,
|
Deref,
|
||||||
|
@ -11,7 +7,6 @@ use derive_more::{
|
||||||
From,
|
From,
|
||||||
};
|
};
|
||||||
use ref_cast::RefCast;
|
use ref_cast::RefCast;
|
||||||
use yansi::Paint as _;
|
|
||||||
|
|
||||||
#[derive(RefCast, Deref, Display, Debug, PartialEq, Eq)]
|
#[derive(RefCast, Deref, Display, Debug, PartialEq, Eq)]
|
||||||
#[repr(transparent)]
|
#[repr(transparent)]
|
||||||
|
@ -32,46 +27,6 @@ impl cmp::Ord for Version {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Version {
|
|
||||||
pub fn diff(old: &Version, new: &Version) -> (String, String) {
|
|
||||||
static NAME_SUFFIX_REGEX: sync::LazyLock<regex::Regex> =
|
|
||||||
sync::LazyLock::new(|| {
|
|
||||||
regex::Regex::new("(-man|-lib|-doc|-dev|-out|-terminfo)")
|
|
||||||
.expect("failed to compile regex for Nix store path versions")
|
|
||||||
});
|
|
||||||
|
|
||||||
let matches = NAME_SUFFIX_REGEX.captures(old);
|
|
||||||
let suffix = matches.map_or("", |matches| {
|
|
||||||
matches.get(0).map_or("", |capture| capture.as_str())
|
|
||||||
});
|
|
||||||
|
|
||||||
let old = old.strip_suffix(suffix).unwrap_or(old);
|
|
||||||
let new = new.strip_suffix(suffix).unwrap_or(new);
|
|
||||||
|
|
||||||
let mut oldacc = String::new();
|
|
||||||
let mut newacc = String::new();
|
|
||||||
|
|
||||||
for diff in diff::chars(old, new) {
|
|
||||||
match diff {
|
|
||||||
diff::Result::Left(oldc) => {
|
|
||||||
write!(oldacc, "{oldc}", oldc = oldc.red()).unwrap();
|
|
||||||
},
|
|
||||||
|
|
||||||
diff::Result::Both(oldc, newc) => {
|
|
||||||
write!(oldacc, "{oldc}", oldc = oldc.yellow()).unwrap();
|
|
||||||
write!(newacc, "{newc}", newc = newc.yellow()).unwrap();
|
|
||||||
},
|
|
||||||
|
|
||||||
diff::Result::Right(newc) => {
|
|
||||||
write!(newacc, "{newc}", newc = newc.green()).unwrap();
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
(oldacc, newacc)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
|
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
|
||||||
enum VersionComponent<'a> {
|
enum VersionComponent<'a> {
|
||||||
Number(u64),
|
Number(u64),
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue