y-rb 0.3.2-x64-mingw-ucrt
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/ext/yrb/Cargo.toml +21 -0
- data/ext/yrb/extconf.rb +6 -0
- data/ext/yrb/src/awareness.rs +425 -0
- data/ext/yrb/src/lib.rs +505 -0
- data/ext/yrb/src/utils.rs +34 -0
- data/ext/yrb/src/yany.rs +44 -0
- data/ext/yrb/src/yarray.rs +148 -0
- data/ext/yrb/src/yattrs.rs +48 -0
- data/ext/yrb/src/yawareness.rs +131 -0
- data/ext/yrb/src/ydoc.rs +35 -0
- data/ext/yrb/src/ymap.rs +151 -0
- data/ext/yrb/src/ytext.rs +204 -0
- data/ext/yrb/src/ytransaction.rs +55 -0
- data/ext/yrb/src/yvalue.rs +231 -0
- data/ext/yrb/src/yxml_element.rs +212 -0
- data/ext/yrb/src/yxml_text.rs +136 -0
- data/lib/3.1/yrb.so +0 -0
- data/lib/y/array.rb +354 -0
- data/lib/y/awareness.rb +266 -0
- data/lib/y/doc.rb +220 -0
- data/lib/y/map.rb +202 -0
- data/lib/y/text.rb +372 -0
- data/lib/y/transaction.rb +143 -0
- data/lib/y/version.rb +5 -0
- data/lib/y/xml.rb +870 -0
- data/lib/y-rb.rb +23 -0
- data/lib/y.rb +3 -0
- metadata +135 -0
@@ -0,0 +1,204 @@
|
|
1
|
+
use crate::yattrs::YAttrs;
|
2
|
+
use crate::yvalue::YValue;
|
3
|
+
use crate::YTransaction;
|
4
|
+
use lib0::any::Any;
|
5
|
+
use magnus::block::Proc;
|
6
|
+
use magnus::value::Qnil;
|
7
|
+
use magnus::{Error, RHash, Symbol, Value};
|
8
|
+
use std::cell::RefCell;
|
9
|
+
use yrs::types::Delta;
|
10
|
+
use yrs::Text;
|
11
|
+
|
12
|
+
#[magnus::wrap(class = "Y::Text")]
|
13
|
+
pub(crate) struct YText(pub(crate) RefCell<Text>);
|
14
|
+
|
15
|
+
/// SAFETY: This is safe because we only access this data when the GVL is held.
|
16
|
+
unsafe impl Send for YText {}
|
17
|
+
|
18
|
+
impl YText {
|
19
|
+
pub(crate) fn ytext_format(
|
20
|
+
&self,
|
21
|
+
transaction: &YTransaction,
|
22
|
+
index: u32,
|
23
|
+
length: u32,
|
24
|
+
attrs: RHash,
|
25
|
+
) -> Result<(), Error> {
|
26
|
+
let a = YAttrs::from(attrs);
|
27
|
+
self.0
|
28
|
+
.borrow_mut()
|
29
|
+
.format(&mut *transaction.0.borrow_mut(), index, length, a.0);
|
30
|
+
|
31
|
+
Ok(())
|
32
|
+
}
|
33
|
+
pub(crate) fn ytext_insert(
|
34
|
+
&self,
|
35
|
+
transaction: &YTransaction,
|
36
|
+
index: u32,
|
37
|
+
chunk: String,
|
38
|
+
) -> Result<(), Error> {
|
39
|
+
self.0
|
40
|
+
.borrow_mut()
|
41
|
+
.insert(&mut *transaction.0.borrow_mut(), index, &*chunk);
|
42
|
+
|
43
|
+
Ok(())
|
44
|
+
}
|
45
|
+
pub(crate) fn ytext_insert_embed(
|
46
|
+
&self,
|
47
|
+
transaction: &YTransaction,
|
48
|
+
index: u32,
|
49
|
+
content: Value,
|
50
|
+
) -> Result<(), Error> {
|
51
|
+
let yvalue = YValue::from(content);
|
52
|
+
let avalue = Any::from(yvalue);
|
53
|
+
|
54
|
+
self.0
|
55
|
+
.borrow_mut()
|
56
|
+
.insert_embed(&mut *transaction.0.borrow_mut(), index, avalue);
|
57
|
+
|
58
|
+
Ok(())
|
59
|
+
}
|
60
|
+
pub(crate) fn ytext_insert_embed_with_attributes(
|
61
|
+
&self,
|
62
|
+
transaction: &YTransaction,
|
63
|
+
index: u32,
|
64
|
+
embed: Value,
|
65
|
+
attrs: RHash,
|
66
|
+
) -> Result<(), Error> {
|
67
|
+
let yvalue = YValue::from(embed);
|
68
|
+
let avalue = Any::from(yvalue);
|
69
|
+
|
70
|
+
let a = YAttrs::from(attrs);
|
71
|
+
|
72
|
+
self.0.borrow_mut().insert_embed_with_attributes(
|
73
|
+
&mut *transaction.0.borrow_mut(),
|
74
|
+
index,
|
75
|
+
avalue,
|
76
|
+
a.0,
|
77
|
+
);
|
78
|
+
|
79
|
+
Ok(())
|
80
|
+
}
|
81
|
+
pub(crate) fn ytext_insert_with_attributes(
|
82
|
+
&self,
|
83
|
+
transaction: &YTransaction,
|
84
|
+
index: u32,
|
85
|
+
chunk: String,
|
86
|
+
attrs: RHash,
|
87
|
+
) -> Result<(), Error> {
|
88
|
+
let a = YAttrs::from(attrs);
|
89
|
+
|
90
|
+
self.0.borrow_mut().insert_with_attributes(
|
91
|
+
&mut *transaction.0.borrow_mut(),
|
92
|
+
index,
|
93
|
+
&*chunk,
|
94
|
+
a.0,
|
95
|
+
);
|
96
|
+
|
97
|
+
Ok(())
|
98
|
+
}
|
99
|
+
pub(crate) fn ytext_length(&self) -> u32 {
|
100
|
+
self.0.borrow().len()
|
101
|
+
}
|
102
|
+
pub(crate) fn ytext_observe(&self, block: Proc) -> Result<u32, Error> {
|
103
|
+
let delta_insert = Symbol::new("insert").to_static();
|
104
|
+
let delta_retain = Symbol::new("retain").to_static();
|
105
|
+
let delta_delete = Symbol::new("delete").to_static();
|
106
|
+
let attributes = Symbol::new("attributes").to_static();
|
107
|
+
|
108
|
+
// let mut error: Option<Error> = None;
|
109
|
+
|
110
|
+
let subscription_id = self
|
111
|
+
.0
|
112
|
+
.borrow_mut()
|
113
|
+
.observe(move |transaction, text_event| {
|
114
|
+
let delta = text_event.delta(transaction);
|
115
|
+
let (_, errors): (Vec<_>, Vec<_>) = delta
|
116
|
+
.iter()
|
117
|
+
.map(|change| match change {
|
118
|
+
Delta::Inserted(value, attrs) => {
|
119
|
+
let yvalue = YValue::from(value.clone());
|
120
|
+
let payload = RHash::new();
|
121
|
+
payload
|
122
|
+
.aset(delta_insert, yvalue.0.into_inner())
|
123
|
+
.map(|()| match attrs {
|
124
|
+
Some(a) => a
|
125
|
+
.clone()
|
126
|
+
.into_iter()
|
127
|
+
.map(|(key, val)| {
|
128
|
+
let yvalue = YValue::from(val);
|
129
|
+
(key.to_string(), yvalue.0.into_inner())
|
130
|
+
})
|
131
|
+
.collect::<RHash>()
|
132
|
+
.into(),
|
133
|
+
None => None,
|
134
|
+
})
|
135
|
+
.map(|attrs_hash| attrs_hash.map(|v| payload.aset(attributes, v)))
|
136
|
+
.map(|_| block.call::<(RHash,), Qnil>((payload,)))
|
137
|
+
}
|
138
|
+
Delta::Retain(index, attrs) => {
|
139
|
+
let payload = RHash::new();
|
140
|
+
|
141
|
+
let yvalue = YValue::from(*index);
|
142
|
+
|
143
|
+
payload
|
144
|
+
.aset(delta_retain, yvalue.0.into_inner())
|
145
|
+
.map(|()| match attrs {
|
146
|
+
Some(a) => a
|
147
|
+
.clone()
|
148
|
+
.into_iter()
|
149
|
+
.map(|(key, val)| {
|
150
|
+
let yvalue = YValue::from(val);
|
151
|
+
(key.to_string(), yvalue.0.into_inner())
|
152
|
+
})
|
153
|
+
.collect::<RHash>()
|
154
|
+
.into(),
|
155
|
+
None => None,
|
156
|
+
})
|
157
|
+
.map(|attrs_hash| attrs_hash.map(|v| payload.aset(attributes, v)))
|
158
|
+
.map(|_| block.call::<(RHash,), Qnil>((payload,)))
|
159
|
+
}
|
160
|
+
Delta::Deleted(index) => {
|
161
|
+
let payload = RHash::new();
|
162
|
+
|
163
|
+
let yvalue = YValue::from(*index);
|
164
|
+
|
165
|
+
payload
|
166
|
+
.aset(delta_delete, yvalue.0.into_inner())
|
167
|
+
.map(|()| block.call::<(RHash,), Qnil>((payload,)))
|
168
|
+
}
|
169
|
+
})
|
170
|
+
.partition(Result::is_ok);
|
171
|
+
|
172
|
+
if !errors.is_empty() {
|
173
|
+
// todo: make sure we respect errors and let the method fail by
|
174
|
+
// by returning a Result containing an Error
|
175
|
+
}
|
176
|
+
})
|
177
|
+
.into();
|
178
|
+
|
179
|
+
Ok(subscription_id)
|
180
|
+
}
|
181
|
+
pub(crate) fn ytext_push(&self, transaction: &YTransaction, chunk: String) {
|
182
|
+
self.0
|
183
|
+
.borrow_mut()
|
184
|
+
.push(&mut *transaction.0.borrow_mut(), &*chunk);
|
185
|
+
}
|
186
|
+
pub(crate) fn ytext_remove_range(
|
187
|
+
&self,
|
188
|
+
transaction: &YTransaction,
|
189
|
+
start: u32,
|
190
|
+
length: u32,
|
191
|
+
) -> Result<(), Error> {
|
192
|
+
self.0
|
193
|
+
.borrow_mut()
|
194
|
+
.remove_range(&mut *transaction.0.borrow_mut(), start, length);
|
195
|
+
|
196
|
+
Ok(())
|
197
|
+
}
|
198
|
+
pub(crate) fn ytext_to_s(&self) -> String {
|
199
|
+
return self.0.borrow().to_string();
|
200
|
+
}
|
201
|
+
pub(crate) fn ytext_unobserve(&self, subscription_id: u32) {
|
202
|
+
return self.0.borrow_mut().unobserve(subscription_id);
|
203
|
+
}
|
204
|
+
}
|
@@ -0,0 +1,55 @@
|
|
1
|
+
use crate::yarray::YArray;
|
2
|
+
use crate::ymap::YMap;
|
3
|
+
use crate::ytext::YText;
|
4
|
+
use crate::yxml_element::YXmlElement;
|
5
|
+
use crate::yxml_text::YXmlText;
|
6
|
+
use magnus::Error;
|
7
|
+
use std::cell::RefCell;
|
8
|
+
use yrs::updates::decoder::Decode;
|
9
|
+
use yrs::updates::encoder::Encode;
|
10
|
+
use yrs::{Transaction, Update};
|
11
|
+
|
12
|
+
#[magnus::wrap(class = "Y::Transaction")]
|
13
|
+
pub(crate) struct YTransaction(pub(crate) RefCell<Transaction>);
|
14
|
+
|
15
|
+
/// SAFETY: This is safe because we only access this data when the GVL is held.
|
16
|
+
unsafe impl Send for YTransaction {}
|
17
|
+
|
18
|
+
impl YTransaction {
|
19
|
+
pub(crate) fn ytransaction_apply_update(&self, update: Vec<u8>) -> Result<(), Error> {
|
20
|
+
return Update::decode_v1(update.as_slice())
|
21
|
+
.map(|u| self.0.borrow_mut().apply_update(u))
|
22
|
+
.map_err(|_e| Error::runtime_error("cannot apply update"));
|
23
|
+
}
|
24
|
+
pub(crate) fn ytransaction_commit(&self) {
|
25
|
+
self.0.borrow_mut().commit();
|
26
|
+
}
|
27
|
+
pub(crate) fn ytransaction_get_array(&self, name: String) -> YArray {
|
28
|
+
let a = self.0.borrow_mut().get_array(&*name);
|
29
|
+
|
30
|
+
YArray(RefCell::from(a))
|
31
|
+
}
|
32
|
+
pub(crate) fn ytransaction_get_map(&self, name: String) -> YMap {
|
33
|
+
let m = self.0.borrow_mut().get_map(&*name);
|
34
|
+
|
35
|
+
YMap(RefCell::from(m))
|
36
|
+
}
|
37
|
+
pub(crate) fn ytransaction_get_text(&self, name: String) -> YText {
|
38
|
+
let t = self.0.borrow_mut().get_text(&*name);
|
39
|
+
|
40
|
+
YText(RefCell::new(t))
|
41
|
+
}
|
42
|
+
pub(crate) fn ytransaction_get_xml_element(&self, name: String) -> YXmlElement {
|
43
|
+
let el = self.0.borrow_mut().get_xml_element(&*name);
|
44
|
+
|
45
|
+
YXmlElement(RefCell::new(el))
|
46
|
+
}
|
47
|
+
pub(crate) fn ytransaction_get_xml_text(&self, name: String) -> YXmlText {
|
48
|
+
let t = self.0.borrow_mut().get_xml_text(&*name);
|
49
|
+
|
50
|
+
YXmlText(RefCell::new(t))
|
51
|
+
}
|
52
|
+
pub(crate) fn ytransaction_state_vector(&self) -> Vec<u8> {
|
53
|
+
return self.0.borrow_mut().state_vector().encode_v1();
|
54
|
+
}
|
55
|
+
}
|
@@ -0,0 +1,231 @@
|
|
1
|
+
use crate::{YText, YXmlElement, YXmlText};
|
2
|
+
use lib0::any::Any;
|
3
|
+
use magnus::r_hash::ForEach::Continue;
|
4
|
+
use magnus::value::Qnil;
|
5
|
+
use magnus::{class, Float, Integer, RArray, RHash, RString, Symbol, Value, QNIL};
|
6
|
+
use std::cell::RefCell;
|
7
|
+
use std::collections::HashMap;
|
8
|
+
use yrs::types::Value as YrsValue;
|
9
|
+
use yrs::{Text as YrsText, XmlElement as YrsXmlElement, XmlText as YrsXmlText};
|
10
|
+
|
11
|
+
pub(crate) struct YValue(pub(crate) RefCell<Value>);
|
12
|
+
|
13
|
+
impl From<Value> for YValue {
|
14
|
+
fn from(value: Value) -> Self {
|
15
|
+
YValue(RefCell::from(value))
|
16
|
+
}
|
17
|
+
}
|
18
|
+
|
19
|
+
impl From<Qnil> for YValue {
|
20
|
+
fn from(value: Qnil) -> Self {
|
21
|
+
YValue(RefCell::from(Value::from(value)))
|
22
|
+
}
|
23
|
+
}
|
24
|
+
|
25
|
+
impl From<bool> for YValue {
|
26
|
+
fn from(value: bool) -> Self {
|
27
|
+
YValue(RefCell::from(Value::from(value)))
|
28
|
+
}
|
29
|
+
}
|
30
|
+
|
31
|
+
impl From<f64> for YValue {
|
32
|
+
fn from(value: f64) -> Self {
|
33
|
+
YValue(RefCell::from(Value::from(value)))
|
34
|
+
}
|
35
|
+
}
|
36
|
+
|
37
|
+
impl From<i64> for YValue {
|
38
|
+
fn from(value: i64) -> Self {
|
39
|
+
YValue(RefCell::from(Value::from(value)))
|
40
|
+
}
|
41
|
+
}
|
42
|
+
|
43
|
+
impl From<u32> for YValue {
|
44
|
+
fn from(value: u32) -> Self {
|
45
|
+
YValue(RefCell::from(Value::from(value)))
|
46
|
+
}
|
47
|
+
}
|
48
|
+
|
49
|
+
impl From<String> for YValue {
|
50
|
+
fn from(value: String) -> Self {
|
51
|
+
YValue(RefCell::from(Value::from(value)))
|
52
|
+
}
|
53
|
+
}
|
54
|
+
|
55
|
+
impl From<RArray> for YValue {
|
56
|
+
fn from(value: RArray) -> Self {
|
57
|
+
YValue(RefCell::from(Value::from(value)))
|
58
|
+
}
|
59
|
+
}
|
60
|
+
|
61
|
+
impl From<RHash> for YValue {
|
62
|
+
fn from(value: RHash) -> Self {
|
63
|
+
YValue(RefCell::from(Value::from(value)))
|
64
|
+
}
|
65
|
+
}
|
66
|
+
|
67
|
+
impl From<YrsText> for YValue {
|
68
|
+
fn from(value: YrsText) -> Self {
|
69
|
+
YValue(RefCell::from(Value::from(YText(RefCell::from(value)))))
|
70
|
+
}
|
71
|
+
}
|
72
|
+
|
73
|
+
impl From<YrsXmlElement> for YValue {
|
74
|
+
fn from(value: YrsXmlElement) -> Self {
|
75
|
+
YValue(RefCell::from(Value::from(YXmlElement(RefCell::from(
|
76
|
+
value,
|
77
|
+
)))))
|
78
|
+
}
|
79
|
+
}
|
80
|
+
|
81
|
+
impl From<YrsXmlText> for YValue {
|
82
|
+
fn from(value: YrsXmlText) -> Self {
|
83
|
+
YValue(RefCell::from(Value::from(YXmlText(RefCell::from(value)))))
|
84
|
+
}
|
85
|
+
}
|
86
|
+
|
87
|
+
impl From<YText> for YValue {
|
88
|
+
fn from(value: YText) -> Self {
|
89
|
+
YValue(RefCell::from(Value::from(value)))
|
90
|
+
}
|
91
|
+
}
|
92
|
+
|
93
|
+
impl From<YXmlElement> for YValue {
|
94
|
+
fn from(value: YXmlElement) -> Self {
|
95
|
+
YValue(RefCell::from(Value::from(value)))
|
96
|
+
}
|
97
|
+
}
|
98
|
+
|
99
|
+
impl From<YXmlText> for YValue {
|
100
|
+
fn from(value: YXmlText) -> Self {
|
101
|
+
YValue(RefCell::from(Value::from(value)))
|
102
|
+
}
|
103
|
+
}
|
104
|
+
|
105
|
+
impl From<Any> for YValue {
|
106
|
+
fn from(value: Any) -> Self {
|
107
|
+
match value {
|
108
|
+
Any::Null => YValue::from(QNIL),
|
109
|
+
Any::Undefined => YValue::from(QNIL),
|
110
|
+
Any::Bool(v) => YValue::from(v),
|
111
|
+
Any::Number(v) => YValue::from(v),
|
112
|
+
Any::BigInt(v) => YValue::from(v),
|
113
|
+
Any::String(v) => YValue::from(v.into_string()),
|
114
|
+
Any::Buffer(v) => YValue::from(Value::from(v.into_vec())),
|
115
|
+
Any::Array(v) => {
|
116
|
+
let arr = v
|
117
|
+
.iter()
|
118
|
+
.map(|i| YValue::from(i.clone()))
|
119
|
+
.map(|value| *value.0.borrow())
|
120
|
+
.collect::<Vec<Value>>();
|
121
|
+
YValue::from(RArray::from_vec(arr))
|
122
|
+
}
|
123
|
+
Any::Map(v) => {
|
124
|
+
let map = v
|
125
|
+
.iter()
|
126
|
+
.map(|(key, val)| {
|
127
|
+
let v = val.clone();
|
128
|
+
(key.to_string(), YValue::from(v).into())
|
129
|
+
})
|
130
|
+
.collect::<HashMap<String, Value>>();
|
131
|
+
YValue::from(RHash::from_iter(map))
|
132
|
+
}
|
133
|
+
}
|
134
|
+
}
|
135
|
+
}
|
136
|
+
|
137
|
+
impl From<YrsValue> for YValue {
|
138
|
+
fn from(value: YrsValue) -> Self {
|
139
|
+
match value {
|
140
|
+
YrsValue::Any(val) => YValue::from(val),
|
141
|
+
YrsValue::YText(text) => YValue::from(text),
|
142
|
+
YrsValue::YXmlElement(el) => YValue::from(el),
|
143
|
+
YrsValue::YXmlText(text) => YValue::from(text),
|
144
|
+
// YrsValue::YArray(val) => YValue::from(RArray::from_vec(val.iter().map(|item| {
|
145
|
+
// let yvalue = YValue::from(item);
|
146
|
+
// *yvalue.0
|
147
|
+
// }))),
|
148
|
+
// YrsValue::YMap(val) => YValue::from(RHash::from_iter(val.iter())),
|
149
|
+
v => panic!("cannot map complex yrs values to yvalue: {}", v.to_string()),
|
150
|
+
}
|
151
|
+
}
|
152
|
+
}
|
153
|
+
|
154
|
+
impl From<YValue> for Any {
|
155
|
+
fn from(val: YValue) -> Self {
|
156
|
+
let value = val.0.into_inner();
|
157
|
+
if value.is_nil() {
|
158
|
+
Any::Null
|
159
|
+
} else if value.is_kind_of(class::float()) {
|
160
|
+
let f = Float::from_value(value).unwrap();
|
161
|
+
Any::Number(f.to_f64())
|
162
|
+
} else if value.is_kind_of(class::integer()) {
|
163
|
+
let i = Integer::from_value(value).unwrap();
|
164
|
+
Any::BigInt(i.to_i64().unwrap())
|
165
|
+
} else if value.is_kind_of(class::symbol()) {
|
166
|
+
let s = Symbol::from_value(value).unwrap();
|
167
|
+
Any::String(Box::from(s.name().unwrap()))
|
168
|
+
} else if value.is_kind_of(class::true_class()) {
|
169
|
+
Any::Bool(true)
|
170
|
+
} else if value.is_kind_of(class::false_class()) {
|
171
|
+
Any::Bool(false)
|
172
|
+
} else if value.is_kind_of(class::string()) {
|
173
|
+
let s = RString::from_value(value).unwrap();
|
174
|
+
unsafe { Any::String(Box::from(s.as_str().unwrap().to_string())) }
|
175
|
+
} else if value.is_kind_of(class::array()) {
|
176
|
+
let arr = RArray::from_value(value).unwrap();
|
177
|
+
let items = arr
|
178
|
+
.each()
|
179
|
+
.map(|item| {
|
180
|
+
let yvalue = YValue::from(item.unwrap());
|
181
|
+
Any::from(yvalue)
|
182
|
+
})
|
183
|
+
.collect::<Vec<Any>>();
|
184
|
+
Any::Array(Box::from(items))
|
185
|
+
} else if value.is_kind_of(class::hash()) {
|
186
|
+
let map = RHash::from_value(value).unwrap();
|
187
|
+
let mut m: HashMap<String, Any> = HashMap::new();
|
188
|
+
|
189
|
+
// we need to map symbol keys to strings, because we can't store
|
190
|
+
// symbols in any of the yrs data structures
|
191
|
+
map.foreach(|key: Value, val: Value| {
|
192
|
+
let k = if let Some(converted_key) = Symbol::from_value(key) {
|
193
|
+
converted_key.to_string()
|
194
|
+
} else {
|
195
|
+
let converted_key = RString::from_value(key).unwrap();
|
196
|
+
let result = converted_key.to_string();
|
197
|
+
result.unwrap()
|
198
|
+
};
|
199
|
+
m.insert(k, Any::from(YValue::from(val)));
|
200
|
+
Ok(Continue)
|
201
|
+
})
|
202
|
+
.expect("cannot map key/value pair");
|
203
|
+
|
204
|
+
Any::Map(Box::from(m))
|
205
|
+
} else {
|
206
|
+
Any::Undefined
|
207
|
+
}
|
208
|
+
}
|
209
|
+
}
|
210
|
+
|
211
|
+
#[allow(clippy::from_over_into)]
|
212
|
+
impl Into<Value> for YValue {
|
213
|
+
fn into(self) -> Value {
|
214
|
+
self.0.into_inner()
|
215
|
+
}
|
216
|
+
}
|
217
|
+
|
218
|
+
#[cfg(test)]
|
219
|
+
mod tests {
|
220
|
+
use crate::yvalue::YValue;
|
221
|
+
use lib0::any::Any;
|
222
|
+
|
223
|
+
#[test]
|
224
|
+
fn convert_any_to_yvalue() {
|
225
|
+
let _cleanup = unsafe { magnus::embed::init() };
|
226
|
+
let value = Any::Null;
|
227
|
+
let yvalue: YValue = value.into();
|
228
|
+
|
229
|
+
assert!(yvalue.0.into_inner().is_nil());
|
230
|
+
}
|
231
|
+
}
|
@@ -0,0 +1,212 @@
|
|
1
|
+
use crate::yvalue::YValue;
|
2
|
+
use crate::yxml_text::YXmlText;
|
3
|
+
use crate::YTransaction;
|
4
|
+
use magnus::block::Proc;
|
5
|
+
use magnus::{Error, RArray, RHash, Symbol, Value};
|
6
|
+
use std::cell::RefCell;
|
7
|
+
use yrs::types::Change;
|
8
|
+
use yrs::{Xml, XmlElement};
|
9
|
+
|
10
|
+
#[magnus::wrap(class = "Y::XMLElement")]
|
11
|
+
pub(crate) struct YXmlElement(pub(crate) RefCell<XmlElement>);
|
12
|
+
|
13
|
+
/// SAFETY: This is safe because we only access this data when the GVL is held.
|
14
|
+
unsafe impl Send for YXmlElement {}
|
15
|
+
|
16
|
+
impl YXmlElement {
|
17
|
+
pub(crate) fn yxml_element_attributes(&self) -> RHash {
|
18
|
+
RHash::from_iter(self.0.borrow().attributes())
|
19
|
+
}
|
20
|
+
pub(crate) fn yxml_element_first_child(&self) -> Option<Value> {
|
21
|
+
self.yxml_element_get(0)
|
22
|
+
}
|
23
|
+
pub(crate) fn yxml_element_get(&self, index: u32) -> Option<Value> {
|
24
|
+
self.0.borrow().get(index).map(|node| match node {
|
25
|
+
Xml::Element(el) => Value::from(YXmlElement(RefCell::from(el))),
|
26
|
+
Xml::Text(text) => Value::from(YXmlText(RefCell::from(text))),
|
27
|
+
})
|
28
|
+
}
|
29
|
+
pub(crate) fn yxml_element_get_attribute(&self, name: String) -> Option<String> {
|
30
|
+
self.0.borrow().get_attribute(&*name)
|
31
|
+
}
|
32
|
+
pub(crate) fn yxml_element_insert_attribute(
|
33
|
+
&self,
|
34
|
+
transaction: &YTransaction,
|
35
|
+
name: String,
|
36
|
+
value: String,
|
37
|
+
) {
|
38
|
+
self.0
|
39
|
+
.borrow_mut()
|
40
|
+
.insert_attribute(&mut *transaction.0.borrow_mut(), name, value);
|
41
|
+
}
|
42
|
+
pub(crate) fn yxml_element_insert_element(
|
43
|
+
&self,
|
44
|
+
transaction: &YTransaction,
|
45
|
+
index: u32,
|
46
|
+
name: String,
|
47
|
+
) -> YXmlElement {
|
48
|
+
let element =
|
49
|
+
self.0
|
50
|
+
.borrow_mut()
|
51
|
+
.insert_elem(&mut *transaction.0.borrow_mut(), index, name);
|
52
|
+
|
53
|
+
YXmlElement(RefCell::from(element))
|
54
|
+
}
|
55
|
+
pub(crate) fn yxml_element_insert_text(
|
56
|
+
&self,
|
57
|
+
transaction: &YTransaction,
|
58
|
+
index: u32,
|
59
|
+
) -> YXmlText {
|
60
|
+
let text = self
|
61
|
+
.0
|
62
|
+
.borrow_mut()
|
63
|
+
.insert_text(&mut *transaction.0.borrow_mut(), index);
|
64
|
+
|
65
|
+
YXmlText(RefCell::from(text))
|
66
|
+
}
|
67
|
+
pub(crate) fn yxml_element_next_sibling(&self) -> Option<Value> {
|
68
|
+
self.0.borrow().next_sibling().map(|item| match item {
|
69
|
+
Xml::Element(el) => Value::from(YXmlElement(RefCell::from(el))),
|
70
|
+
Xml::Text(text) => Value::from(YXmlText(RefCell::from(text))),
|
71
|
+
})
|
72
|
+
}
|
73
|
+
pub(crate) fn yxml_element_observe(&self, block: Proc) -> Result<u32, Error> {
|
74
|
+
let change_added = Symbol::new("added").to_static();
|
75
|
+
let change_retain = Symbol::new("retain").to_static();
|
76
|
+
let change_removed = Symbol::new("removed").to_static();
|
77
|
+
|
78
|
+
let subscription_id = self
|
79
|
+
.0
|
80
|
+
.borrow_mut()
|
81
|
+
.observe(move |transaction, xml_element_event| {
|
82
|
+
let delta = xml_element_event.delta(transaction);
|
83
|
+
let changes = RArray::with_capacity(delta.len());
|
84
|
+
|
85
|
+
for change in delta {
|
86
|
+
match change {
|
87
|
+
Change::Added(v) => {
|
88
|
+
let values = v
|
89
|
+
.iter()
|
90
|
+
.map(|o| YValue::from(o.clone()))
|
91
|
+
.map(|o| *o.0.borrow())
|
92
|
+
.collect::<Vec<_>>();
|
93
|
+
|
94
|
+
let payload = RHash::new();
|
95
|
+
payload
|
96
|
+
.aset(change_added, RArray::from_vec(values))
|
97
|
+
.expect("cannot create change::added payload");
|
98
|
+
|
99
|
+
changes
|
100
|
+
.push(payload)
|
101
|
+
.expect("cannot push payload to list of changes");
|
102
|
+
}
|
103
|
+
Change::Retain(position) => {
|
104
|
+
let payload = RHash::new();
|
105
|
+
payload
|
106
|
+
.aset(change_retain, *position)
|
107
|
+
.expect("cannot create change::retain payload");
|
108
|
+
|
109
|
+
changes
|
110
|
+
.push(payload)
|
111
|
+
.expect("cannot push payload to list of changes");
|
112
|
+
}
|
113
|
+
Change::Removed(position) => {
|
114
|
+
let payload = RHash::new();
|
115
|
+
payload
|
116
|
+
.aset(change_removed, *position)
|
117
|
+
.expect("cannot create change::removed payload");
|
118
|
+
|
119
|
+
changes
|
120
|
+
.push(payload)
|
121
|
+
.expect("cannot push payload to list of changes");
|
122
|
+
}
|
123
|
+
}
|
124
|
+
}
|
125
|
+
|
126
|
+
block
|
127
|
+
.call::<(RArray,), Value>((changes,))
|
128
|
+
.expect("cannot call block");
|
129
|
+
});
|
130
|
+
|
131
|
+
Ok(subscription_id.into())
|
132
|
+
}
|
133
|
+
pub(crate) fn yxml_element_parent(&self) -> Option<Value> {
|
134
|
+
self.0
|
135
|
+
.borrow()
|
136
|
+
.parent()
|
137
|
+
.map(|item| Value::from(YXmlElement(RefCell::from(item))))
|
138
|
+
}
|
139
|
+
pub(crate) fn yxml_element_prev_sibling(&self) -> Option<Value> {
|
140
|
+
self.0.borrow().prev_sibling().map(|item| match item {
|
141
|
+
Xml::Element(el) => Value::from(YXmlElement(RefCell::from(el))),
|
142
|
+
Xml::Text(text) => Value::from(YXmlText(RefCell::from(text))),
|
143
|
+
})
|
144
|
+
}
|
145
|
+
pub(crate) fn yxml_element_push_element_back(
|
146
|
+
&self,
|
147
|
+
transaction: &YTransaction,
|
148
|
+
name: String,
|
149
|
+
) -> YXmlElement {
|
150
|
+
let xml_element = self
|
151
|
+
.0
|
152
|
+
.borrow_mut()
|
153
|
+
.push_elem_back(&mut *transaction.0.borrow_mut(), name);
|
154
|
+
|
155
|
+
YXmlElement(RefCell::from(xml_element))
|
156
|
+
}
|
157
|
+
pub(crate) fn yxml_element_push_element_front(
|
158
|
+
&self,
|
159
|
+
transaction: &YTransaction,
|
160
|
+
name: String,
|
161
|
+
) -> YXmlElement {
|
162
|
+
let xml_element = self
|
163
|
+
.0
|
164
|
+
.borrow_mut()
|
165
|
+
.push_elem_front(&mut *transaction.0.borrow_mut(), name);
|
166
|
+
|
167
|
+
YXmlElement(RefCell::from(xml_element))
|
168
|
+
}
|
169
|
+
pub(crate) fn yxml_element_push_text_back(&self, transaction: &YTransaction) -> YXmlText {
|
170
|
+
let xml_text = self
|
171
|
+
.0
|
172
|
+
.borrow_mut()
|
173
|
+
.push_text_back(&mut *transaction.0.borrow_mut());
|
174
|
+
|
175
|
+
YXmlText(RefCell::from(xml_text))
|
176
|
+
}
|
177
|
+
pub(crate) fn yxml_element_push_text_front(&self, transaction: &YTransaction) -> YXmlText {
|
178
|
+
let xml_text = self
|
179
|
+
.0
|
180
|
+
.borrow_mut()
|
181
|
+
.push_text_front(&mut *transaction.0.borrow_mut());
|
182
|
+
|
183
|
+
YXmlText(RefCell::from(xml_text))
|
184
|
+
}
|
185
|
+
pub(crate) fn yxml_element_remove_attribute(&self, transaction: &YTransaction, name: String) {
|
186
|
+
self.0
|
187
|
+
.borrow_mut()
|
188
|
+
.remove_attribute::<&str>(&mut *transaction.0.borrow_mut(), &name.as_str());
|
189
|
+
}
|
190
|
+
pub(crate) fn yxml_element_remove_range(
|
191
|
+
&self,
|
192
|
+
transaction: &YTransaction,
|
193
|
+
index: u32,
|
194
|
+
length: u32,
|
195
|
+
) {
|
196
|
+
self.0
|
197
|
+
.borrow_mut()
|
198
|
+
.remove_range(&mut *transaction.0.borrow_mut(), index, length);
|
199
|
+
}
|
200
|
+
pub(crate) fn yxml_element_size(&self) -> u32 {
|
201
|
+
self.0.borrow().len()
|
202
|
+
}
|
203
|
+
pub(crate) fn yxml_element_tag(&self) -> String {
|
204
|
+
self.0.borrow().tag().to_string()
|
205
|
+
}
|
206
|
+
pub(crate) fn yxml_element_to_s(&self) -> String {
|
207
|
+
self.0.borrow().to_string()
|
208
|
+
}
|
209
|
+
pub(crate) fn yxml_element_unobserve(&self, subscription_id: u32) {
|
210
|
+
self.0.borrow_mut().unobserve(subscription_id);
|
211
|
+
}
|
212
|
+
}
|