Skip to content

Commit 1f79ef2

Browse files
committed
Remove pub from inner fields of Map, NodeArray, and NodeTuple
This encapsulates the internal data structures by making inner fields private and exposing public methods for access. Changes include: - Remove pub from Map<K, V> inner IndexMap field - Remove pub from NodeArray inner Vec<NodeId> field - Remove pub from NodeTuple inner Vec<NodeId> field - Add from_vec() methods to NodeArray and NodeTuple for construction - Add #[plural(...)] attributes to generate len, is_empty, iter methods - Update all usages to use public methods instead of direct field access Resolves TODO comments in map.rs and node.rs about removing pub fields.
1 parent e0f6989 commit 1f79ef2

File tree

8 files changed

+88
-86
lines changed

8 files changed

+88
-86
lines changed

crates/eure-document/src/data_model.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -93,18 +93,16 @@ impl VariantRepr {
9393
Some(VariantRepr::Untagged)
9494
}
9595
NodeValue::Map(map) => {
96-
let tag =
97-
map.0
98-
.get(&ObjectKey::String("tag".to_string()))
99-
.and_then(|&id| match &doc.node(id).content {
100-
NodeValue::Primitive(PrimitiveValue::Text(t)) => {
101-
Some(t.as_str().to_string())
102-
}
103-
_ => None,
104-
});
96+
let tag = map
97+
.get(&ObjectKey::String("tag".to_string()))
98+
.and_then(|&id| match &doc.node(id).content {
99+
NodeValue::Primitive(PrimitiveValue::Text(t)) => {
100+
Some(t.as_str().to_string())
101+
}
102+
_ => None,
103+
});
105104

106105
let content = map
107-
.0
108106
.get(&ObjectKey::String("content".to_string()))
109107
.and_then(|&id| match &doc.node(id).content {
110108
NodeValue::Primitive(PrimitiveValue::Text(t)) => {

crates/eure-document/src/document.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,11 @@ impl EureDocument {
100100
}
101101

102102
fn node_arrays_equal(&self, arr1: &NodeArray, other: &EureDocument, arr2: &NodeArray) -> bool {
103-
if arr1.0.len() != arr2.0.len() {
103+
if arr1.len() != arr2.len() {
104104
return false;
105105
}
106106

107-
for (child_id1, child_id2) in arr1.0.iter().zip(arr2.0.iter()) {
107+
for (child_id1, child_id2) in arr1.iter().zip(arr2.iter()) {
108108
if !self.nodes_equal(*child_id1, other, *child_id2) {
109109
return false;
110110
}
@@ -114,11 +114,11 @@ impl EureDocument {
114114
}
115115

116116
fn node_tuples_equal(&self, tup1: &NodeTuple, other: &EureDocument, tup2: &NodeTuple) -> bool {
117-
if tup1.0.len() != tup2.0.len() {
117+
if tup1.len() != tup2.len() {
118118
return false;
119119
}
120120

121-
for (child_id1, child_id2) in tup1.0.iter().zip(tup2.0.iter()) {
121+
for (child_id1, child_id2) in tup1.iter().zip(tup2.iter()) {
122122
if !self.nodes_equal(*child_id1, other, *child_id2) {
123123
return false;
124124
}
@@ -128,12 +128,12 @@ impl EureDocument {
128128
}
129129

130130
fn node_maps_equal(&self, map1: &NodeMap, other: &EureDocument, map2: &NodeMap) -> bool {
131-
if map1.0.len() != map2.0.len() {
131+
if map1.len() != map2.len() {
132132
return false;
133133
}
134134

135-
for (key1, &child_id1) in &map1.0 {
136-
match map2.0.get(key1) {
135+
for (key1, &child_id1) in map1.iter() {
136+
match map2.get(key1) {
137137
Some(&child_id2) => {
138138
if !self.nodes_equal(child_id1, other, child_id2) {
139139
return false;
@@ -515,7 +515,7 @@ mod tests {
515515
.node_id;
516516

517517
let tuple = doc.node(tuple_id).as_tuple().expect("Expected tuple");
518-
assert_eq!(tuple.0, vec![node_id]);
518+
assert_eq!(tuple.to_vec(), vec![node_id]);
519519
}
520520

521521
#[test]
@@ -537,7 +537,7 @@ mod tests {
537537
.node_id;
538538

539539
let tuple = doc.node(tuple_id).as_tuple().expect("Expected tuple");
540-
assert_eq!(tuple.0, vec![node_id1, node_id2]);
540+
assert_eq!(tuple.to_vec(), vec![node_id1, node_id2]);
541541
}
542542

543543
#[test]
@@ -584,7 +584,7 @@ mod tests {
584584
.node_id;
585585

586586
let array = doc.node(array_id).as_array().expect("Expected array");
587-
assert_eq!(array.0, vec![node_id]);
587+
assert_eq!(array.to_vec(), vec![node_id]);
588588
}
589589

590590
#[test]
@@ -606,7 +606,7 @@ mod tests {
606606
.node_id;
607607

608608
let array = doc.node(array_id).as_array().expect("Expected array");
609-
assert_eq!(array.0, vec![node_id1, node_id2]);
609+
assert_eq!(array.to_vec(), vec![node_id1, node_id2]);
610610
}
611611

612612
#[test]
@@ -695,7 +695,7 @@ mod tests {
695695
assert!(result.is_ok());
696696

697697
let tuple = doc.node(tuple_id).as_tuple().expect("Expected tuple");
698-
assert_eq!(tuple.0.len(), 1);
698+
assert_eq!(tuple.len(), 1);
699699
}
700700

701701
#[test]
@@ -711,7 +711,7 @@ mod tests {
711711
assert!(result.is_ok());
712712

713713
let array = doc.node(array_id).as_array().expect("Expected array");
714-
assert_eq!(array.0.len(), 1);
714+
assert_eq!(array.len(), 1);
715715
}
716716

717717
#[test]
@@ -727,7 +727,7 @@ mod tests {
727727
assert!(result.is_ok());
728728

729729
let array = doc.node(array_id).as_array().expect("Expected array");
730-
assert_eq!(array.0.len(), 1);
730+
assert_eq!(array.len(), 1);
731731
}
732732

733733
#[test]
@@ -855,9 +855,9 @@ mod tests {
855855

856856
// Verify both nodes exist in array
857857
let array = doc.node(parent_id).as_array().expect("Expected array");
858-
assert_eq!(array.0.len(), 2);
859-
assert_eq!(array.0[0], node_id1);
860-
assert_eq!(array.0[1], node_id2);
858+
assert_eq!(array.len(), 2);
859+
assert_eq!(array.get(0).unwrap(), node_id1);
860+
assert_eq!(array.get(1).unwrap(), node_id2);
861861
}
862862

863863
#[test]

crates/eure-document/src/document/node.rs

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -227,13 +227,13 @@ impl From<PrimitiveValue> for NodeValue {
227227
}
228228
}
229229

230-
// TODO: Remove `pub`
231230
#[derive(Debug, Default, Clone, PartialEq, Eq, Plural)]
232-
pub struct NodeArray(pub Vec<NodeId>);
231+
#[plural(len, is_empty, iter, into_iter, new)]
232+
pub struct NodeArray(Vec<NodeId>);
233233

234-
// TODO: Remove `pub`
235234
#[derive(Debug, Default, Clone, PartialEq, Eq, Hash, Plural)]
236-
pub struct NodeTuple(pub Vec<NodeId>);
235+
#[plural(len, is_empty, iter, into_iter, new)]
236+
pub struct NodeTuple(Vec<NodeId>);
237237

238238
pub type NodeMap = Map<ObjectKey, NodeId>;
239239

@@ -257,6 +257,14 @@ impl NodeTuple {
257257
self.0.insert(index as usize, node_id);
258258
Ok(())
259259
}
260+
261+
pub fn to_vec(&self) -> Vec<NodeId> {
262+
self.0.clone()
263+
}
264+
265+
pub fn from_vec(vec: Vec<NodeId>) -> Self {
266+
Self(vec)
267+
}
260268
}
261269

262270
impl NodeArray {
@@ -279,6 +287,14 @@ impl NodeArray {
279287
self.0.insert(index, node_id);
280288
Ok(())
281289
}
290+
291+
pub fn to_vec(&self) -> Vec<NodeId> {
292+
self.0.clone()
293+
}
294+
295+
pub fn from_vec(vec: Vec<NodeId>) -> Self {
296+
Self(vec)
297+
}
282298
}
283299

284300
#[cfg(test)]
@@ -297,7 +313,7 @@ mod tests {
297313
};
298314

299315
let map = node.require_map().expect("Should convert to map");
300-
assert_eq!(map.0.len(), 0);
316+
assert_eq!(map.len(), 0);
301317

302318
// Verify content was changed
303319
assert!(node.as_map().is_some());
@@ -311,7 +327,7 @@ mod tests {
311327
};
312328

313329
let map = node.require_map().expect("Should return existing map");
314-
assert_eq!(map.0.len(), 0);
330+
assert_eq!(map.len(), 0);
315331
}
316332

317333
#[test]
@@ -333,7 +349,7 @@ mod tests {
333349
};
334350

335351
let tuple = node.require_tuple().expect("Should convert to tuple");
336-
assert_eq!(tuple.0.len(), 0);
352+
assert_eq!(tuple.len(), 0);
337353

338354
// Verify content was changed
339355
assert!(node.as_tuple().is_some());
@@ -347,7 +363,7 @@ mod tests {
347363
};
348364

349365
let tuple = node.require_tuple().expect("Should return existing tuple");
350-
assert_eq!(tuple.0.len(), 0);
366+
assert_eq!(tuple.len(), 0);
351367
}
352368

353369
#[test]
@@ -369,7 +385,7 @@ mod tests {
369385
};
370386

371387
let array = node.require_array().expect("Should convert to array");
372-
assert_eq!(array.0.len(), 0);
388+
assert_eq!(array.len(), 0);
373389

374390
// Verify content was changed
375391
assert!(node.as_array().is_some());
@@ -383,7 +399,7 @@ mod tests {
383399
};
384400

385401
let array = node.require_array().expect("Should return existing array");
386-
assert_eq!(array.0.len(), 0);
402+
assert_eq!(array.len(), 0);
387403
}
388404

389405
#[test]

crates/eure-document/src/map.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ use indexmap::IndexMap;
22

33
use crate::prelude_internal::*;
44

5-
// TODO: Remove `pub`
65
#[derive(Debug, Clone, Plural)]
76
#[plural(len, is_empty, iter, into_iter, into_iter_ref, new)]
8-
pub struct Map<K, V>(pub IndexMap<K, V>);
7+
pub struct Map<K, V>(IndexMap<K, V>);
98

109
impl<K: Eq + std::hash::Hash, V: Eq> Eq for Map<K, V> {}
1110
impl<K: Eq + std::hash::Hash, V: PartialEq> PartialEq for Map<K, V> {

crates/eure-json/src/lib.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,21 +77,21 @@ fn convert_node(
7777
NodeValue::Primitive(prim) => convert_primitive(prim),
7878
NodeValue::Array(arr) => {
7979
let mut result = Vec::new();
80-
for &child_id in &arr.0 {
80+
for &child_id in arr.iter() {
8181
result.push(convert_node(doc, child_id, config)?);
8282
}
8383
Ok(JsonValue::Array(result))
8484
}
8585
NodeValue::Tuple(tuple) => {
8686
let mut result = Vec::new();
87-
for &child_id in &tuple.0 {
87+
for &child_id in tuple.iter() {
8888
result.push(convert_node(doc, child_id, config)?);
8989
}
9090
Ok(JsonValue::Array(result))
9191
}
9292
NodeValue::Map(map) => {
9393
let mut result = serde_json::Map::new();
94-
for (key, &child_id) in &map.0 {
94+
for (key, &child_id) in map.iter() {
9595
let key_string = convert_object_key(key)?;
9696
let value = convert_node(doc, child_id, config)?;
9797
result.insert(key_string, value);
@@ -212,21 +212,21 @@ fn convert_node_content_only(
212212
NodeValue::Primitive(prim) => convert_primitive(prim),
213213
NodeValue::Array(arr) => {
214214
let mut result = Vec::new();
215-
for &child_id in &arr.0 {
215+
for &child_id in arr.iter() {
216216
result.push(convert_node(doc, child_id, config)?);
217217
}
218218
Ok(JsonValue::Array(result))
219219
}
220220
NodeValue::Tuple(tuple) => {
221221
let mut result = Vec::new();
222-
for &child_id in &tuple.0 {
222+
for &child_id in tuple.iter() {
223223
result.push(convert_node(doc, child_id, config)?);
224224
}
225225
Ok(JsonValue::Array(result))
226226
}
227227
NodeValue::Map(map) => {
228228
let mut result = serde_json::Map::new();
229-
for (key, &child_id) in &map.0 {
229+
for (key, &child_id) in map.iter() {
230230
let key_string = convert_object_key(key)?;
231231
let value = convert_node(doc, child_id, config)?;
232232
result.insert(key_string, value);
@@ -312,7 +312,7 @@ fn convert_json_to_node(doc: &mut EureDocument, node_id: NodeId, value: &JsonVal
312312
let child_id = doc.create_node(NodeValue::hole());
313313
convert_json_to_node(doc, child_id, item);
314314
if let NodeValue::Array(ref mut array) = doc.node_mut(node_id).content {
315-
array.0.push(child_id);
315+
let _ = array.push(child_id);
316316
}
317317
}
318318
}
@@ -322,7 +322,7 @@ fn convert_json_to_node(doc: &mut EureDocument, node_id: NodeId, value: &JsonVal
322322
let child_id = doc.create_node(NodeValue::hole());
323323
convert_json_to_node(doc, child_id, val);
324324
if let NodeValue::Map(ref mut map) = doc.node_mut(node_id).content {
325-
map.0.insert(ObjectKey::String(key.clone()), child_id);
325+
map.insert(ObjectKey::String(key.clone()), child_id);
326326
}
327327
}
328328
}

0 commit comments

Comments
 (0)