1use ipld_core::cid::Cid;
2use rust_unixfs::file::adder::FileAdder;
3use serde::{Deserialize, Serialize};
4
5#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
6#[serde(untagged)]
7pub enum Data {
8 Base64(String),
9 Encrypted(()),
10}
11
12pub fn compute_data_cid(data: &[u8]) -> Option<String> {
14 let blocks = data_to_unixfs(data);
15 blocks.last().map(|(c, _)| c.to_string())
16}
17
18fn data_to_unixfs(data: &[u8]) -> Vec<(Cid, Vec<u8>)> {
19 let mut adder = FileAdder::default();
20
21 let mut total = 0;
22 let mut blocks = Vec::default();
23
24 while total < data.len() {
25 let (new_blocks, consumed) = adder.push(&data[total..]);
26 total += consumed;
27
28 for b in new_blocks {
29 blocks.push(b);
30 }
31 }
32
33 for b in adder.finish() {
34 blocks.push(b);
35 }
36
37 blocks
38}
39
40#[cfg(test)]
41mod tests {
42 use super::*;
43
44 #[test]
45 fn test_compute_data_cid() {
46 let data = "test data".as_bytes();
47 assert!(compute_data_cid(data).is_some());
48 }
49}