ccsync_core/sync/
actions.rs

1//! Sync action determination logic
2
3use std::path::PathBuf;
4
5use crate::comparison::{ComparisonResult, ConflictStrategy};
6
7/// Sync action to perform
8#[derive(Debug, Clone, PartialEq, Eq)]
9pub enum SyncAction {
10    /// Create new file at destination
11    Create {
12        /// Source file path
13        source: PathBuf,
14        /// Destination file path
15        dest: PathBuf,
16    },
17    /// Create new directory at destination
18    CreateDirectory {
19        /// Source directory path
20        source: PathBuf,
21        /// Destination directory path
22        dest: PathBuf,
23    },
24    /// Skip this file (no action needed)
25    Skip {
26        /// File path being skipped
27        path: PathBuf,
28        /// Reason for skipping
29        reason: String,
30    },
31    /// Conflict requiring resolution
32    Conflict {
33        /// Source file path
34        source: PathBuf,
35        /// Destination file path
36        dest: PathBuf,
37        /// Conflict resolution strategy
38        strategy: ConflictStrategy,
39        /// Whether source is newer than destination
40        source_newer: bool,
41    },
42    /// Directory conflict requiring resolution
43    DirectoryConflict {
44        /// Source directory path
45        source: PathBuf,
46        /// Destination directory path
47        dest: PathBuf,
48        /// Conflict resolution strategy
49        strategy: ConflictStrategy,
50        /// Whether source is newer than destination
51        source_newer: bool,
52    },
53}
54
55/// Resolves comparison results into sync actions
56pub struct SyncActionResolver;
57
58impl SyncActionResolver {
59    /// Determine sync action from comparison result
60    #[must_use]
61    pub fn resolve(source: PathBuf, dest: PathBuf, comparison: &ComparisonResult) -> SyncAction {
62        match comparison {
63            ComparisonResult::Identical => SyncAction::Skip {
64                path: source,
65                reason: "identical content".to_string(),
66            },
67            ComparisonResult::SourceOnly => SyncAction::Create { source, dest },
68            ComparisonResult::DestinationOnly => SyncAction::Skip {
69                path: dest,
70                reason: "source doesn't exist".to_string(),
71            },
72            ComparisonResult::Conflict {
73                source_newer,
74                strategy,
75            } => SyncAction::Conflict {
76                source,
77                dest,
78                strategy: *strategy,
79                source_newer: *source_newer,
80            },
81        }
82    }
83}