taubyte_sdk/storage/content/
push.rs1use cid::Cid;
2
3use crate::{errno::Error, storage::Content, utils::codec};
4
5use super::{imports, ReadWriteContent};
6
7impl Content {
8 fn push_unsafe(&self, cid: *mut u8) -> Error {
9 #[allow(unused_unsafe)]
10 unsafe {
11 imports::contentPushFile(self.id, cid)
12 }
13 }
14
15 pub fn push(&self) -> Result<Cid, Box<dyn std::error::Error>> {
16 let mut reader = codec::cid::Reader::new();
17
18 let err0 = self.push_unsafe(reader.ptr());
19 if err0.is_err() {
20 Err(format!("Pushing content failed with: {}", err0).into())
21 } else {
22 Ok(reader.parse()?)
23 }
24 }
25}
26
27impl ReadWriteContent {
28 pub fn push(&self) -> Result<Cid, Box<dyn std::error::Error>> {
29 self.content.push()
30 }
31}
32
33#[cfg(test)]
34pub mod test {
35 use crate::storage::Content;
36
37 pub static CID: &str = "QmeqqyqBSwyJTSjEyiybuB8NUhJrecQSuNDnjEEE68Dy8z";
38
39 #[test]
40 fn cid_read_write() {
41 let read_write = Content::new().unwrap_or_else(|err| {
42 panic!("{}", err);
43 });
44
45 let cid = read_write.push().unwrap_or_else(|err| {
46 panic!("{}", err);
47 });
48
49 assert_eq!(cid.to_string(), CID);
50 }
51}
52
53#[cfg(test)]
54#[allow(non_snake_case)]
55pub mod mock {
56 use crate::{
57 errno::{Errno, Error},
58 storage::content::new::test as new_test,
59 utils::test as utils,
60 };
61
62 pub fn contentPushFile(id: u32, cid_ptr: *mut u8) -> Error {
63 use super::test;
64
65 if id != new_test::ID {
66 Errno::ErrorCap.error()
67 } else {
68 utils::write_cid_string(test::CID, cid_ptr);
69 Errno::ErrorNone.error()
70 }
71 }
72}