1use miette::Diagnostic;
4use std::path::Path;
5use thiserror::Error;
6
7#[derive(Error, Debug, Diagnostic)]
9pub enum Error {
10 #[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 #[source]
19 source: std::io::Error,
20 path: Option<Box<Path>>,
22 operation: String,
24 },
25
26 #[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 message: String,
35 },
36}
37
38impl Error {
39 #[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 #[must_use]
55 pub fn pattern(message: impl Into<String>) -> Self {
56 Self::Pattern {
57 message: message.into(),
58 }
59 }
60}
61
62pub type Result<T> = std::result::Result<T, Error>;