use std::collections::BTreeSet;
use std::num::NonZeroUsize;
use std::path::PathBuf;
use crate::config::Config;
use crate::lock::Lock;
use crate::projection::diagnostic::ProjectionWarning;
use crate::sync::state::ArtifactRecord;
use super::ConflictResolver;
use super::hooks::HookOutcome;
use super::model::{ChangeSet, ConflictKind, LocalChange, ReconciliationPolicy, RemovalReason};
use super::progress::{self, ProgressSink};
#[derive(Debug, Clone, Default)]
pub struct LockSet {
pub base: Option<Lock>,
pub local: Option<Lock>,
}
#[derive(Debug, Clone)]
pub struct TrustRequest<'a> {
pub dep_instance: &'a str,
pub hook_id: &'a str,
pub command: String,
}
pub trait TrustPrompt: Sync {
fn confirm(&self, request: &TrustRequest<'_>) -> bool;
}
pub struct DeclineAll;
impl TrustPrompt for DeclineAll {
fn confirm(&self, _request: &TrustRequest<'_>) -> bool {
false
}
}
pub struct SyncRequest<'a> {
pub base_config: &'a Config,
pub local_config: Option<&'a Config>,
pub locks: LockSet,
pub options: SyncOptions,
pub resolver: Option<&'a dyn ConflictResolver>,
pub sink: &'a dyn ProgressSink,
pub trust_prompt: Option<&'a dyn TrustPrompt>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SyncOptions {
pub source_policy: SourcePolicy,
pub conflict_policy: ConflictPolicy,
pub prune_policy: PrunePolicy,
pub hook_policy: HookPolicy,
pub concurrency: Concurrency,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SourcePolicy {
Reuse,
Refresh,
Locked,
Frozen,
}
impl SourcePolicy {
#[must_use]
pub fn refreshes(self) -> bool {
matches!(self, Self::Refresh)
}
#[must_use]
pub fn requires_pins(self) -> bool {
matches!(self, Self::Locked | Self::Frozen)
}
#[must_use]
pub fn offline(self) -> bool {
matches!(self, Self::Frozen)
}
pub(crate) fn unpinned(self, subject: &str) -> crate::error::Error {
debug_assert!(
self.requires_pins(),
"only a pin-requiring policy reports a missing pin"
);
let flag = match self {
Self::Frozen => "--frozen",
Self::Locked | Self::Reuse | Self::Refresh => "--locked",
};
crate::error::Error::Lock(format!(
"{subject} is not pinned in the lock; {flag} requires every source to be pinned"
))
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ConflictPolicy {
Refuse,
ResolveInteractively,
Overwrite,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PrunePolicy {
KeepOrphans,
RemoveOrphans,
RemoveOrphansOf(PruneScope),
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct PruneScope {
pub sources: BTreeSet<String>,
pub targets: BTreeSet<String>,
}
impl PruneScope {
#[must_use]
pub fn covers(&self, record: &ArtifactRecord) -> bool {
self.sources.contains(record.source_name()) || self.targets.contains(&record.key.target)
}
}
impl PrunePolicy {
#[must_use]
pub fn prunes(&self) -> bool {
!matches!(self, Self::KeepOrphans)
}
#[must_use]
pub fn removes(&self, record: &ArtifactRecord) -> bool {
match self {
Self::KeepOrphans => false,
Self::RemoveOrphans => true,
Self::RemoveOrphansOf(scope) => scope.covers(record),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum HookPolicy {
All,
NoTransitive,
None,
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct Concurrency {
pub jobs: Option<NonZeroUsize>,
}
impl From<&SyncOptions> for ReconciliationPolicy {
fn from(options: &SyncOptions) -> Self {
Self {
force: matches!(options.conflict_policy, ConflictPolicy::Overwrite),
prune: options.prune_policy.prunes(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AppliedChange {
Deployed {
target: String,
source: String,
artifact: String,
},
Overwritten {
target: String,
source: String,
artifact: String,
},
OverlayRewritten {
target: String,
source: String,
artifact: String,
},
Ejected {
target: String,
source: String,
artifact: String,
},
Removed {
target: String,
source: String,
artifact: String,
reason: RemovalReason,
},
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SkippedChange {
Conflict {
target: String,
source: String,
artifact: String,
kind: ConflictKind,
},
Failed {
target: String,
source: String,
artifact: String,
message: String,
},
ReadonlyOverlayRewrite {
target: String,
source: String,
artifact: String,
},
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SyncWarning {
Projection(ProjectionWarning),
MalformedTransitiveHooks {
target: String,
detail: String,
},
LinkPathNotPortable {
source: String,
path: PathBuf,
},
UnboundSource {
source: String,
},
ReferenceMoved {
source: String,
target: String,
from: String,
to: String,
},
OrphanedRecords {
count: usize,
},
RemovalsSkippedAfterFailures,
PruneRefused {
path: PathBuf,
reason: String,
},
PruneKeptDirectory {
path: PathBuf,
},
OrphanRecordPathUnknown {
source: String,
artifact: String,
layout: String,
},
ReleasedUnderLive {
source: String,
artifact: String,
path: PathBuf,
reason: RemovalReason,
},
KeptLocalChange {
source: String,
artifact: String,
path: PathBuf,
change: LocalChange,
reason: RemovalReason,
},
CrossDeviceFallback {
destination: PathBuf,
},
ConflictModified {
source: String,
artifact: String,
changed: Vec<PathBuf>,
},
ConflictForeign {
path: PathBuf,
},
UntrustedTransitiveHooks {
count: usize,
},
HistoryContentFilter {
source: String,
attributes: bool,
autocrlf: bool,
},
BuildFailed {
source: String,
detail: String,
},
ContentStoreRepaired {
object: PathBuf,
},
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum SyncStatus {
#[default]
Success,
Failed,
}
#[derive(Debug)]
pub struct SyncReport {
pub locks: LockSet,
pub changes: ChangeSet,
pub applied: Vec<AppliedChange>,
pub skipped: Vec<SkippedChange>,
pub warnings: Vec<SyncWarning>,
pub hook_outcomes: Vec<HookOutcome>,
pub status: SyncStatus,
}
pub(super) struct SyncEvents<'a> {
sink: &'a dyn ProgressSink,
pub(super) applied: Vec<AppliedChange>,
pub(super) skipped: Vec<SkippedChange>,
pub(super) warnings: Vec<SyncWarning>,
pub(super) unchanged: usize,
}
impl std::fmt::Debug for SyncEvents<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("SyncEvents")
.field("applied", &self.applied)
.field("skipped", &self.skipped)
.field("warnings", &self.warnings)
.finish()
}
}
impl<'a> SyncEvents<'a> {
pub(super) fn new(sink: &'a dyn ProgressSink) -> Self {
Self {
sink,
applied: Vec::new(),
skipped: Vec::new(),
warnings: Vec::new(),
unchanged: 0,
}
}
pub(super) fn discarding() -> SyncEvents<'static> {
SyncEvents::new(progress::SILENT)
}
pub(super) fn sink(&self) -> &'a dyn ProgressSink {
self.sink
}
pub(super) fn push_applied(&mut self, change: AppliedChange) {
self.sink.artifact_applied(&change);
self.applied.push(change);
}
pub(super) fn push_skipped(&mut self, change: SkippedChange) {
self.sink.artifact_skipped(&change);
self.skipped.push(change);
}
pub(super) fn push_warning(&mut self, warning: SyncWarning) {
self.sink.warning(&warning);
self.warnings.push(warning);
}
pub(super) fn push_unchanged(&mut self, artifact: &progress::ArtifactId) {
self.sink.artifact_unchanged(artifact);
self.unchanged += 1;
}
}