Skip to main content

openszigno_core/
error.rs

1use serde::Serialize;
2use thiserror::Error;
3
4/// Stable machine-readable categories returned by the core library.
5#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize)]
6#[serde(rename_all = "snake_case")]
7pub enum ErrorCode {
8    InputTooLarge,
9    UnsupportedEncoding,
10    InvalidEncoding,
11    UnsafeXml,
12    InvalidXml,
13    WrongRoot,
14    MissingElement,
15    InvalidAttribute,
16    DuplicateId,
17    UnresolvedObjref,
18    TooManyDocuments,
19    InvalidBase64,
20    DecodedTooLarge,
21    SourceSizeMismatch,
22    InvalidZip,
23    ZipMemberLimit,
24    ZipSizeLimit,
25    ZipRatioLimit,
26    UnsafeZipMember,
27    UnsupportedZipMember,
28    InvalidDecryptionKey,
29    InvalidDecryptionCertificate,
30    DecryptionCertificateRequired,
31    DecryptionKeyMismatch,
32    InvalidCms,
33    DecryptFailed,
34}
35
36impl ErrorCode {
37    pub const fn as_str(self) -> &'static str {
38        match self {
39            Self::InputTooLarge => "input_too_large",
40            Self::UnsupportedEncoding => "unsupported_encoding",
41            Self::InvalidEncoding => "invalid_encoding",
42            Self::UnsafeXml => "unsafe_xml",
43            Self::InvalidXml => "invalid_xml",
44            Self::WrongRoot => "wrong_root",
45            Self::MissingElement => "missing_element",
46            Self::InvalidAttribute => "invalid_attribute",
47            Self::DuplicateId => "duplicate_id",
48            Self::UnresolvedObjref => "unresolved_objref",
49            Self::TooManyDocuments => "too_many_documents",
50            Self::InvalidBase64 => "invalid_base64",
51            Self::DecodedTooLarge => "decoded_too_large",
52            Self::SourceSizeMismatch => "source_size_mismatch",
53            Self::InvalidZip => "invalid_zip",
54            Self::ZipMemberLimit => "zip_member_limit",
55            Self::ZipSizeLimit => "zip_size_limit",
56            Self::ZipRatioLimit => "zip_ratio_limit",
57            Self::UnsafeZipMember => "unsafe_zip_member",
58            Self::UnsupportedZipMember => "unsupported_zip_member",
59            Self::InvalidDecryptionKey => "invalid_decryption_key",
60            Self::InvalidDecryptionCertificate => "invalid_decryption_certificate",
61            Self::DecryptionCertificateRequired => "decryption_certificate_required",
62            Self::DecryptionKeyMismatch => "decryption_key_mismatch",
63            Self::InvalidCms => "invalid_cms",
64            Self::DecryptFailed => "decrypt_failed",
65        }
66    }
67}
68
69#[derive(Debug, Error)]
70#[error("{message}")]
71pub struct Error {
72    code: ErrorCode,
73    message: String,
74}
75
76impl Error {
77    pub(crate) fn new(code: ErrorCode, message: impl Into<String>) -> Self {
78        Self {
79            code,
80            message: message.into(),
81        }
82    }
83
84    pub const fn code(&self) -> ErrorCode {
85        self.code
86    }
87
88    pub fn message(&self) -> &str {
89        &self.message
90    }
91}