taubyte_sdk/storage/content/
new.rs

1use crate::errno::Error;
2
3use super::{imports, Content, ReadWriteContent};
4
5impl Content {
6    fn new_unsafe(id: *mut u32) -> Error {
7        #[allow(unused_unsafe)]
8        unsafe {
9            imports::storageNewContent(id)
10        }
11    }
12
13    pub fn new() -> Result<ReadWriteContent, Box<dyn std::error::Error>> {
14        let mut id: u32 = 0;
15        let err0 = Content::new_unsafe(&mut id);
16        if err0.is_err() {
17            Err(format!("Creating new content failed with: {}", err0).into())
18        } else {
19            Ok(ReadWriteContent {
20                content: Content {
21                    id: id,
22                    consumed: false,
23                },
24            })
25        }
26    }
27}
28
29#[cfg(test)]
30pub mod test {
31    use crate::storage::Content;
32
33    pub static ID: u32 = 4;
34
35    #[test]
36    fn new() {
37        let read_write = Content::new().unwrap_or_else(|err| {
38            panic!("{}", err);
39        });
40        assert_eq!(read_write.content.id, ID);
41    }
42}
43
44#[cfg(test)]
45#[allow(non_snake_case)]
46pub mod mock {
47    use super::test;
48    use crate::{
49        errno::{Errno, Error},
50        utils::test as utils,
51    };
52
53    pub fn storageNewContent(id: *mut u32) -> Error {
54        utils::write_u32(id, test::ID);
55        Errno::ErrorNone.error()
56    }
57}