json0-rs 0.1.0

JSON0 OT implement in rust.
Documentation
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
use std::fmt::Display;
use std::hash::Hash;
use std::vec;

use dashmap::mapref::one::Ref;
use dashmap::DashMap;
use serde_json::{Map, Value};

use crate::error::{JsonError, Result};
use crate::operation::Operator;
use crate::path::Path;
use crate::transformer::TransformSide;

const NUMBER_ADD_SUB_TYPE_NAME: &str = "na";
const TEXT_SUB_TYPE_NAME: &str = "text";

pub trait SubTypeFunctions {
    fn box_clone(&self) -> Box<dyn SubTypeFunctions>;

    fn invert(&self, path: &Path, sub_type_operand: &Value) -> Result<Operator>;

    fn merge(&self, base_operand: &Value, other: &Operator) -> Option<Operator>;

    fn transform(&self, new: &Value, base: &Value, side: TransformSide) -> Result<Vec<Value>>;

    fn apply(&self, val: Option<&Value>, sub_type_operand: &Value) -> Result<Option<Value>>;

    fn validate_operand(&self, val: &Value) -> Result<()>;
}

impl Clone for Box<dyn SubTypeFunctions> {
    fn clone(&self) -> Self {
        self.box_clone()
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum SubType {
    NumberAdd,
    Text,
    Custome(String),
}

impl TryFrom<&Value> for SubType {
    type Error = JsonError;

    fn try_from(value: &Value) -> std::result::Result<Self, Self::Error> {
        match value {
            Value::String(sub) => {
                if sub.eq(NUMBER_ADD_SUB_TYPE_NAME) {
                    return Ok(SubType::NumberAdd);
                }
                if sub.eq(TEXT_SUB_TYPE_NAME) {
                    return Ok(SubType::Text);
                }
                Ok(SubType::Custome(sub.to_string()))
            }
            _ => Err(JsonError::InvalidOperation(format!(
                "invalid sub type: {}",
                value
            ))),
        }
    }
}

impl Display for SubType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let s: String = match self {
            SubType::NumberAdd => NUMBER_ADD_SUB_TYPE_NAME.into(),
            SubType::Text => TEXT_SUB_TYPE_NAME.into(),
            SubType::Custome(t) => t.to_string(),
        };
        f.write_str(&s)?;
        Ok(())
    }
}

pub struct SubTypeFunctionsHolder {
    subtype_operators: DashMap<SubType, Box<dyn SubTypeFunctions>>,
}

impl SubTypeFunctionsHolder {
    pub fn new() -> SubTypeFunctionsHolder {
        let subtype_operators: DashMap<SubType, Box<dyn SubTypeFunctions>> = DashMap::new();
        subtype_operators.insert(SubType::NumberAdd, Box::new(NumberAddSubType {}));
        subtype_operators.insert(SubType::Text, Box::new(TextSubType {}));
        SubTypeFunctionsHolder { subtype_operators }
    }

    pub fn register_subtype(
        &self,
        sub_type: String,
        o: Box<dyn SubTypeFunctions>,
    ) -> Result<Option<Box<dyn SubTypeFunctions>>> {
        if sub_type.eq(NUMBER_ADD_SUB_TYPE_NAME) || sub_type.eq(TEXT_SUB_TYPE_NAME) {
            return Err(JsonError::ConflictSubType(sub_type));
        }

        Ok(self.subtype_operators.insert(SubType::Custome(sub_type), o))
    }

    pub fn unregister_subtype(&self, sub_type: &String) -> Option<Box<dyn SubTypeFunctions>> {
        if sub_type.eq(NUMBER_ADD_SUB_TYPE_NAME) || sub_type.eq(TEXT_SUB_TYPE_NAME) {
            return None;
        }

        self.subtype_operators
            .remove(&SubType::Custome(sub_type.clone()))
            .map(|s| s.1)
    }

    pub fn get(&self, sub_type: &SubType) -> Option<Ref<SubType, Box<dyn SubTypeFunctions>>> {
        self.subtype_operators.get(sub_type)
    }

    pub fn clear(&self) {
        self.subtype_operators.clear();
    }
}

impl Default for SubTypeFunctionsHolder {
    fn default() -> Self {
        Self::new()
    }
}

struct NumberAddSubType {}

impl SubTypeFunctions for NumberAddSubType {
    fn box_clone(&self) -> Box<dyn SubTypeFunctions> {
        Box::new(NumberAddSubType {})
    }

