taubyte_sdk/storage/
get.rs1use super::{imports, Storage};
2use crate::errno::Error;
3
4fn get_storage(name: &str, id: *mut u32) -> Error {
5 #[allow(unused_unsafe)]
6 unsafe {
7 imports::storageGet(name.as_ptr(), name.len(), id)
8 }
9}
10
11impl Storage {
12 pub fn get(name: &str) -> Result<Self, Box<dyn std::error::Error>> {
13 let mut id: u32 = 0;
14 let err0 = get_storage(name, &mut id);
15 if err0.is_err() {
16 Err(format!("Creating storage failed with: {}", err0).into())
17 } else {
18 Ok(Storage { id })
19 }
20 }
21}
22
23#[cfg(test)]
24pub mod test {
25 use crate::storage::Storage;
26
27 pub static STORAGE_ID: u32 = 1;
28 pub static STORAGE_NAME: &str = "testStorage";
29
30 #[test]
31 fn test_get() {
32 let storage = Storage::get(STORAGE_NAME).unwrap_or_else(|err| {
33 panic!("{}", err);
34 });
35
36 assert_eq!(storage.id, STORAGE_ID)
37 }
38}
39
40#[cfg(test)]
41#[allow(non_snake_case)]
42pub mod mock {
43 use crate::{
44 errno::{Errno, Error},
45 utils::test as utils,
46 };
47
48 pub fn storageGet(name_ptr: *const u8, name_size: usize, id: *mut u32) -> Error {
49 use super::test;
50
51 let name = utils::read_string(name_ptr, name_size);
52 if name != test::STORAGE_NAME {
53 Errno::ErrorCap.error()
54 } else {
55 utils::write_u32(id, test::STORAGE_ID);
56 Errno::ErrorNone.error()
57 }
58 }
59}