loro_ffi/
value.rs

1use std::collections::HashMap;
2
3use loro::{Counter, PeerID};
4
5pub trait LoroValueLike: Sync + Send {
6    fn as_loro_value(&self) -> crate::LoroValue;
7}
8
9#[derive(Debug, Clone, Copy)]
10pub enum ContainerType {
11    Text,
12    Map,
13    List,
14    MovableList,
15    Tree,
16    Counter,
17    Unknown { kind: u8 },
18}
19
20#[derive(Debug, Clone)]
21pub enum ContainerID {
22    Root {
23        name: String,
24        container_type: ContainerType,
25    },
26    Normal {
27        peer: PeerID,
28        counter: Counter,
29        container_type: ContainerType,
30    },
31}
32
33#[derive(Debug, Clone)]
34pub enum LoroValue {
35    Null,
36    Bool { value: bool },
37    Double { value: f64 },
38    I64 { value: i64 },
39    Binary { value: Vec<u8> },
40    String { value: String },
41    List { value: Vec<LoroValue> },
42    Map { value: HashMap<String, LoroValue> },
43    Container { value: ContainerID },
44}
45
46impl From<LoroValue> for loro::LoroValue {
47    fn from(value: LoroValue) -> loro::LoroValue {
48        match value {
49            LoroValue::Null => loro::LoroValue::Null,
50            LoroValue::Bool { value } => loro::LoroValue::Bool(value),
51            LoroValue::Double { value } => loro::LoroValue::Double(value),
52            LoroValue::I64 { value } => loro::LoroValue::I64(value),
53            LoroValue::Binary { value } => loro::LoroValue::Binary(value.into()),
54            LoroValue::String { value } => loro::LoroValue::String(value.into()),
55            LoroValue::List { value } => {
56                loro::LoroValue::List(value.into_iter().map(Into::into).collect())
57            }
58            LoroValue::Map { value } => {
59                loro::LoroValue::Map(value.into_iter().map(|(k, v)| (k, v.into())).collect())
60            }
61            LoroValue::Container { value } => loro::LoroValue::Container(value.into()),
62        }
63    }
64}
65
66impl From<&LoroValue> for loro::LoroValue {
67    fn from(value: &LoroValue) -> loro::LoroValue {
68        match value {
69            LoroValue::Null => loro::LoroValue::Null,
70            LoroValue::Bool { value } => loro::LoroValue::Bool(*value),
71            LoroValue::Double { value } => loro::LoroValue::Double(*value),
72            LoroValue::I64 { value } => loro::LoroValue::I64(*value),
73            LoroValue::Binary { value } => loro::LoroValue::Binary(value.clone().into()),
74            LoroValue::String { value } => loro::LoroValue::String(value.clone().into()),
75            LoroValue::List { value } => {
76                loro::LoroValue::List(value.iter().map(Into::into).collect())
77            }
78            LoroValue::Map { value } => {
79                loro::LoroValue::Map(value.iter().map(|(k, v)| (k.clone(), v.into())).collect())
80            }
81            LoroValue::Container { value } => loro::LoroValue::Container(value.into()),
82        }
83    }
84}
85
86impl From<loro::LoroValue> for LoroValue {
87    fn from(value: loro::LoroValue) -> LoroValue {
88        match value {
89            loro::LoroValue::Null => LoroValue::Null,
90            loro::LoroValue::Bool(value) => LoroValue::Bool { value },
91            loro::LoroValue::Double(value) => LoroValue::Double { value },
92            loro::LoroValue::I64(value) => LoroValue::I64 { value },
93            loro::LoroValue::Binary(value) => LoroValue::Binary {
94                value: value.to_vec(),
95            },
96            loro::LoroValue::String(value) => LoroValue::String {
97                value: value.to_string(),
98            },
99            loro::LoroValue::List(value) => LoroValue::List {
100                value: (*value).clone().into_iter().map(Into::into).collect(),
101            },
102            loro::LoroValue::Map(value) => LoroValue::Map {
103                value: (*value)
104                    .clone()
105                    .into_iter()
106                    .map(|(k, v)| (k, v.into()))
107                    .collect(),
108            },
109            loro::LoroValue::Container(value) => LoroValue::Container {
110                value: value.into(),
111            },
112        }
113    }
114}
115
116impl From<ContainerType> for loro::ContainerType {
117    fn from(value: ContainerType) -> loro::ContainerType {
118        match value {
119            ContainerType::Text => loro::ContainerType::Text,
120            ContainerType::Map => loro::ContainerType::Map,
121            ContainerType::List => loro::ContainerType::List,
122            ContainerType::MovableList => loro::ContainerType::MovableList,
123            ContainerType::Tree => loro::ContainerType::Tree,
124            ContainerType::Counter => loro::ContainerType::Counter,
125            ContainerType::Unknown { kind } => loro::ContainerType::Unknown(kind),
126        }
127    }
128}
129
130impl From<loro::ContainerType> for ContainerType {
131    fn from(value: loro::ContainerType) -> ContainerType {
132        match value {
133            loro::ContainerType::Text => ContainerType::Text,
134            loro::ContainerType::Map => ContainerType::Map,
135            loro::ContainerType::List => ContainerType::List,
136            loro::ContainerType::MovableList => ContainerType::MovableList,
137            loro::ContainerType::Tree => ContainerType::Tree,
138            loro::ContainerType::Counter => ContainerType::Counter,
139            loro::ContainerType::Unknown(kind) => ContainerType::Unknown { kind },
140        }
141    }
142}
143
144impl From<ContainerID> for loro::ContainerID {
145    fn from(value: ContainerID) -> loro::ContainerID {
146        match value {
147            ContainerID::Root {
148                name,
149                container_type,
150            } => loro::ContainerID::Root {
151                name: name.into(),
152                container_type: container_type.into(),
153            },
154            ContainerID::Normal {
155                peer,
156                counter,
157                container_type,
158            } => loro::ContainerID::Normal {
159                peer,
160                counter,
161                container_type: container_type.into(),
162            },
163        }
164    }
165}
166
167impl From<&ContainerID> for loro::ContainerID {
168    fn from(value: &ContainerID) -> loro::ContainerID {
169        match value {
170            ContainerID::Root {
171                name,
172                container_type,
173            } => loro::ContainerID::Root {
174                name: name.clone().into(),
175                container_type: (*container_type).into(),
176            },
177            ContainerID::Normal {
178                peer,
179                counter,
180                container_type,
181            } => loro::ContainerID::Normal {
182                peer: *peer,
183                counter: *counter,
184                container_type: (*container_type).into(),
185            },
186        }
187    }
188}
189
190impl From<loro::ContainerID> for ContainerID {
191    fn from(value: loro::ContainerID) -> ContainerID {
192        match value {
193            loro::ContainerID::Root {
194                name,
195                container_type,
196            } => ContainerID::Root {
197                name: name.to_string(),
198                container_type: container_type.into(),
199            },
200            loro::ContainerID::Normal {
201                peer,
202                counter,
203                container_type,
204            } => ContainerID::Normal {
205                peer,
206                counter,
207                container_type: container_type.into(),
208            },
209        }
210    }
211}
212
213impl From<&loro::ContainerID> for ContainerID {
214    fn from(value: &loro::ContainerID) -> ContainerID {
215        match value {
216            loro::ContainerID::Root {
217                name,
218                container_type,
219            } => ContainerID::Root {
220                name: name.to_string(),
221                container_type: (*container_type).into(),
222            },
223            loro::ContainerID::Normal {
224                peer,
225                counter,
226                container_type,
227            } => ContainerID::Normal {
228                peer: *peer,
229                counter: *counter,
230                container_type: (*container_type).into(),
231            },
232        }
233    }
234}