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
use std::fmt;
use std::path::PathBuf;

use crate::{ObjectId, ObjectVersion};

#[derive(Debug, Clone)]
pub enum AllocationError {
    InvalidSize(usize, usize),
    BackingStorageFull(),
}

impl fmt::Display for AllocationError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Self::InvalidSize(req, lim) => f.write_fmt(format_args!(
                "Requested size ({}) exceeds maximum ({})",
                req, lim
            )),
            _ => write!(f, "An unknown error occured"),
        }
    }
}

#[derive(Debug, Clone)]
pub enum AccessError {
    UnknownObject(String),
    InvalidAddress(),
}

impl fmt::Display for AccessError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Self::UnknownObject(object_id) => {
                f.write_fmt(format_args!("No object with id {} exists", object_id))
            }
            _ => write!(f, "An unknown error occured"),
        }
    }
}

#[derive(Debug, Clone)]
pub enum UpdateError {
    VersionOverflowError(ObjectId, ObjectVersion),
}

impl fmt::Display for UpdateError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Self::VersionOverflowError(object_id, initial_object_version) => {
                f.write_fmt(format_args!(
                    "Cannot update version {} of object {}, operation would lead to overflow",
                    initial_object_version, object_id
                ))
            }
        }
    }
}

#[derive(Debug, Clone)]
pub enum ObjectOperationError {
    OperationAllocationError(AllocationError),
    OperationAccessError(AccessError),
    OperationUpdateError(UpdateError),
    UnknownError(),
}

impl fmt::Display for ObjectOperationError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Self::UnknownError() => write!(f, "An unknown error occured"),
            e => e.fmt(f),
        }
    }
}

#[derive(Debug, Clone)]
pub enum FileError {
    DirCreationError(PathBuf),
    UnknownError(),
}

impl fmt::Display for FileError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Self::DirCreationError(d) => write!(
                f,
                "Failed to set up target directory {}",
                d.to_str().unwrap()
            ),
            _ => write!(f, "An unknown error occured"),
        }
    }
}