Refactored Refresh::combine (#15609)

This commit is contained in:
adamnemecek 2025-08-31 10:18:58 -07:00 committed by GitHub
parent 22f80ca00d
commit 36216363eb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 9 additions and 14 deletions

View File

@ -1217,35 +1217,30 @@ impl Refresh {
/// Combine two [`Refresh`] policies, taking the "max" of the two policies.
#[must_use]
pub fn combine(self, other: Self) -> 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), Self::None(t2)) => Self::None(max(t1, t2)),
(Self::None(t1), Self::All(t2)) => Self::All(max(t1, t2)),
(Self::None(t1), Self::None(t2)) => Self::None(t1.max(t2)),
(Self::None(t1), Self::All(t2)) => Self::All(t1.max(t2)),
(Self::None(t1), Self::Packages(packages, paths, t2)) => {
Self::Packages(packages, paths, max(t1, t2))
Self::Packages(packages, paths, t1.max(t2))
}
// If the policy is `All`, refresh all packages.
(Self::All(t1), Self::None(t2)) => Self::All(max(t1, t2)),
(Self::All(t1), Self::All(t2)) => Self::All(max(t1, t2)),
(Self::All(t1), Self::Packages(.., t2)) => Self::All(max(t1, t2)),
(Self::All(t1), Self::None(t2) | Self::All(t2) | Self::Packages(.., t2)) => {
Self::All(t1.max(t2))
}
// If the policy is `Packages`, take the "max" of the two policies.
(Self::Packages(packages, paths, t1), Self::None(t2)) => {
Self::Packages(packages, paths, max(t1, t2))
Self::Packages(packages, paths, t1.max(t2))
}
(Self::Packages(.., t1), Self::All(t2)) => Self::All(max(t1, t2)),
(Self::Packages(.., t1), Self::All(t2)) => Self::All(t1.max(t2)),
(Self::Packages(packages1, paths1, t1), Self::Packages(packages2, paths2, t2)) => {
Self::Packages(
packages1.into_iter().chain(packages2).collect(),
paths1.into_iter().chain(paths2).collect(),
max(t1, t2),
t1.max(t2),
)
}
}