| 1 | use rle::{HasLength, Mergable, Sliceable}; |
| 2 | use serde::{Deserialize, Serialize}; |
| 3 | |
| 4 | use crate::{InternalString, LoroValue}; |
| 5 | |
| 6 | |
| 7 | #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] |
| 8 | pub struct MapSet { |
| 9 | pub(crate) key: InternalString, |
| 10 | |
| 11 | pub(crate) value: Option<LoroValue>, |
| 12 | } |
| 13 | |
| 14 | impl Mergable for MapSet {} |
| 15 | impl Sliceable for MapSet { |
| 16 | fn slice(&self, from: usize, to: usize) -> Self { |
| 17 | assert!(from == 0 && to == 1); |
| 18 | self.clone() |
| 19 | } |
| 20 | } |
| 21 | impl HasLength for MapSet { |
| 22 | fn content_len(&self) -> usize { |
| 23 | 1 |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | #[cfg(test)] |
| 28 | mod 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 | } |