1pub use cursieve_derive::Sieve;
2
3pub trait Sieve: SieveSift + SieveDisperse {
4}
5
6pub trait SieveSift {
7 fn sift_cursor_at(cursor: &mut std::io::Cursor<&[u8]>, offset: u64) -> Result<Self, Error> where Self: Sized;
8 fn sift_cursor(cursor: &mut std::io::Cursor<&[u8]>) -> Result<Self, Error> where Self: Sized {
9 Self::sift_cursor_at(cursor, 0)
10 }
11 fn sift_at(data: &[u8], offset: u64) -> Result<Self, Error> where Self: Sized {
12 let mut cursor = std::io::Cursor::new(data);
13 Self::sift_cursor_at(&mut cursor, offset)
14 }
15 fn sift(data: &[u8]) -> Result<Self, Error> where Self: Sized {
16 let mut cursor = std::io::Cursor::new(data);
17 Self::sift_cursor(&mut cursor)
18 }
19}
20
21pub trait SieveDisperse {
22 fn disperse_cursor_at(&self, cursor: &mut std::io::Cursor<&mut [u8]>, offset: u64) -> Result<(), Error>;
23 fn disperse_cursor(&self, cursor: &mut std::io::Cursor<&mut [u8]>) -> Result<(), Error> {
24 self.disperse_cursor_at(cursor, 0)
25 }
26 fn disperse_at(&self, data: &mut [u8], offset: u64) -> Result<(), Error> {
27 let mut cursor = std::io::Cursor::new(data);
28 self.disperse_cursor_at(&mut cursor, offset)
29 }
30 fn disperse(&self, data: &mut [u8]) -> Result<(), Error> {
31 let mut cursor = std::io::Cursor::new(data);
32 self.disperse_cursor(&mut cursor)
33 }
34
35 fn sieve_size() -> usize;
36
37 fn to_bytes(&self) -> Result<Vec<u8>, Error> {
38 let mut data: Vec<u8> = vec![0; Self::sieve_size()];
39 let mut cursor = std::io::Cursor::new(&mut data[..]);
40 self.disperse_cursor(&mut cursor)?;
41 Ok(data)
42 }
43}
44
45#[derive(Debug, thiserror::Error)]
46pub enum Error {
47 #[error("IoError")]
48 IoError(#[from] std::io::Error),
49 #[error("IoError")]
50 TryFromError(String),
51 #[error("A Sieve error occurred: `{0}`")]
52 Generic(String),
53}
54
55impl From<String> for Error {
56 fn from(msg: String) -> Self {
57 Error::Generic(msg)
58 }
59}