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
use {
crate::error::{DittoError, ErrorKind},
::serde::{de::DeserializeOwned, Deserialize, Serialize},
};
use core::ops::Deref;
#[derive(Serialize, Deserialize, Debug)]
#[serde(deny_unknown_fields)]
pub struct DittoRgaStoredFormat {
_value: Vec<serde_json::Value>,
#[serde(rename = "_ditto_internal_type_jkb12973t4b")]
_type: u64,
}
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, Default)]
#[serde(from = "Vec<serde_json::Value>")]
#[serde(into = "DittoRgaStoredFormat")]
pub struct DittoRga {
pub value: Vec<serde_json::Value>,
}
impl From<Vec<serde_json::Value>> for DittoRga {
fn from(value: Vec<serde_json::Value>) -> DittoRga {
DittoRga { value }
}
}
impl From<DittoRga> for DittoRgaStoredFormat {
fn from(DittoRga { value }: DittoRga) -> DittoRgaStoredFormat {
Self {
_value: value,
_type: ::ffi_sdk::DittoCrdtType::Rga as u64,
}
}
}
impl Deref for DittoRga {
type Target = Vec<serde_json::Value>;
fn deref(&self) -> &Self::Target {
&self.value
}
}
impl DittoRga {
#[deprecated(
note = "DittoRga usage should be replaced. Use arrays inside DittoRegisters instead"
)]
pub fn new() -> Self {
Self::default()
}
pub fn push<T: Serialize>(&'_ mut self, value: T) -> Result<&'_ mut Self, DittoError> {
self.value.push(serde_json::to_value(value)?);
Ok(self)
}
pub(super) fn pop(&mut self) {
self.value.pop();
}
pub(super) fn remove(&mut self, index: usize) -> Result<(), DittoError> {
if index >= self.value.len() {
return Err(DittoError::from_str(
ErrorKind::InvalidInput,
"Provided index to not exists",
));
}
self.value.remove(index);
Ok(())
}
pub(super) fn insert<T: Serialize>(
&mut self,
index: usize,
value: T,
) -> Result<(), DittoError> {
if index >= self.value.len() {
return Err(DittoError::from_str(
ErrorKind::InvalidInput,
"Provided index to not exists",
));
}
let value = serde_json::to_value(value)?;
self.value.insert(index, value);
Ok(())
}
pub fn get<T: DeserializeOwned>(&self, index: usize) -> Result<T, DittoError> {
self.value
.get(index)
.ok_or_else(|| {
DittoError::from_str(ErrorKind::InvalidInput, "Provided index to not exists")
})
.and_then(|v| serde_json::from_value(v.clone()).map_err(|json_err| json_err.into()))
}
}