uuencoding_multi/error.rs
1/// Errors produced by multi-part UUencoding reassembly.
2///
3/// This enum is `#[non_exhaustive]`; new variants may be added in future
4/// releases without a breaking change.
5///
6/// # Example
7///
8/// ```
9/// use uuencoding_multi::MultiUuError;
10///
11/// let err = MultiUuError::EmptyCollection;
12/// assert!(err.to_string().contains("empty"));
13/// ```
14#[derive(Debug)]
15#[non_exhaustive]
16pub enum MultiUuError {
17 /// One or more part numbers in `1..=expected` are absent from the
18 /// collection. `present` lists the part numbers that *were* provided, in
19 /// ascending order.
20 MissingParts { expected: u32, present: Vec<u32> },
21
22 /// [`reassemble()`][crate::reassemble()] was called on a [`PartCollection`][crate::PartCollection]
23 /// that contains no parts with `part_number >= 1`. A collection that holds
24 /// only a TOC part (`part_number = 0`) triggers this error.
25 EmptyCollection,
26
27 /// A UUdecode error was returned by the `uuencoding` crate while decoding
28 /// one of the part bodies. Reassembly stops at the first failing part.
29 DecodeError(uuencoding::UuError),
30
31 /// Two calls to [`PartCollection::add`][crate::PartCollection::add] provided
32 /// entries with the same `part_number`. The duplicate entry is rejected and
33 /// the collection is left unchanged.
34 DuplicatePart { part_number: u32 },
35}
36
37impl std::fmt::Display for MultiUuError {
38 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
39 match self {
40 MultiUuError::MissingParts { expected, present } => write!(
41 f,
42 "missing parts: expected {} total, but only have parts {:?}",
43 expected, present
44 ),
45 MultiUuError::EmptyCollection => {
46 write!(f, "reassemble() called on empty PartCollection")
47 }
48 MultiUuError::DecodeError(e) => write!(f, "UUdecode error: {}", e),
49 MultiUuError::DuplicatePart { part_number } => {
50 write!(f, "duplicate part number: {}", part_number)
51 }
52 }
53 }
54}
55
56impl std::error::Error for MultiUuError {}
57
58impl From<uuencoding::UuError> for MultiUuError {
59 fn from(e: uuencoding::UuError) -> Self {
60 Self::DecodeError(e)
61 }
62}