ckbfs_types/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#![no_std]
extern crate alloc;
pub use crate::generated::ckbfs::{Bytes, CKBFSData};
use alloc::{string::String, vec::Vec};

use generated::ckbfs::{BackLink, BackLinkVec, Byte32, Indexes, Uint32};
use molecule::prelude::{Builder, Entity};

pub mod generated;

impl Into<Bytes> for &[u8] {
    fn into(self) -> Bytes {
        let len = self.len();
        let mut vec: Vec<u8> = Vec::with_capacity(4 + len);
        vec.extend_from_slice(&(len as u32).to_le_bytes()[..]);
        vec.extend_from_slice(self);
        Bytes::new_unchecked(Bytes::from_slice(vec.as_slice()).unwrap().as_bytes())
    }
}

#[derive(Debug, Clone)]
pub struct BackLinkNative {
    pub indexes: Vec<u32>,
    pub checksum: u32,
    pub tx_hash: [u8; 32],
}

impl Into<BackLinkNative> for BackLink {
    fn into(self) -> BackLinkNative {
        let indexes = self
            .indexes()
            .into_iter()
            .map(|x| {
                u32::from_le_bytes(x.as_slice().try_into().unwrap())
                    .try_into()
                    .unwrap()
            })
            .collect::<Vec<u32>>();
        let checksum = u32::from_le_bytes(self.checksum().as_slice().try_into().unwrap());
        let tx_hash = self.tx_hash().as_slice().try_into().unwrap();
        BackLinkNative {
            indexes,
            checksum,
            tx_hash,
        }
    }
}

impl Into<BackLink> for BackLinkNative {
    fn into(self) -> BackLink {
        BackLink::new_builder()
            .indexes(
                Indexes::new_builder()
                    .extend(self.indexes.iter().map(|x| {
                        Uint32::new_unchecked(molecule::bytes::Bytes::from(
                            x.to_le_bytes().to_vec(),
                        ))
                    }))
                    .build(),
            )
            .checksum(Uint32::new_unchecked(molecule::bytes::Bytes::from(
                self.checksum.to_le_bytes().to_vec(),
            )))
            .tx_hash(Byte32::new_unchecked(molecule::bytes::Bytes::from(
                self.tx_hash.to_vec(),
            )))
            .build()
    }
}

#[derive(Debug, Clone)]
pub struct CKBFSDataNative {
    pub indexes: Vec<u32>,
    pub checksum: u32,
    pub content_type: String,
    pub filename: String,
    pub backlinks: Vec<BackLinkNative>,
}

impl From<CKBFSDataNative> for CKBFSData {
    fn from(data: CKBFSDataNative) -> Self {
        let content_type = data.content_type.as_bytes().into();
        let filename = data.filename.as_bytes().into();
        let indexes = data
            .indexes
            .iter()
            .map(|x| Uint32::new_unchecked(molecule::bytes::Bytes::from(x.to_le_bytes().to_vec())))
            .collect::<_>();
        let backlinks = data
            .backlinks
            .into_iter()
            .map(|backlink| backlink.into())
            .collect::<Vec<BackLink>>();
        CKBFSData::new_builder()
            .indexes(Indexes::new_builder().set(indexes).build())
            .checksum(Uint32::new_unchecked(molecule::bytes::Bytes::from(
                data.checksum.to_le_bytes().to_vec(),
            )))
            .filename(filename)
            .content_type(content_type)
            .backlinks(BackLinkVec::new_builder().extend(backlinks).build())
            .build()
    }
}

impl Into<CKBFSDataNative> for CKBFSData {
    fn into(self) -> CKBFSDataNative {
        let content_type = String::from_utf8(self.content_type().as_slice().to_vec())
            .expect("Failed to extract content-type");
        let filename = String::from_utf8(self.filename().as_slice().to_vec())
            .expect("Failed to extract filname");
        let indexes = self
            .indexes()
            .into_iter()
            .map(|x| u32::from_le_bytes(x.as_slice().try_into().unwrap()))
            .collect::<Vec<u32>>();
        let checksum = u32::from_le_bytes(self.checksum().as_slice().try_into().unwrap());
        let backlinks = self
            .backlinks()
            .into_iter()
            .map(|raw_backlink| raw_backlink.into())
            .collect::<Vec<BackLinkNative>>();
        CKBFSDataNative {
            indexes,
            checksum,
            content_type,
            filename,
            backlinks,
        }
    }
}