Skip to main content

openszigno_author/
error.rs

1//! The stable machine-readable categories this crate refuses a build with.
2//!
3//! Every code here names something about the caller's request, never
4//! something about a dossier that was read: this crate reads nothing.
5
6use serde::Serialize;
7use thiserror::Error;
8
9/// Stable machine-readable categories returned by the author library.
10#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize)]
11#[serde(rename_all = "snake_case")]
12pub enum ErrorCode {
13    NoDocuments,
14    TooManyDocuments,
15    DecodedTooLarge,
16    TotalSizeLimit,
17    ZipRatioLimit,
18    ZipFailed,
19    UnsafeDocumentTitle,
20    InvalidDossierTitle,
21    InvalidMimeType,
22    UnknownMimeType,
23    NoRecipients,
24    InvalidRecipientCertificate,
25    UnsupportedRecipientKey,
26    EncryptFailed,
27}
28
29impl ErrorCode {
30    pub const fn as_str(self) -> &'static str {
31        match self {
32            Self::NoDocuments => "no_documents",
33            Self::TooManyDocuments => "too_many_documents",
34            Self::DecodedTooLarge => "decoded_too_large",
35            Self::TotalSizeLimit => "total_size_limit",
36            Self::ZipRatioLimit => "zip_ratio_limit",
37            Self::ZipFailed => "zip_failed",
38            Self::UnsafeDocumentTitle => "unsafe_document_title",
39            Self::InvalidDossierTitle => "invalid_dossier_title",
40            Self::InvalidMimeType => "invalid_mime_type",
41            Self::UnknownMimeType => "unknown_mime_type",
42            Self::NoRecipients => "no_recipients",
43            Self::InvalidRecipientCertificate => "invalid_recipient_certificate",
44            Self::UnsupportedRecipientKey => "unsupported_recipient_key",
45            Self::EncryptFailed => "encrypt_failed",
46        }
47    }
48}
49
50/// One refused build. The message names a document by index only: a title, a
51/// path, and payload content never appear in it.
52#[derive(Debug, Error)]
53#[error("{message}")]
54pub struct Error {
55    code: ErrorCode,
56    message: String,
57}
58
59impl Error {
60    pub(crate) fn new(code: ErrorCode, message: impl Into<String>) -> Self {
61        Self {
62            code,
63            message: message.into(),
64        }
65    }
66
67    pub const fn code(&self) -> ErrorCode {
68        self.code
69    }
70
71    pub fn message(&self) -> &str {
72        &self.message
73    }
74}
75
76#[cfg(test)]
77mod tests {
78    use super::*;
79
80    #[test]
81    fn every_code_has_a_snake_case_string() {
82        for code in [
83            ErrorCode::NoDocuments,
84            ErrorCode::TooManyDocuments,
85            ErrorCode::DecodedTooLarge,
86            ErrorCode::TotalSizeLimit,
87            ErrorCode::ZipRatioLimit,
88            ErrorCode::ZipFailed,
89            ErrorCode::UnsafeDocumentTitle,
90            ErrorCode::InvalidDossierTitle,
91            ErrorCode::InvalidMimeType,
92            ErrorCode::UnknownMimeType,
93            ErrorCode::NoRecipients,
94            ErrorCode::InvalidRecipientCertificate,
95            ErrorCode::UnsupportedRecipientKey,
96            ErrorCode::EncryptFailed,
97        ] {
98            let text = code.as_str();
99            assert!(!text.is_empty());
100            assert!(
101                text.chars()
102                    .all(|character| character.is_ascii_lowercase() || character == '_')
103            );
104        }
105    }
106
107    #[test]
108    fn an_error_reports_its_code_and_message() {
109        let error = Error::new(ErrorCode::NoDocuments, "a dossier needs one document");
110        assert_eq!(error.code(), ErrorCode::NoDocuments);
111        assert_eq!(error.message(), "a dossier needs one document");
112        assert_eq!(error.to_string(), "a dossier needs one document");
113    }
114}