Skip to main content

cuenv_vcs/
error.rs

1//! Errors produced by the [`VcsHasher`](crate::VcsHasher) implementations.
2
3use miette::Diagnostic;
4use std::path::Path;
5use thiserror::Error;
6
7/// Errors produced while resolving or hashing input files.
8#[derive(Error, Debug, Diagnostic)]
9pub enum Error {
10    /// An I/O operation failed.
11    #[error("I/O {operation} failed{}", path.as_ref().map_or(String::new(), |p| format!(": {}", p.display())))]
12    #[diagnostic(
13        code(cuenv::vcs::io),
14        help("Check that the path exists and that cuenv has permission to read it")
15    )]
16    Io {
17        /// Underlying I/O error.
18        #[source]
19        source: std::io::Error,
20        /// Path that triggered the failure, if known.
21        path: Option<Box<Path>>,
22        /// Operation being attempted (e.g. `read`, `open`).
23        operation: String,
24    },
25
26    /// A supplied glob/pattern was invalid.
27    #[error("invalid input pattern: {message}")]
28    #[diagnostic(
29        code(cuenv::vcs::pattern),
30        help("Patterns are globs rooted at the workspace (e.g. `src/**/*.rs`)")
31    )]
32    Pattern {
33        /// Human-readable explanation.
34        message: String,
35    },
36}
37
38impl Error {
39    /// Build an I/O error tagged with a path and operation.
40    #[must_use]
41    pub fn io(
42        source: std::io::Error,
43        path: impl AsRef<Path>,
44        operation: impl Into<String>,
45    ) -> Self {
46        Self::Io {
47            source,
48            path: Some(path.as_ref().into()),
49            operation: operation.into(),
50        }
51    }
52
53    /// Build a pattern error.
54    #[must_use]
55    pub fn pattern(message: impl Into<String>) -> Self {
56        Self::Pattern {
57            message: message.into(),
58        }
59    }
60}
61
62/// Convenience alias.
63pub type Result<T> = std::result::Result<T, Error>;