forge
Explore
Log in

Don't you dare go Hollow

project_note

Clone
★ 0

0 watching0 forksRust3.6 MiBc49f5e7Tahinli1 month ago2 commits

map_content.rs42 lines, 1.1 KiBHistoryRawDownload
1use rle::{HasLength, Mergable, Sliceable};
2use serde::{Deserialize, Serialize};
3
4use crate::{InternalString, LoroValue};
5
6// Note: It will be encoded into binary format, so the order of its fields should not be changed.
7#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
8pub struct MapSet {
9 pub(crate) key: InternalString,
10 // the key is deleted if value is None
11 pub(crate) value: Option<LoroValue>,
12}
13
14impl Mergable for MapSet {}
15impl Sliceable for MapSet {
16 fn slice(&self, from: usize, to: usize) -> Self {
17 assert!(from == 0 && to == 1);
18 self.clone()
19 }
20}
21impl HasLength for MapSet {
22 fn content_len(&self) -> usize {
23 1
24 }
25}
26
27#[cfg(test)]
28mod test {
29 use super::MapSet;
30 #[test]
31 fn fix_fields_order() {
32 let map_set = vec![MapSet {
33 key: "key".to_string().into(),
34 value: Some("value".to_string().into()),
35 }];
36 let map_set_buf = vec![1, 3, 107, 101, 121, 1, 4, 5, 118, 97, 108, 117, 101];
37 assert_eq!(
38 postcard::from_bytes::<Vec<MapSet>>(&map_set_buf).unwrap(),
39 map_set
40 );
41 }
42}