nuts_archive/error.rs
1// MIT License
2//
3// Copyright (c) 2023,2024 Robin Doer
4//
5// Permission is hereby granted, free of charge, to any person obtaining a copy
6// of this software and associated documentation files (the "Software"), to
7// deal in the Software without restriction, including without limitation the
8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9// sell copies of the Software, and to permit persons to whom the Software is
10// furnished to do so, subject to the following conditions:
11//
12// The above copyright notice and this permission notice shall be included in
13// all copies or substantial portions of the Software.
14//
15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21// IN THE SOFTWARE.
22
23use nuts_backend::Backend;
24use thiserror::Error;
25
26use crate::header::HeaderMagicError;
27
28/// Error type of this library.
29#[derive(Debug, Error)]
30pub enum Error<B: Backend> {
31 /// Error while (de-) serializing binary data.
32 #[error(transparent)]
33 Bytes(nuts_bytes::Error),
34
35 /// An error occured in a container operation.
36 #[error(transparent)]
37 Container(#[from] nuts_container::Error<B>),
38
39 /// The revision of the archive is not supported anymore. You can open the
40 /// archive till the given version.
41 #[error("unsupported revision: {0}, latest supported version is {1}")]
42 UnsupportedRevision(u16, String),
43
44 /// The service requires a top-id but the container does not provide it.
45 #[error("no top-id available")]
46 NoTopId,
47
48 /// Could not parse the header of the archive.
49 #[error("could not parse the header of the archive")]
50 InvalidHeader(nuts_bytes::Error),
51
52 /// Cannot aquire another block, the archive is full.
53 #[error("the archive is full")]
54 Full,
55
56 /// The block size of the underlaying
57 /// [container](nuts_container::Container) is too small.
58 #[error("the block size is too small")]
59 InvalidBlockSize,
60
61 /// Tries to read an archive node, which is invalid.
62 #[error("not an archive node: {0}")]
63 InvalidNode(B::Id),
64
65 /// An error returned by
66 /// [`FileEntry::read_all()`](crate::FileEntry::read_all) when the
67 /// operation could not be completed because an “end of file” was reached
68 /// prematurely.
69 #[error("could not fill the whole buffer")]
70 UnexpectedEof,
71
72 /// Could not decode the type of an [`Entry`](crate::Entry) stored at the
73 /// give block.
74 #[error("could not detect the type of the entry {}", if let Some(id) = .0 { format!("stored in {}", id) } else { "in unknown block".to_string() })]
75 InvalidType(Option<B::Id>),
76}
77
78impl<B: Backend> From<nuts_bytes::Error> for Error<B> {
79 fn from(cause: nuts_bytes::Error) -> Self {
80 match &cause {
81 nuts_bytes::Error::Custom(cust) => {
82 if cust.is::<HeaderMagicError>() {
83 Error::InvalidHeader(cause)
84 } else {
85 Error::Bytes(cause)
86 }
87 }
88 nuts_bytes::Error::TakeBytes(nuts_bytes::TakeBytesError::Eof)
89 | nuts_bytes::Error::PutBytes(nuts_bytes::PutBytesError::NoSpace) => {
90 Error::InvalidBlockSize
91 }
92 _ => Error::Bytes(cause),
93 }
94 }
95}
96
97pub type ArchiveResult<T, B> = Result<T, Error<B>>;