    fn invert(&self, _: &Path, sub_type_operand: &Value) -> Result<Operator> {
        if let Value::Number(n) = sub_type_operand {
            if n.is_i64() {
                Ok(Operator::SubType(
                    SubType::NumberAdd,
                    serde_json::to_value(-n.as_i64().unwrap()).unwrap(),
                    self.box_clone(),
                ))
            } else if n.is_f64() {
                Ok(Operator::SubType(
                    SubType::NumberAdd,
                    serde_json::to_value(-n.as_f64().unwrap()).unwrap(),
                    self.box_clone(),
                ))
            } else {
                Err(JsonError::InvalidOperation(format!(
                    "invalid number value:\"{}\" in NumberAdd sub type operand",
                    sub_type_operand
                )))
            }
        } else {
            Err(JsonError::InvalidOperation(format!(
                "invalid operand:\"{}\" for NumberAdd sub type",
                sub_type_operand
            )))
        }
    }

    fn merge(&self, base_operand: &Value, other: &Operator) -> Option<Operator> {
        match &other {
            Operator::SubType(_, other_v, _) => {
                if base_operand.is_i64() && other_v.is_i64() {
                    let new_v = base_operand.as_i64().unwrap() + other_v.as_i64().unwrap();
                    Some(Operator::SubType(
                        SubType::NumberAdd,
                        serde_json::to_value(new_v).unwrap(),
                        self.box_clone(),
                    ))
                } else if base_operand.is_f64() || other_v.is_f64() {
                    let new_v = base_operand.as_f64().unwrap() + other_v.as_f64().unwrap();
                    Some(Operator::SubType(
                        SubType::NumberAdd,
                        serde_json::to_value(new_v).unwrap(),
                        self.box_clone(),
                    ))
                } else {
                    None
                }
            }
            _ => None,
        }
    }

    fn transform(&self, new: &Value, _: &Value, _: TransformSide) -> Result<Vec<Value>> {
        Ok(vec![new.clone()])
    }

    fn apply(&self, val: Option<&Value>, sub_type_operand: &Value) -> Result<Option<Value>> {
        if let Value::Number(new_n) = sub_type_operand {
            if let Some(old_v) = val {
                match old_v {
                    Value::Number(old_n) => {
                        if old_n.is_i64() && new_n.is_i64() {
                            return Ok(Some(serde_json::to_value(
                                old_n.as_i64().unwrap() + new_n.as_i64().unwrap(),
                            )?));
                        }

                        Ok(Some(serde_json::to_value(
                            old_n.as_f64().unwrap() + new_n.as_f64().unwrap(),
                        )?))
                    }
                    _ => Err(JsonError::BadPath),
                }
            } else {
                Ok(Some(sub_type_operand.clone()))
            }
        } else {
            Err(JsonError::InvalidOperation(format!(
                "operand: \"{}\" for NumberAdd sub type is not a number",
                sub_type_operand
            )))
        }
    }

    fn validate_operand(&self, val: &Value) -> Result<()> {
        match val {
            Value::Number(_) => Ok(()),
            _ => Err(JsonError::InvalidOperation(
                "Value in AddNumber operator is not a number".into(),
            )),
        }
    }
}

#[derive(Debug, PartialEq)]
struct TextOperand {
    offset: usize,
    insert: Option<String>,
    delete: Option<String>,
}

impl TextOperand {
    fn new_insert(offset: usize, insert: String) -> TextOperand {
        TextOperand {
            offset,
            insert: Some(insert),
            delete: None,
        }
    }
    fn new_delete(offset: usize, delete: String) -> TextOperand {
        TextOperand {
            offset,
            insert: None,
            delete: Some(delete),
        }
    }
    fn is_insert(&self) -> bool {
        self.insert.is_some()
    }
    fn is_delete(&self) -> bool {
        self.delete.is_some()
    }
    fn get_insert(&self) -> &Option<String> {
        &self.insert
    }
    fn get_delete(&self) -> &Option<String> {
        &self.delete
    }
    fn uncheck_get_insert(&self) -> String {
        self.get_insert().as_ref().unwrap().clone()
    }
    fn uncheck_get_delete(&self) -> String {
        self.get_delete().as_ref().unwrap().clone()
    }
    fn to_value(&self) -> Value {
        let mut op = Map::new();
        op.insert("p".into(), serde_json::to_value(self.offset).unwrap());

        if let Some(i) = &self.insert {
            op.insert("i".into(), Value::String(i.clone()));
        }

        if let Some(d) = &self.delete {
            op.insert("d".into(), Value::String(d.clone()));
        }
        Value::Object(op)
    }
}

impl PartialOrd for TextOperand {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        self.offset.partial_cmp(&other.offset)
    }
}

