Skip to main content

jerasure_rs/
lib.rs

1pub mod erasure;
2pub mod galois;
3
4const MACHINE_LONG_SIZE: usize = size_of::<std::os::raw::c_long>();
5
6#[derive(Debug, Clone, Copy, Default)]
7/// The `CodeWord` is used to represent the size of the code word in bits.
8///
9/// The `CodeWord` enum defines the possible code words that can be used in galois fied.
10/// And the default value is `W8`, which is the most common code word size.
11pub enum CodeWord {
12    #[default]
13    /// A code word of 1 Byte.
14    W8,
15    /// A code word of 2 Bytes.
16    W16,
17    /// A code word of 4 Bytes.
18    W32,
19    /// A code word of other size in bits.
20    Other(u8),
21}
22
23impl CodeWord {
24    /// Makes a new `CodeWord` from the given size in bits.
25    pub fn from_u8(w: u8) -> Self {
26        match w {
27            8 => Self::W8,
28            16 => Self::W16,
29            32 => Self::W32,
30            _ => Self::Other(w),
31        }
32    }
33
34    /// Returns the size of the code word in bits.
35    pub fn to_u8(&self) -> u8 {
36        match self {
37            Self::W8 => 8,
38            Self::W16 => 16,
39            Self::W32 => 32,
40            Self::Other(w) => *w,
41        }
42    }
43
44    fn as_cint(&self) -> ::std::ffi::c_int {
45        match self {
46            Self::W8 => 8,
47            Self::W16 => 16,
48            Self::W32 => 32,
49            Self::Other(w) => *w as ::std::ffi::c_int,
50        }
51    }
52}
53
54/// The `Error` enum defines the possible errors that this crate can occur.
55#[derive(Debug, thiserror::Error)]
56pub enum Error {
57    /// TooManyErasure: The number of erasures is larger than the maximum allowed,
58    /// and the lost data cannot be recovered.
59    #[error("Too Many Erased Blocks: {0} erased, up to {1} allowed")]
60    TooManyErasure(i32, i32),
61    /// InvalidArguments: The the input is invalid.
62    #[error("Invalid Arguments: {0}")]
63    InvalidArguments(String),
64    /// NotAligned: The input is not a multiple of the machine long size.
65    #[error("Not Aligned: {0} is not multiple of {MACHINE_LONG_SIZE}")]
66    NotAligned(usize),
67    /// NotSupported: The input is not supported.
68    #[error("Not Supported: {0}")]
69    NotSupported(String),
70    /// Other: Other errors that are not covered by the above.
71    #[error("Error: {0}")]
72    Other(String),
73}
74
75impl Error {
76    fn too_many_erasure(erasures: i32, max_erasures: i32) -> Self {
77        Self::TooManyErasure(erasures, max_erasures)
78    }
79
80    fn invalid_arguments(msg: impl Into<String>) -> Self {
81        Self::InvalidArguments(msg.into())
82    }
83
84    fn not_supported(msg: impl Into<String>) -> Self {
85        Self::NotSupported(msg.into())
86    }
87
88    fn other(msg: impl Into<String>) -> Self {
89        Self::Other(msg.into())
90    }
91}
92
93#[cfg(test)]
94mod tests {
95    use jerasure_sys;
96
97    #[test]
98    fn link_works() {
99        unsafe {
100            jerasure_sys::jerasure::galois_init_default_field(8);
101        }
102    }
103}