rpm/
errors.rs

1use std::io;
2
3use thiserror::Error;
4
5use crate::{DigestAlgorithm, TimestampError};
6
7#[derive(Error, Debug)]
8#[non_exhaustive]
9pub enum Error {
10    #[error(transparent)]
11    Io(#[from] io::Error),
12
13    #[error(transparent)]
14    Hex(#[from] hex::FromHexError),
15
16    #[error("{0}")]
17    Nom(String),
18    #[error(
19        "invalid magic expected: {expected} but got: {actual} - whole input was {complete_input:?}"
20    )]
21    InvalidMagic {
22        expected: u8,
23        actual: u8,
24        complete_input: Vec<u8>,
25    },
26    #[error("unsupported Version {0} - only header version 1 is supported")]
27    UnsupportedHeaderVersion(u8),
28    #[error("invalid tag data type in store {store_type}: expected 0 - 9 but got {raw_data_type}")]
29    InvalidTagDataType {
30        raw_data_type: u32,
31        store_type: &'static str,
32    },
33    #[error("unable to find tag {0}")]
34    TagNotFound(String),
35    #[error("tag {tag} has data type {actual_data_type}, not {expected_data_type}")]
36    UnexpectedTagDataType {
37        expected_data_type: &'static str,
38        actual_data_type: String,
39        tag: String,
40    },
41    #[error("invalid tag array index {tag} with {index} while bounded at {bound}")]
42    InvalidTagIndex { tag: String, index: u32, bound: u32 },
43
44    #[error("invalid tag value enum variant for {tag} with {variant}")]
45    InvalidTagValueEnumVariant { tag: String, variant: u32 },
46
47    #[error("invalid size of reserved area - expected length of {expected} but got {actual}")]
48    InvalidReservedSpaceSize { expected: u16, actual: usize },
49
50    #[error("invalid destination path {path} - {desc}")]
51    InvalidDestinationPath { path: String, desc: &'static str },
52
53    #[error("signature packet not found in what is supposed to be a signature")]
54    NoSignatureFound,
55
56    #[error("error creating signature: {0}")]
57    SignError(Box<dyn std::error::Error>),
58
59    #[error("error parsing key - {details}. underlying error was: {source}")]
60    KeyLoadError {
61        source: Box<dyn std::error::Error>,
62        details: &'static str,
63    },
64
65    #[error("error verifying signature with key {key_ref}: {source}")]
66    VerificationError {
67        source: Box<dyn std::error::Error>,
68        key_ref: String,
69    },
70
71    #[error("digests from content did not match those in the header")]
72    DigestMismatchError,
73
74    #[error("unable to find key with key-ref: {key_ref}")]
75    KeyNotFoundError { key_ref: String },
76
77    #[error("unknown compressor type {0} - only gzip and none are supported")]
78    UnknownCompressorType(String),
79
80    #[error("unsupported digest algorithm {0:?}")]
81    UnsupportedDigestAlgorithm(DigestAlgorithm),
82
83    #[cfg(feature = "signature-pgp")]
84    #[error("unsupported PGP key type {0:?}")]
85    UnsupportedPGPKeyType(pgp::crypto::public_key::PublicKeyAlgorithm),
86
87    #[error("invalid file mode {raw_mode} - {reason}")]
88    InvalidFileMode { raw_mode: i32, reason: &'static str },
89
90    #[error("timestamp conversion error: {0:?}")]
91    TimestampConv(TimestampError),
92}
93
94impl From<nom::Err<(&[u8], nom::error::ErrorKind)>> for Error {
95    fn from(error: nom::Err<(&[u8], nom::error::ErrorKind)>) -> Self {
96        match error {
97            nom::Err::Error((_, kind)) | nom::Err::Failure((_, kind)) => {
98                Error::Nom(kind.description().to_string())
99            }
100            nom::Err::Incomplete(_) => Error::Nom("unhandled incomplete".to_string()),
101        }
102    }
103}
104
105impl From<TimestampError> for Error {
106    fn from(error: TimestampError) -> Self {
107        Error::TimestampConv(error)
108    }
109}