cyfs_base/objects/
diff.rs1use crate::*;
2
3use std::convert::TryFrom;
4
5#[derive(Clone, Debug)]
6pub struct DiffDescContent {
7 file_id: FileId, diff_list: Vec<ChunkId>, }
10
11impl DiffDescContent {
12 pub fn new(file_id: FileId, diff_list: Vec<ChunkId>) -> Self {
13 Self { file_id, diff_list }
14 }
15
16 pub fn file_id(&self) -> &FileId {
17 &self.file_id
18 }
19
20 pub fn diff_list(&self) -> &Vec<ChunkId> {
21 &self.diff_list
22 }
23}
24
25impl RawEncode for DiffDescContent {
26 fn raw_measure(&self, purpose: &Option<RawEncodePurpose>) -> BuckyResult<usize> {
27 let size =
28 0 + self.file_id.raw_measure(purpose).map_err(|e| {
29 log::error!("DiffDescContent::raw_measure/file_id error:{}", e);
30 e
31 })? + self.diff_list.raw_measure(purpose).map_err(|e| {
32 log::error!("DiffDescContent::raw_measure/diff_list error:{}", e);
33 e
34 })?;
35 Ok(size)
36 }
37
38 fn raw_encode<'a>(
39 &self,
40 buf: &'a mut [u8],
41 purpose: &Option<RawEncodePurpose>,
42 ) -> BuckyResult<&'a mut [u8]> {
43 let size = self.raw_measure(purpose).unwrap();
44 if buf.len() < size {
45 return Err(BuckyError::new(
46 BuckyErrorCode::OutOfLimit,
47 "[raw_encode] not enough buffer for DiffDescContent",
48 ));
49 }
50
51 let buf = self.file_id.raw_encode(buf, purpose).map_err(|e| {
52 log::error!("DiffDescContent::raw_encode/file_id error:{}", e);
53 e
54 })?;
55
56 let buf = self.diff_list.raw_encode(buf, purpose).map_err(|e| {
57 log::error!("DiffDescContent::raw_encode/diff_list error:{}", e);
58 e
59 })?;
60 Ok(buf)
61 }
62}
63
64impl<'de> RawDecode<'de> for DiffDescContent {
65 fn raw_decode(buf: &'de [u8]) -> BuckyResult<(Self, &'de [u8])> {
66 let (file_id, buf) = FileId::raw_decode(buf).map_err(|e| {
67 log::error!("DiffDescContent::raw_decode/file_id error:{}", e);
68 e
69 })?;
70
71 let (diff_list, buf) = Vec::<ChunkId>::raw_decode(buf).map_err(|e| {
72 log::error!("DiffDescContent::raw_decode/diff_list error:{}", e);
73 e
74 })?;
75
76 Ok((Self { file_id, diff_list }, buf))
77 }
78}
79
80impl DescContent for DiffDescContent {
81 fn obj_type() -> u16 {
82 ObjectTypeCode::Diff.into()
83 }
84
85 fn debug_info() -> String {
86 String::from("DiffDescContent")
87 }
88
89 type OwnerType = Option<ObjectId>;
90 type AreaType = SubDescNone;
91 type AuthorType = SubDescNone;
92 type PublicKeyType = SubDescNone;
93}
94
95#[derive(Clone, Debug, RawEncode, RawDecode)]
96pub struct DiffBodyContent {
97 }
99
100impl BodyContent for DiffBodyContent {}
101
102pub type DiffType = NamedObjType<DiffDescContent, DiffBodyContent>;
103pub type DiffBuilder = NamedObjectBuilder<DiffDescContent, DiffBodyContent>;
104
105pub type DiffDesc = NamedObjectDesc<DiffDescContent>;
106pub type DiffId = NamedObjectId<DiffType>;
107pub type Diff = NamedObjectBase<DiffType>;
108
109impl DiffDesc {
110 pub fn diff_id(&self) -> DiffId {
111 DiffId::try_from(self.calculate_id()).unwrap()
112 }
113}
114
115impl Diff {
116 pub fn new(file_id: FileId, diff_list: Vec<ChunkId>) -> DiffBuilder {
117 let desc_content = DiffDescContent::new(file_id, diff_list);
118 let body_content = DiffBodyContent {};
119 DiffBuilder::new(desc_content, body_content)
120 }
121}
122
123#[cfg(test)]
124mod test {
125 use crate::{ChunkId, Diff, FileId, RawConvertTo, RawFrom};
126
127 #[test]
128 fn diff() {
129 let action = Diff::new(FileId::default(), vec![ChunkId::default()]).build();
130
131 let buf = action.to_vec().unwrap();
132 let _obj = Diff::clone_from_slice(&buf).unwrap();
133 }
134}