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)]
7pub enum CodeWord {
12 #[default]
13 W8,
15 W16,
17 W32,
19 Other(u8),
21}
22
23impl CodeWord {
24 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 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#[derive(Debug, thiserror::Error)]
56pub enum Error {
57 #[error("Too Many Erased Blocks: {0} erased, up to {1} allowed")]
60 TooManyErasure(i32, i32),
61 #[error("Invalid Arguments: {0}")]
63 InvalidArguments(String),
64 #[error("Not Aligned: {0} is not multiple of {MACHINE_LONG_SIZE}")]
66 NotAligned(usize),
67 #[error("Not Supported: {0}")]
69 NotSupported(String),
70 #[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}