From ebeb9dd594acad7dd550a7bbeccb3c9870b1a304 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Thu, 8 Aug 2024 19:22:36 -0400 Subject: [PATCH] Clean up Refresh API --- crates/uv-cache/src/lib.rs | 36 +++++++++++++++ .../uv-configuration/src/package_options.rs | 44 +++++++++---------- crates/uv/src/lib.rs | 20 ++++----- 3 files changed, 66 insertions(+), 34 deletions(-) diff --git a/crates/uv-cache/src/lib.rs b/crates/uv-cache/src/lib.rs index f34035b9a293..87fb5293bb02 100644 --- a/crates/uv-cache/src/lib.rs +++ b/crates/uv-cache/src/lib.rs @@ -1038,4 +1038,40 @@ impl Refresh { pub fn is_none(&self) -> bool { matches!(self, Self::None(_)) } + + /// Combine two [`Refresh`] policies, taking the "max" of the two policies. + pub fn combine(self, other: Refresh) -> Self { + /// Return the maximum of two timestamps. + fn max(a: Timestamp, b: Timestamp) -> Timestamp { + if a > b { + a + } else { + b + } + } + + match (self, other) { + // If the policy is `None`, return the existing refresh policy. + // Take the `max` of the two timestamps. + (Self::None(t1), Refresh::None(t2)) => Refresh::None(max(t1, t2)), + (Self::None(t1), Refresh::All(t2)) => Refresh::All(max(t1, t2)), + (Self::None(t1), Refresh::Packages(packages, t2)) => { + Refresh::Packages(packages, max(t1, t2)) + } + + // If the policy is `All`, refresh all packages. + (Self::All(t1), Refresh::None(t2)) => Refresh::All(max(t1, t2)), + (Self::All(t1), Refresh::All(t2)) => Refresh::All(max(t1, t2)), + (Self::All(t1), Refresh::Packages(_packages, t2)) => Refresh::All(max(t1, t2)), + + // If the policy is `Packages`, take the "max" of the two policies. + (Self::Packages(packages, t1), Refresh::None(t2)) => { + Refresh::Packages(packages, max(t1, t2)) + } + (Self::Packages(_packages, t1), Refresh::All(t2)) => Refresh::All(max(t1, t2)), + (Self::Packages(packages1, t1), Refresh::Packages(packages2, t2)) => { + Refresh::Packages(packages1.into_iter().chain(packages2).collect(), max(t1, t2)) + } + } + } } diff --git a/crates/uv-configuration/src/package_options.rs b/crates/uv-configuration/src/package_options.rs index 6f39dc5a43a9..9e042138164b 100644 --- a/crates/uv-configuration/src/package_options.rs +++ b/crates/uv-configuration/src/package_options.rs @@ -3,7 +3,7 @@ use pep508_rs::PackageName; use pypi_types::Requirement; use rustc_hash::FxHashMap; -use uv_cache::Refresh; +use uv_cache::{Refresh, Timestamp}; /// Whether to reinstall packages. #[derive(Debug, Default, Clone)] @@ -44,30 +44,15 @@ impl Reinstall { pub fn is_all(&self) -> bool { matches!(self, Self::All) } +} - /// Create a [`Refresh`] policy by integrating the [`Reinstall`] policy. - pub fn to_refresh(self, refresh: Refresh) -> Refresh { - match (self, refresh) { - // If the policy is `None`, return the existing refresh policy. - (Self::None, Refresh::None(timestamp)) => Refresh::None(timestamp), - (Self::None, Refresh::All(timestamp)) => Refresh::All(timestamp), - (Self::None, Refresh::Packages(packages, timestamp)) => { - Refresh::Packages(packages, timestamp) - } - - // If the policy is `All`, refresh all packages. - (Self::All, Refresh::None(timestamp)) => Refresh::All(timestamp), - (Self::All, Refresh::All(timestamp)) => Refresh::All(timestamp), - (Self::All, Refresh::Packages(_packages, timestamp)) => Refresh::All(timestamp), - - // If the policy is `Packages`, take the "max" of the two policies. - (Self::Packages(packages), Refresh::None(timestamp)) => { - Refresh::Packages(packages, timestamp) - } - (Self::Packages(_packages), Refresh::All(timestamp)) => Refresh::All(timestamp), - (Self::Packages(packages1), Refresh::Packages(packages2, timestamp)) => { - Refresh::Packages(packages1.into_iter().chain(packages2).collect(), timestamp) - } +/// Create a [`Refresh`] policy by integrating the [`Reinstall`] policy. +impl From for Refresh { + fn from(value: Reinstall) -> Self { + match value { + Reinstall::None => Self::None(Timestamp::now()), + Reinstall::All => Self::All(Timestamp::now()), + Reinstall::Packages(packages) => Self::Packages(packages, Timestamp::now()), } } } @@ -144,3 +129,14 @@ impl Upgrade { } } } + +/// Create a [`Refresh`] policy by integrating the [`Upgrade`] policy. +impl From for Refresh { + fn from(value: Upgrade) -> Self { + match value { + Upgrade::None => Self::None(Timestamp::now()), + Upgrade::All => Self::All(Timestamp::now()), + Upgrade::Packages(packages) => Self::Packages(packages.into_keys().collect::>(), Timestamp::now()), + } + } +} diff --git a/crates/uv/src/lib.rs b/crates/uv/src/lib.rs index e5f863cf9735..a1c343afd9f6 100644 --- a/crates/uv/src/lib.rs +++ b/crates/uv/src/lib.rs @@ -12,7 +12,7 @@ use owo_colors::OwoColorize; use tracing::{debug, instrument}; use settings::PipTreeSettings; -use uv_cache::Cache; +use uv_cache::{Cache, Refresh}; use uv_cli::{ compat::CompatArgs, CacheCommand, CacheNamespace, Cli, Commands, PipCommand, PipNamespace, ProjectCommand, @@ -226,7 +226,7 @@ async fn run(cli: Cli) -> Result { // Initialize the cache. let cache = cache .init()? - .with_refresh(args.settings.reinstall.clone().to_refresh(args.refresh)); + .with_refresh(Refresh::from(args.settings.reinstall.clone()).combine(args.refresh)); let requirements = args .src_file @@ -319,7 +319,7 @@ async fn run(cli: Cli) -> Result { // Initialize the cache. let cache = cache .init()? - .with_refresh(args.settings.reinstall.clone().to_refresh(args.refresh)); + .with_refresh(Refresh::from(args.settings.reinstall.clone()).combine(args.refresh)); let requirements = args .src_file @@ -391,7 +391,7 @@ async fn run(cli: Cli) -> Result { // Initialize the cache. let cache = cache .init()? - .with_refresh(args.settings.reinstall.clone().to_refresh(args.refresh)); + .with_refresh(Refresh::from(args.settings.reinstall.clone()).combine(args.refresh)); let requirements = args .package @@ -707,7 +707,7 @@ async fn run(cli: Cli) -> Result { // Initialize the cache. let cache = cache .init()? - .with_refresh(args.settings.reinstall.clone().to_refresh(args.refresh)); + .with_refresh(Refresh::from(args.settings.reinstall.clone()).combine(args.refresh)); let requirements = args .with @@ -750,7 +750,7 @@ async fn run(cli: Cli) -> Result { // Initialize the cache. let cache = cache .init()? - .with_refresh(args.settings.reinstall.clone().to_refresh(args.refresh)); + .with_refresh(Refresh::from(args.settings.reinstall.clone()).combine(args.refresh)); let requirements = args .with @@ -998,7 +998,7 @@ async fn run_project( // Initialize the cache. let cache = cache .init()? - .with_refresh(args.settings.reinstall.clone().to_refresh(args.refresh)); + .with_refresh(Refresh::from(args.settings.reinstall.clone()).combine(args.refresh)); let requirements = args .with @@ -1043,7 +1043,7 @@ async fn run_project( // Initialize the cache. let cache = cache .init()? - .with_refresh(args.settings.reinstall.clone().to_refresh(args.refresh)); + .with_refresh(Refresh::from(args.settings.reinstall.clone()).combine(args.refresh)); commands::sync( args.locked, @@ -1097,7 +1097,7 @@ async fn run_project( // Initialize the cache. let cache = cache .init()? - .with_refresh(args.settings.reinstall.clone().to_refresh(args.refresh)); + .with_refresh(Refresh::from(args.settings.reinstall.clone()).combine(args.refresh)); commands::add( args.locked, @@ -1133,7 +1133,7 @@ async fn run_project( // Initialize the cache. let cache = cache .init()? - .with_refresh(args.settings.reinstall.clone().to_refresh(args.refresh)); + .with_refresh(Refresh::from(args.settings.reinstall.clone()).combine(args.refresh)); commands::remove( args.locked,