Skip to main content

ripsync_core/
error.rs

1//! Error types for `ripsync-core`. Library code returns [`Result`]; it never
2//! panics on bad input.
3
4use std::path::PathBuf;
5
6/// Convenience alias for results produced by `ripsync-core`.
7pub type Result<T> = std::result::Result<T, Error>;
8
9/// Errors that can arise while planning or applying a sync.
10#[derive(Debug, thiserror::Error)]
11pub enum Error {
12    /// The run was cancelled cooperatively.
13    #[error("run cancelled")]
14    Cancelled,
15
16    /// An underlying I/O failure, annotated with the offending path.
17    #[error("I/O error at {path}: {source}")]
18    Io {
19        /// Path that triggered the error.
20        path: PathBuf,
21        /// The underlying I/O error.
22        source: std::io::Error,
23    },
24
25    /// A bare I/O failure with no associated path.
26    #[error(transparent)]
27    BareIo(#[from] std::io::Error),
28
29    /// A path escaped the destination root (containment violation).
30    #[error("path escapes destination root: {0}")]
31    Containment(PathBuf),
32
33    /// `--delete` was requested but the source is empty or unreadable.
34    #[error("refusing to mirror deletions: source is empty or unreadable ({0})")]
35    EmptySource(PathBuf),
36
37    /// An exclude/glob pattern failed to compile.
38    #[error("invalid exclude pattern: {0}")]
39    Pattern(String),
40
41    /// An include/exclude/files-from filter was invalid.
42    #[error("filter error: {0}")]
43    Filter(String),
44
45    /// A delta could not be applied to the supplied basis.
46    #[error("delta apply failed: {0}")]
47    DeltaApply(String),
48
49    /// The remote-sync wire protocol was violated (bad frame, version mismatch,
50    /// unexpected message, oversized frame).
51    #[error("protocol error: {0}")]
52    Protocol(String),
53
54    /// Verification found one or more mismatches.
55    #[error("verification failed with {0} mismatch(es)")]
56    Verification(usize),
57}
58
59impl Error {
60    /// Wrap an [`std::io::Error`] together with the path that produced it.
61    #[must_use]
62    pub fn io(path: impl Into<PathBuf>, source: std::io::Error) -> Self {
63        Error::Io {
64            path: path.into(),
65            source,
66        }
67    }
68}