From 4f0ee04e19e5687b8dbb7a5fb680a991aef6cd53 Mon Sep 17 00:00:00 2001 From: RGBCube Date: Fri, 9 May 2025 15:37:08 +0300 Subject: [PATCH] chore: inline Version::diff --- src/diff.rs | 68 ++++++++++++++++++++++++++++++++++---------------- src/version.rs | 47 +--------------------------------- 2 files changed, 47 insertions(+), 68 deletions(-) diff --git a/src/diff.rs b/src/diff.rs index 4dc75de..6132322 100644 --- a/src/diff.rs +++ b/src/diff.rs @@ -1,6 +1,9 @@ -use std::fmt::{ - self, - Write as _, +use std::{ + fmt::{ + self, + Write as _, + }, + sync, }; use itertools::EitherOrBoth; @@ -128,29 +131,50 @@ pub fn diff<'a>( match diff { // I have no idea why itertools is returning `versions.new` in `Left`. EitherOrBoth::Left(new) => { - write!( - newacc, - " {new}", - new = new.unwrap_or(Version::ref_cast("")).green() - )?; - }, + let new = new.unwrap_or(Version::ref_cast("")); - EitherOrBoth::Both(old, new) => { - let (old, new) = Version::diff( - old.unwrap_or(Version::ref_cast("")), - new.unwrap_or(Version::ref_cast("")), - ); - - write!(oldacc, " {old}")?; - write!(newacc, " {new}")?; + write!(newacc, " {new}", new = new.green())?; }, EitherOrBoth::Right(old) => { - write!( - oldacc, - " {old}", - old = old.unwrap_or(Version::ref_cast("")).red() - )?; + let old = old.unwrap_or(Version::ref_cast("")); + + write!(oldacc, " {old}", old = old.red())?; + }, + + EitherOrBoth::Both(old, new) => { + static NAME_SUFFIX_REGEX: sync::LazyLock = + 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("")); + let new = new.unwrap_or(Version::ref_cast("")); + + 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(); + }, + } + } }, } } diff --git a/src/version.rs b/src/version.rs index 86cc4c9..8a40027 100644 --- a/src/version.rs +++ b/src/version.rs @@ -1,8 +1,4 @@ -use std::{ - cmp, - fmt::Write as _, - sync, -}; +use std::cmp; use derive_more::{ Deref, @@ -11,7 +7,6 @@ use derive_more::{ From, }; use ref_cast::RefCast; -use yansi::Paint as _; #[derive(RefCast, Deref, Display, Debug, PartialEq, Eq)] #[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 = - 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)] enum VersionComponent<'a> { Number(u64),