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