Skip to main content

falcon_library/
error.rs

1//! Error type for library I/O.
2
3use std::path::PathBuf;
4
5/// Errors raised while reading or writing a library.
6#[derive(Debug, thiserror::Error)]
7pub enum Error {
8    /// A filesystem operation failed.
9    #[error("I/O error at {path}: {source}")]
10    Io {
11        /// Path the operation was acting on.
12        path: PathBuf,
13        /// Underlying error.
14        #[source]
15        source: std::io::Error,
16    },
17    /// A JSON file could not be parsed or produced.
18    #[error("JSON error in {path}: {source}")]
19    Json {
20        /// File that failed.
21        path: PathBuf,
22        /// Underlying error.
23        #[source]
24        source: serde_json::Error,
25    },
26    /// The folder does not look like an Eagle library.
27    #[error("{0} is not an Eagle library (missing metadata.json or images/)")]
28    NotALibrary(PathBuf),
29    /// The caller won't use a library at this place; the text says why
30    /// (the app refuses one inside its own install folder, as Eagle does).
31    #[error("{0}")]
32    Refused(String),
33    /// `save_to` was pointed at the library's own folder, which would copy
34    /// every file onto itself.
35    #[error("cannot save {0} onto itself")]
36    SaveOntoSelf(PathBuf),
37    /// The file cannot be imported (empty, missing, unknown folder, ...).
38    #[error("cannot import {path}: {reason}")]
39    Import {
40        /// The file being imported.
41        path: PathBuf,
42        /// Why it was refused.
43        reason: String,
44    },
45    /// The library was written by a newer Eagle than the one falcon
46    /// matches ([`crate::library::APP_VERSION`]); Eagle refuses to open it
47    /// and asks for an update.
48    #[error("{path} was made by Eagle {version}, newer than the Eagle {supported} format falcon reads; update falcon to open it", supported = crate::library::APP_VERSION)]
49    NewerLibrary {
50        /// The library folder.
51        path: PathBuf,
52        /// Its `applicationVersion`.
53        version: String,
54    },
55    /// `metadata.json` on disk is newer than the copy in memory: another
56    /// app or a sync tool changed it since falcon read it. The write was
57    /// refused, as Eagle refuses it; reload the library.
58    #[error("{0} was changed by another app or device since falcon read it; reload the library")]
59    SyncConflict(PathBuf),
60    /// An `.eaglepack` could not be written or read.
61    #[error("eaglepack {path}: {reason}")]
62    Pack {
63        /// The pack file.
64        path: PathBuf,
65        /// What went wrong.
66        reason: String,
67    },
68}
69
70/// Convenience alias.
71pub type Result<T> = std::result::Result<T, Error>;
72
73impl Error {
74    pub(crate) fn io(path: impl Into<PathBuf>, source: std::io::Error) -> Self {
75        Error::Io { path: path.into(), source }
76    }
77    pub(crate) fn json(path: impl Into<PathBuf>, source: serde_json::Error) -> Self {
78        Error::Json { path: path.into(), source }
79    }
80}