Skip to main content

vivacity_core/
error.rs

1use std::path::{Path, PathBuf};
2
3#[derive(Debug, thiserror::Error)]
4pub enum Error {
5    #[error("failed to read {path}: {source}")]
6    ReadFile {
7        path: PathBuf,
8        #[source]
9        source: std::io::Error,
10    },
11
12    #[error("I/O error at {path}: {source}")]
13    Io {
14        path: PathBuf,
15        #[source]
16        source: std::io::Error,
17    },
18
19    #[error("invalid JSON in {context}: {source}")]
20    Json {
21        context: String,
22        #[source]
23        source: serde_json::Error,
24    },
25
26    #[error("invalid zip archive for {dest}: {source}")]
27    Zip {
28        dest: PathBuf,
29        #[source]
30        source: zip::result::ZipError,
31    },
32
33    #[error("hostile archive refused for {dest}: {reason}")]
34    HostileArchive { dest: PathBuf, reason: String },
35
36    #[error("HTTP failure for {url}: {message}")]
37    Http { url: String, message: String },
38
39    #[error("dist checksum mismatch for {name}: expected sha1 {expected}, got {actual}")]
40    ShasumMismatch {
41        name: String,
42        expected: String,
43        actual: String,
44    },
45
46    #[error("cannot encode non-finite float ({0}) as JSON (PHP json_encode would fail too)")]
47    NonFiniteFloat(f64),
48
49    /// Plugin emulation that cannot be reproduced byte for byte, detected
50    /// before any change to vendor/: the CLI delegates to Composer.
51    #[error("{0}")]
52    Unsupported(String),
53}
54
55impl Error {
56    pub fn io(path: &Path) -> impl FnOnce(std::io::Error) -> Error + '_ {
57        move |source| Error::Io {
58            path: path.to_path_buf(),
59            source,
60        }
61    }
62
63    pub fn zip(dest: &Path) -> impl FnOnce(zip::result::ZipError) -> Error + '_ {
64        move |source| Error::Zip {
65            dest: dest.to_path_buf(),
66            source,
67        }
68    }
69}
70
71pub type Result<T> = std::result::Result<T, Error>;