impl TryFrom<&Value> for TextOperand {
    type Error = JsonError;

    fn try_from(val: &Value) -> std::result::Result<Self, Self::Error> {
        let p = val.get("p");
        if p.is_none() {
            return Err(JsonError::InvalidOperation(
                "text sub type operand does not contains Offset".into(),
            ));
        }
        if !p.unwrap().is_i64() {
            return Err(JsonError::InvalidOperation(format!(
                "offset: {} in text sub type operand is not value number",
                p.unwrap()
            )));
        }

        let offset = p.unwrap().as_i64().unwrap() as usize;

        if let Some(insert) = val.get("i") {
            if val.get("d").is_some() {
                return Err(JsonError::InvalidOperation(format!(
                    "invalid text operand: {}, insert and delete at the same time",
                    val
                )));
            }
            if !insert.is_string() {
                return Err(JsonError::InvalidOperation(format!(
                    "text insert non-string value: {}",
                    insert
                )));
            }
            return Ok(TextOperand {
                offset,
                insert: Some(insert.as_str().unwrap().into()),
                delete: None,
            });
        }

        if let Some(delete) = val.get("d") {
            if !delete.is_string() {
                return Err(JsonError::InvalidOperation(format!(
                    "text delete non-string value: {}",
                    delete
                )));
            }
            return Ok(TextOperand {
                offset,
                insert: None,
                delete: Some(delete.as_str().unwrap().into()),
            });
        }
        Err(JsonError::InvalidOperation(format!(
            "invalid text operand: {}",
            val
        )))
    }
}

struct TextSubType {}

impl TextSubType {
    fn invert_object(&self, op: &TextOperand) -> Result<TextOperand> {
        if let Some(i) = op.get_insert() {
            Ok(TextOperand::new_delete(op.offset, i.clone()))
        } else if let Some(d) = op.get_delete() {
            Ok(TextOperand::new_insert(op.offset, d.clone()))
        } else {
            Err(JsonError::InvalidOperation(format!(
                "invalid sub type operand:\"{}\" for TextSubType",
                op.to_value()
            )))
        }
    }

    fn transform_position(&self, pos: usize, op: &TextOperand, insert_after: bool) -> usize {
        let p = op.offset;
        if let Some(i) = &op.insert {
            if p < pos || (p == pos && insert_after) {
                pos + i.len()
            } else {
                pos
            }
        } else if pos <= p {
            pos
        } else if pos <= p + op.delete.as_ref().unwrap().len() {
            p
        } else {
            pos - op.delete.as_ref().unwrap().len()
        }
    }
}

impl SubTypeFunctions for TextSubType {
    fn box_clone(&self) -> Box<dyn SubTypeFunctions> {
        Box::new(TextSubType {})
    }

    fn invert(&self, _: &Path, sub_type_operand: &Value) -> Result<Operator> {
        let s: TextOperand = sub_type_operand.try_into()?;
        Ok(Operator::SubType(
            SubType::Text,
            self.invert_object(&s)?.to_value(),
            self.box_clone(),
        ))
    }

    fn merge(&self, base: &Value, other: &Operator) -> Option<Operator> {
        if let Operator::SubType(sub_type, sub_type_operand, _) = other {
            if SubType::Text.eq(sub_type) {
                let base_op: TextOperand = base.try_into().ok()?;
                let other_op: TextOperand = sub_type_operand.try_into().ok()?;

                if base_op.is_insert()
                    && other_op.is_insert()
                    && base_op <= other_op
                    && other_op.offset <= base_op.offset + base_op.uncheck_get_insert().len()
                {
                    let s = format!(
                        "{}{}{}",
                        &base_op.uncheck_get_insert()[0..other_op.offset - base_op.offset],
                        &other_op.uncheck_get_insert(),
                        &base_op.uncheck_get_insert()[other_op.offset - base_op.offset..],
                    );

                    return Some(Operator::SubType(
                        SubType::Text,
                        TextOperand::new_insert(base_op.offset, s).to_value(),
                        self.box_clone(),
                    ));
                }
                if base_op.is_delete()
                    && other_op.is_delete()
                    && other_op <= base_op
                    && base_op.offset <= other_op.offset + other_op.uncheck_get_delete().len()
                {
                    let s = format!(
                        "{}{}{}",
                        &other_op.uncheck_get_delete()[0..base_op.offset - other_op.offset],
                        &base_op.uncheck_get_delete(),
                        &other_op.uncheck_get_delete()[base_op.offset - other_op.offset..],
                    );

                    return Some(Operator::SubType(
                        SubType::Text,
                        TextOperand::new_delete(other_op.offset, s).to_value(),
                        self.box_clone(),
                    ));
                }
            }
        }

        None
    }

