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
mod health;
mod version;
pub use health::{Error as StorageHealthError, StorageHealth};
pub use version::StorageVersion;
use bee_common::packable::{Packable, Read, Write};
pub const SYSTEM_VERSION_KEY: u8 = 0;
pub const SYSTEM_HEALTH_KEY: u8 = 1;
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("I/O error happened: {0}")]
Io(#[from] std::io::Error),
#[error("Storage health error: {0}")]
Health(#[from] StorageHealthError),
#[error("Unknown system key: {0}")]
UnknownSystemKey(u8),
}
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum System {
Version(StorageVersion),
Health(StorageHealth),
}
impl Packable for System {
type Error = Error;
fn packed_len(&self) -> usize {
match self {
System::Version(version) => SYSTEM_VERSION_KEY.packed_len() + version.packed_len(),
System::Health(health) => SYSTEM_HEALTH_KEY.packed_len() + health.packed_len(),
}
}
fn pack<W: Write>(&self, writer: &mut W) -> Result<(), Self::Error> {
match self {
System::Version(version) => {
SYSTEM_VERSION_KEY.pack(writer)?;
version.pack(writer)?;
}
System::Health(health) => {
SYSTEM_HEALTH_KEY.pack(writer)?;
health.pack(writer)?;
}
}
Ok(())
}
fn unpack_inner<R: Read + ?Sized, const CHECK: bool>(reader: &mut R) -> Result<Self, Self::Error> {
match u8::unpack_inner::<R, CHECK>(reader)? {
SYSTEM_VERSION_KEY => Ok(System::Version(StorageVersion::unpack_inner::<R, CHECK>(reader)?)),
SYSTEM_HEALTH_KEY => Ok(System::Health(StorageHealth::unpack_inner::<R, CHECK>(reader)?)),
s => Err(Error::UnknownSystemKey(s)),
}
}
}