nuts_directory/error.rs
1// MIT License
2//
3// Copyright (c) 2022,2023 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 std::{error, fmt, io, result};
24
25/// The error type for the directory backend.
26#[derive(Debug)]
27pub enum Error {
28 /// An I/O error occured.
29 Io(io::Error),
30
31 /// You are creating a new backend which already exists.
32 Exists,
33
34 /// Could not generate a unique [id](crate::Id).
35 UniqueId,
36
37 /// The [id](crate::Id) is invalid, is not a hex string.
38 InvalidId(String),
39
40 /// The block size passed to [CreateOptions](crate::CreateOptions) is
41 /// invalid.
42 InvalidBlockSize(u32),
43}
44
45impl fmt::Display for Error {
46 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
47 match self {
48 Error::Io(cause) => fmt::Display::fmt(cause, fmt),
49 Error::Exists => write!(fmt, "The container already exists"),
50 Error::UniqueId => write!(fmt, "could not generate a unique id"),
51 Error::InvalidId(id) => write!(fmt, "The id '{}' is invalid", id),
52 Error::InvalidBlockSize(n) => write!(fmt, "The block-size is invalid: {}", n),
53 }
54 }
55}
56
57impl error::Error for Error {
58 fn source(&self) -> Option<&(dyn error::Error + 'static)> {
59 match self {
60 Error::Io(cause) => Some(cause),
61 Error::Exists | Error::UniqueId | Error::InvalidId(_) | Error::InvalidBlockSize(_) => {
62 None
63 }
64 }
65 }
66}
67
68impl From<io::Error> for Error {
69 fn from(cause: io::Error) -> Self {
70 Error::Io(cause)
71 }
72}
73
74pub type Result<T> = result::Result<T, Error>;