1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
// MIT License
//
// Copyright (c) 2023 Robin Doer
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
use nuts_container::backend::Backend;
use nuts_container::container;
use std::{error, fmt};
/// Error type of this library.
pub enum Error<B: Backend> {
/// Error while (de-) serializing binary data.
Bytes(nuts_bytes::Error),
/// An error occured in a container operation.
Container(container::Error<B>),
/// Tried to overwrite an existing
/// [userdata record](container::Container::userdata).
OverwriteUserdata,
/// The [userdata record](container::Container::userdata) of the container
/// does not refer to an archive:
///
/// * The userdata record is empty, the container has no attached service.
/// * Another service is attached to the container.
InvalidUserdata(Option<nuts_bytes::Error>),
/// Could not parse the header of the archive.
InvalidHeader(nuts_bytes::Error),
/// Cannot aquire another block, the archive is full.
Full,
/// The block size of the underlaying [container](container::Container) is
/// too small.
InvalidBlockSize,
/// An error returned by
/// [`FileEntry::read_all()`](crate::FileEntry::read_all) when the
/// operation could not be completed because an “end of file” was reached
/// prematurely.
UnexpectedEof,
/// Could not decode the type of an [`Entry`](crate::Entry).
InvalidType,
}
impl<B: Backend> fmt::Display for Error<B> {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::Bytes(cause) => fmt::Display::fmt(cause, fmt),
Self::Container(cause) => fmt::Display::fmt(cause, fmt),
Self::OverwriteUserdata => write!(fmt, "the container is not empty"),
Self::InvalidUserdata(None) => write!(fmt, "the container is empty"),
Self::InvalidUserdata(Some(_)) => {
write!(fmt, "the container does not have an archive")
}
Self::InvalidHeader(_) => write!(fmt, "could not parse the header of the archive"),
Self::Full => write!(fmt, "the archive is full"),
Self::InvalidBlockSize => write!(fmt, "the block size is too small"),
Self::UnexpectedEof => write!(fmt, "could not fill the whole buffer"),
Self::InvalidType => write!(fmt, "could not detect the type of the entry"),
}
}
}
impl<B: Backend> fmt::Debug for Error<B> {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Bytes(cause) => fmt.debug_tuple("Bytes").field(cause).finish(),
Self::Container(cause) => fmt.debug_tuple("Container").field(cause).finish(),
Self::OverwriteUserdata => fmt.debug_tuple("OverwriteUserdata").finish(),
Self::InvalidUserdata(cause) => {
fmt.debug_tuple("InvalidUserdata").field(cause).finish()
}
Self::InvalidHeader(cause) => fmt.debug_tuple("InvalidHeader").field(cause).finish(),
Self::Full => fmt.debug_tuple("Full").finish(),
Self::InvalidBlockSize => fmt.debug_tuple("InvalidBlockSize").finish(),
Self::UnexpectedEof => fmt.debug_tuple("UnexpectedEof").finish(),
Self::InvalidType => fmt.debug_tuple("InvalidType").finish(),
}
}
}
impl<B: Backend> error::Error for Error<B> {}
impl<B: Backend> From<nuts_bytes::Error> for Error<B> {
fn from(cause: nuts_bytes::Error) -> Self {
match cause {
nuts_bytes::Error::Serde(ref msg) => {
if msg == "invalid userdata-magic" {
Error::InvalidUserdata(Some(cause))
} else if msg == "invalid header-magic" {
Error::InvalidHeader(cause)
} else {
Error::Bytes(cause)
}
}
nuts_bytes::Error::NoSpace(_) | nuts_bytes::Error::Eof(_) => Error::InvalidBlockSize,
_ => Error::Bytes(cause),
}
}
}
impl<B: Backend> From<container::Error<B>> for Error<B> {
fn from(cause: container::Error<B>) -> Self {
Error::Container(cause)
}
}
pub type ArchiveResult<T, B> = Result<T, Error<B>>;