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
132
133
134
use crate::*;
use std::convert::TryFrom;
#[derive(Clone, Debug)]
pub struct DiffDescContent {
file_id: FileId,
diff_list: Vec<ChunkId>,
}
impl DiffDescContent {
pub fn new(file_id: FileId, diff_list: Vec<ChunkId>) -> Self {
Self { file_id, diff_list }
}
pub fn file_id(&self) -> &FileId {
&self.file_id
}
pub fn diff_list(&self) -> &Vec<ChunkId> {
&self.diff_list
}
}
impl RawEncode for DiffDescContent {
fn raw_measure(&self, purpose: &Option<RawEncodePurpose>) -> BuckyResult<usize> {
let size =
0 + self.file_id.raw_measure(purpose).map_err(|e| {
log::error!("DiffDescContent::raw_measure/file_id error:{}", e);
e
})? + self.diff_list.raw_measure(purpose).map_err(|e| {
log::error!("DiffDescContent::raw_measure/diff_list error:{}", e);
e
})?;
Ok(size)
}
fn raw_encode<'a>(
&self,
buf: &'a mut [u8],
purpose: &Option<RawEncodePurpose>,
) -> BuckyResult<&'a mut [u8]> {
let size = self.raw_measure(purpose).unwrap();
if buf.len() < size {
return Err(BuckyError::new(
BuckyErrorCode::OutOfLimit,
"[raw_encode] not enough buffer for DiffDescContent",
));
}
let buf = self.file_id.raw_encode(buf, purpose).map_err(|e| {
log::error!("DiffDescContent::raw_encode/file_id error:{}", e);
e
})?;
let buf = self.diff_list.raw_encode(buf, purpose).map_err(|e| {
log::error!("DiffDescContent::raw_encode/diff_list error:{}", e);
e
})?;
Ok(buf)
}
}
impl<'de> RawDecode<'de> for DiffDescContent {
fn raw_decode(buf: &'de [u8]) -> BuckyResult<(Self, &'de [u8])> {
let (file_id, buf) = FileId::raw_decode(buf).map_err(|e| {
log::error!("DiffDescContent::raw_decode/file_id error:{}", e);
e
})?;
let (diff_list, buf) = Vec::<ChunkId>::raw_decode(buf).map_err(|e| {
log::error!("DiffDescContent::raw_decode/diff_list error:{}", e);
e
})?;
Ok((Self { file_id, diff_list }, buf))
}
}
impl DescContent for DiffDescContent {
fn obj_type() -> u16 {
ObjectTypeCode::Diff.into()
}
fn debug_info() -> String {
String::from("DiffDescContent")
}
type OwnerType = Option<ObjectId>;
type AreaType = SubDescNone;
type AuthorType = SubDescNone;
type PublicKeyType = SubDescNone;
}
#[derive(Clone, Debug, RawEncode, RawDecode)]
pub struct DiffBodyContent {
}
impl BodyContent for DiffBodyContent {}
pub type DiffType = NamedObjType<DiffDescContent, DiffBodyContent>;
pub type DiffBuilder = NamedObjectBuilder<DiffDescContent, DiffBodyContent>;
pub type DiffDesc = NamedObjectDesc<DiffDescContent>;
pub type DiffId = NamedObjectId<DiffType>;
pub type Diff = NamedObjectBase<DiffType>;
impl DiffDesc {
pub fn diff_id(&self) -> DiffId {
DiffId::try_from(self.calculate_id()).unwrap()
}
}
impl Diff {
pub fn new(file_id: FileId, diff_list: Vec<ChunkId>) -> DiffBuilder {
let desc_content = DiffDescContent::new(file_id, diff_list);
let body_content = DiffBodyContent {};
DiffBuilder::new(desc_content, body_content)
}
}
#[cfg(test)]
mod test {
use crate::{ChunkId, Diff, FileId, RawConvertTo, RawFrom};
#[test]
fn diff() {
let action = Diff::new(FileId::default(), vec![ChunkId::default()]).build();
let buf = action.to_vec().unwrap();
let _obj = Diff::clone_from_slice(&buf).unwrap();
}
}