alpm_lint/
error.rs

1use std::path::PathBuf;
2
3use crate::LintScope;
4
5/// Errors that can occur when using alpm-lint.
6#[derive(Debug, thiserror::Error)]
7pub enum Error {
8    /// I/O error.
9    #[error("I/O error while {context}:\n{source}")]
10    Io {
11        /// The context in which the error occurred.
12        ///
13        /// This is meant to complete the sentence "I/O error while ...".
14        context: &'static str,
15        /// The error source.
16        source: std::io::Error,
17    },
18
19    /// I/O error with additional path info for more context.
20    #[error("I/O error at path {path:?} while {context}:\n{source}")]
21    IoPath {
22        /// The path at which the error occurred.
23        path: PathBuf,
24        /// The context in which the error occurred.
25        ///
26        /// This is meant to complete the sentence "I/O error at path $path while ...".
27        context: &'static str,
28        /// The error source
29        source: std::io::Error,
30    },
31
32    /// No lint scope could be automatically detected for a path.
33    #[error(
34        "The lint scope could not be detected for path '{path}'. Please set it manually via --scope."
35    )]
36    NoLintScope {
37        /// The path for which no lint scope could be determined.
38        path: PathBuf,
39    },
40
41    /// The wrong type of path was provided for a lint scope.
42    ///
43    /// # Example
44    ///
45    /// The [`LintScope::Package`] scope expects a directory. If a file is provided, this error
46    /// will be thrown.
47    #[error("Invalid path type for lint scope '{scope}' at {path:?}. Expected a {expected}.")]
48    InvalidPathForLintScope {
49        /// The path that is invalid.
50        path: PathBuf,
51        /// The set [`LintScope`]
52        scope: LintScope,
53        /// The type of path we expected to find.
54        expected: &'static str,
55    },
56
57    /// An incompatible lint scope has been provided to a function.
58    #[error(
59        "Incompatible lint scope '{scope}' provided to function '{function}'. Expected a {expected}."
60    )]
61    InvalidLintScope {
62        /// The [`LintScope`] that is encountered.
63        scope: LintScope,
64        /// The function context for this error.
65        ///
66        /// Used to complete the sentence:
67        /// `Incompatible lint scope '{scope}' provided to function '{function}'`.
68        function: &'static str,
69        /// Something that is expected instead.
70        ///
71        /// Used to complete the sentence `Expected a {expected}`.
72        expected: &'static str,
73    },
74
75    /// An incompatible resource of specific scope is provided to a lint rule.
76    #[error(
77        "Invalid resources of scope '{scope}' provided to lint rule '{lint_rule}'. Expected a {expected}."
78    )]
79    InvalidResources {
80        /// The [`LintScope`] that was encountered.
81        scope: LintScope,
82        /// The lint rule that produces the error.
83        ///
84        /// Use to complete the sentence:
85        /// `Invalid resources of scope '{scope}' provided to lint rule '{lint_rule}'`.```
86        lint_rule: String,
87        /// The expected [`LintScope`] for the `lint_rule`.
88        ///
89        /// Used to complete the sentence `Expected a {expected}`.
90        expected: LintScope,
91    },
92
93    /// JSON serialization error.
94    #[error("JSON serialization error for {context}: {error}")]
95    Json {
96        /// The error source
97        error: serde_json::Error,
98        /// The context in which the error occurred.
99        ///
100        /// This is meant to complete the sentence "JSON serialization error for ...".
101        context: String,
102    },
103
104    /// `alpm-buildinfo` error.
105    #[error(transparent)]
106    BuildInfo(#[from] alpm_buildinfo::Error),
107
108    /// `alpm-pkgbuild` error.
109    #[error(transparent)]
110    PackageBuild(#[from] alpm_pkgbuild::Error),
111
112    /// `alpm-pkginfo` error.
113    #[error(transparent)]
114    PackageInfo(#[from] alpm_pkginfo::Error),
115
116    /// `alpm-srcinfo` error.
117    #[error(transparent)]
118    SourceInfo(#[from] alpm_srcinfo::Error),
119
120    /// `alpm-lint-config` error.
121    #[error(transparent)]
122    LintConfig(#[from] alpm_lint_config::Error),
123}