alpm_types/
error.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
use std::path::PathBuf;

/// The library's error type
///
/// These errors are usually parsing errors and they each contain a context
/// about why the error has occurred and the value that caused the error.
///
/// The original error is also included in the variants that have the `source` field.
/// You can access it using the `source()` method.
/// See [Error::source](https://doc.rust-lang.org/std/error/trait.Error.html#method.source) for
/// more information.
#[derive(Debug, thiserror::Error, PartialEq)]
pub enum Error {
    /// An invalid integer
    #[error("Invalid integer (caused by {kind:?})")]
    InvalidInteger { kind: std::num::IntErrorKind },

    /// An invalid enum variant
    #[error("Invalid variant ({0})")]
    InvalidVariant(#[from] strum::ParseError),

    /// An invalid email address
    #[error("Invalid e-mail ({0})")]
    InvalidEmail(#[from] email_address::Error),

    /// An invalid URL
    #[error("Invalid URL ({0})")]
    InvalidUrl(#[from] url::ParseError),

    /// An invalid license
    #[error("Invalid license ({0})")]
    InvalidLicense(#[from] spdx::ParseError),

    /// An invalid semantic version string
    ///
    /// This error occurs when a semantic version cannot be parsed from a string
    /// We cannot use `#[source] semver::Error` here because it does not implement `PartialEq`.
    /// See: <https://github.com/dtolnay/semver/issues/326>
    ///
    /// TODO: Use the error source when the issue above is resolved.
    #[error("Invalid semver ({kind})")]
    InvalidSemver { kind: String },

    /// Value contains invalid characters
    #[error("Value contains invalid characters: {invalid_char:?}")]
    ValueContainsInvalidChars { invalid_char: char },

    /// Value length is incorrect
    #[error("Incorrect length, got {length} expected {expected}")]
    IncorrectLength { length: usize, expected: usize },

    /// Value is missing a delimiter character
    #[error("Value is missing the required delimiter: {delimiter}")]
    DelimiterNotFound { delimiter: char },

    /// Value does not match the restrictions
    #[error("Does not match the restrictions ({restrictions:?})")]
    ValueDoesNotMatchRestrictions { restrictions: Vec<String> },

    /// A validation regex does not match the value
    #[error("Value '{value}' does not match the '{regex_type}' regex: {regex}")]
    RegexDoesNotMatch {
        value: String,
        regex_type: String,
        regex: String,
    },

    /// Missing field in a value
    #[error("Missing component: {component}")]
    MissingComponent { component: &'static str },

    /// An invalid absolute path (i.e. does not start with a `/`)
    #[error("The path is not absolute: {0}")]
    PathNotAbsolute(PathBuf),

    /// An invalid relative path (i.e. starts with a `/`)
    #[error("The path is not relative: {0}")]
    PathNotRelative(PathBuf),

    /// File name contains invalid characters
    #[error("File name ({0}) contains invalid characters: {1:?}")]
    FileNameContainsInvalidChars(PathBuf, char),

    /// File name is empty
    #[error("File name is empty")]
    FileNameIsEmpty,

    /// A deprecated license
    #[error("Deprecated license: {0}")]
    DeprecatedLicense(String),

    /// An invalid OpenPGP v4 fingerprint
    #[error(
        "Invalid OpenPGP v4 fingerprint, only 40 uppercase hexadecimal characters are allowed"
    )]
    InvalidOpenPGPv4Fingerprint,
}

#[cfg(test)]
mod tests {
    use std::num::IntErrorKind;

    use rstest::rstest;

    use super::*;
    use crate::{name::NAME_REGEX, openpgp::PACKAGER_REGEX};

    #[rstest]
    #[case(
        "Invalid integer (caused by InvalidDigit)",
        Error::InvalidInteger {
            kind: IntErrorKind::InvalidDigit
        }
    )]
    #[case(
        "Invalid integer (caused by InvalidDigit)",
        Error::InvalidInteger {
            kind: IntErrorKind::InvalidDigit
        }
    )]
    #[case(
        "Invalid integer (caused by PosOverflow)",
        Error::InvalidInteger {
            kind: IntErrorKind::PosOverflow
        }
    )]
    #[case(
        "Value '€i²' does not match the 'pkgname' regex: ^[a-z\\d_@+]+[a-z\\d\\-._@+]*$",
        Error::RegexDoesNotMatch {
            value: "€i²".to_string(),
            regex_type: "pkgname".to_string(),
            regex: NAME_REGEX.to_string(),
        }
    )]
    #[allow(deprecated)]
    #[case(
        "Invalid integer (caused by InvalidDigit)",
        Error::InvalidInteger {
            kind: IntErrorKind::InvalidDigit
        }
    )]
    #[case(
        "Value '€i²' does not match the 'packager' regex: ^(?P<name>[\\w\\s\\-().]+) <(?P<email>.*)>$",
        Error::RegexDoesNotMatch {
            value: "€i²".to_string(),
            regex_type: "packager".to_string(),
            regex: PACKAGER_REGEX.to_string(),
        }
    )]
    #[case(
        "Invalid e-mail (Missing separator character '@'.)",
        email_address::Error::MissingSeparator.into()
    )]
    #[case(
        "Invalid integer (caused by InvalidDigit)",
        Error::InvalidInteger {
            kind: IntErrorKind::InvalidDigit
        }
    )]
    fn error_format_string(#[case] error_str: &str, #[case] error: Error) {
        assert_eq!(error_str, format!("{}", error));
    }
}