    fn transform(&self, new: &Value, base: &Value, side: TransformSide) -> Result<Vec<Value>> {
        let new_operand: TextOperand = new.try_into()?;
        let base_operand: TextOperand = base.try_into()?;
        let mut ops = vec![];
        if new_operand.is_insert() {
            let p = self.transform_position(
                new_operand.offset,
                &base_operand,
                side == TransformSide::RIGHT,
            );
            ops.push(TextOperand::new_insert(p, new_operand.insert.unwrap()).to_value())
        } else {
            let mut d_str = new_operand.uncheck_get_delete();
            if let Some(base_i) = base_operand.get_insert() {
                let base_p = base_operand.offset;
                let new_p = new_operand.offset;
                if new_operand < base_operand {
                    ops.push(
                        TextOperand::new_delete(
                            new_operand.offset,
                            d_str[0..(base_p - new_p)].into(),
                        )
                        .to_value(),
                    );
                    d_str = d_str[base_p - new_p..].into();
                }
                if !d_str.is_empty() {
                    ops.push(
                        TextOperand::new_delete(new_operand.offset + base_i.len(), d_str)
                            .to_value(),
                    );
                }
            } else {
                // Delete vs Delete
                let base_d_str = base_operand.uncheck_get_delete();
                if new_operand.offset >= base_operand.offset + base_d_str.len() {
                    ops.push(
                        TextOperand::new_delete(new_operand.offset - base_d_str.len(), d_str)
                            .to_value(),
                    )
                } else if new_operand.offset + d_str.len() <= base_operand.offset {
                    ops.push(new.clone())
                } else {
                    let mut new_d = "";
                    if new_operand.offset < base_operand.offset {
                        new_d = &d_str[0..base_operand.offset - new_operand.offset]
                    }
                    if new_operand.offset + d_str.len() > base_operand.offset + base_d_str.len() {
                        new_d =
                            &d_str[base_operand.offset + base_d_str.len() - new_operand.offset..]
                    }

                    if !new_d.is_empty() {
                        let p = self.transform_position(new_operand.offset, &base_operand, false);
                        ops.push(TextOperand::new_delete(p, new_d.into()).to_value());
                    }
                }
            }
        }
        Ok(ops)
    }

    fn apply(&self, val: Option<&Value>, sub_type_operand: &Value) -> Result<Option<Value>> {
        let sub_operand: TextOperand = sub_type_operand.try_into()?;
        let p = sub_operand.offset;
        if let Some(v) = val {
            match v {
                Value::Null => {}
                Value::String(s) => {
                    if let Some(insert) = sub_operand.get_insert() {
                        if p <= s.len() {
                            return Ok(Some(Value::String(format!(
                                "{}{}{}",
                                &s[0..p],
                                insert,
                                &s[p..]
                            ))));
                        } else {
                            return Ok(Some(Value::String(format!("{}{}", s, insert))));
                        }
                    } else {
                        let to_delete = sub_operand.uncheck_get_delete();
                        let deleted = &s[p..to_delete.len()];
                        if !to_delete.eq(deleted) {
                            return Err(JsonError::InvalidOperation(
                                "text to delete in text operation is not match target text".into(),
                            ));
                        }

                        if p <= s.len() {
                            return Ok(Some(Value::String(format!(
                                "{}{}",
                                &s[0..p],
                                &s[p + to_delete.len()..]
                            ))));
                        } else {
                            return Ok(Some(v.clone()));
                        }
                    }
                }
                _ => {
                    return Err(JsonError::InvalidOperation(format!(
                        "can not apply text sub operation on value: {}",
                        v
                    )))
                }
            }
        }

        if let Some(insert) = sub_type_operand.get("i") {
            return Ok(Some(insert.clone()));
        }
        Ok(None)
    }

    fn validate_operand(&self, val: &Value) -> Result<()> {
        let p = val.get("p");
        if p.is_none() {
            return Err(JsonError::InvalidOperation(
                "text sub type operand does not contains Offset".into(),
            ));
        }

        if let Some(insert) = val.get("i") {
            if !insert.is_string() {
                return Err(JsonError::InvalidOperation(format!(
                    "text insert non-string value: {}",
                    insert
                )));
            }
        }

        if let Some(delete) = val.get("d") {
            if !delete.is_string() {
                return Err(JsonError::InvalidOperation(format!(
                    "text delete non-string value: {}",
                    delete
                )));
            }
        }
        Ok(())
    }
}