This repository was archived by the owner on Nov 18, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathflat_node.rs
183 lines (165 loc) · 5.78 KB
/
flat_node.rs
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
use std::collections::HashMap;
use deskc_ids::NodeId;
use dson::Dson;
use ty::Type;
use crate::{
content::Content,
patch::{AttributePatch, ContentPatch, OperandPatch, OperandPosition},
rules::{NodeOperation, Rules},
};
pub type Operands = Vec<NodeId>;
pub type Attributes = HashMap<Type, Dson>;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct FlatNode {
pub content: Content,
pub operands: Operands,
pub attributes: Attributes,
pub rules: Rules<NodeOperation>,
pub operand_rules: Rules<NodeOperation>,
}
impl FlatNode {
pub fn new(content: Content) -> Self {
Self {
content,
operands: Operands::default(),
attributes: Attributes::default(),
rules: Rules::default(),
operand_rules: Rules::default(),
}
}
pub fn patch_content(&mut self, patch: &ContentPatch) {
match patch {
ContentPatch::Replace(content) => self.content = content.clone(),
_ => todo!(),
}
}
pub fn patch_attribute(&mut self, patch: &AttributePatch) {
match patch {
AttributePatch::Update { key, value } => {
self.attributes.insert(key.clone(), value.clone());
}
AttributePatch::Remove { key } => {
self.attributes.remove(key);
}
}
}
pub fn patch_children(&mut self, patch: &OperandPatch) {
fn find_index(operands: &Operands, node_id: &NodeId) -> usize {
operands.iter().position(|&id| id == *node_id).unwrap()
}
fn get_index(operands: &Operands, position: &OperandPosition) -> usize {
match position {
OperandPosition::At(index) => *index,
OperandPosition::First => 0,
OperandPosition::Last => operands.len(),
OperandPosition::Before(node_id) => find_index(operands, node_id),
OperandPosition::After(node_id) => find_index(operands, node_id) + 1,
}
}
match patch {
OperandPatch::Insert { node_id, position } => {
let index = get_index(&self.operands, position);
self.operands.insert(index, *node_id);
}
OperandPatch::Remove { node_id } => {
let index = self.operands.iter().position(|&id| id == *node_id).unwrap();
self.operands.remove(index);
}
OperandPatch::Move { node_id, position } => {
let origin_index = find_index(&self.operands, node_id);
let node = self.operands.remove(origin_index);
let target = get_index(&self.operands, position);
self.operands.insert(target, node);
}
}
}
pub fn rules(mut self, rules: Rules<NodeOperation>) -> Self {
self.rules = rules;
self
}
pub fn operand_rules(mut self, rules: Rules<NodeOperation>) -> Self {
self.operand_rules = rules;
self
}
pub fn operands(mut self, operands: Operands) -> Self {
self.operands = operands;
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn update() {
let mut flat_node = FlatNode::new(Content::String("a".into()));
flat_node.patch_attribute(&AttributePatch::Update {
key: Type::Real,
value: 1.into(),
});
assert_eq!(flat_node.attributes.get(&Type::Real), Some(&1.into()));
}
#[test]
fn remove() {
let mut flat_node = FlatNode::new(Content::String("a".into()));
flat_node.patch_attribute(&AttributePatch::Update {
key: Type::Real,
value: 1.into(),
});
flat_node.patch_attribute(&AttributePatch::Remove { key: Type::Real });
assert_eq!(flat_node.attributes.get(&Type::Real), None,);
}
#[test]
fn operands_insert() {
let mut flat_node = FlatNode::new(Content::String("a".into()));
let a = NodeId::new();
flat_node.patch_children(&OperandPatch::Insert {
node_id: a.clone(),
position: OperandPosition::First,
});
assert_eq!(flat_node.operands, vec![a]);
}
#[test]
fn operands_remove() {
let mut flat_node = FlatNode::new(Content::String("a".into()));
let node_a = NodeId::new();
let node_b = NodeId::new();
flat_node.patch_children(&OperandPatch::Insert {
position: OperandPosition::First,
node_id: node_a.clone(),
});
flat_node.patch_children(&OperandPatch::Insert {
position: OperandPosition::Last,
node_id: node_b,
});
flat_node.patch_children(&OperandPatch::Remove { node_id: node_b });
assert_eq!(flat_node.operands, vec![node_a]);
}
#[test]
fn operands_move() {
let mut flat_node = FlatNode::new(Content::String("a".into()));
let node_a = NodeId::new();
let node_b = NodeId::new();
let node_c = NodeId::new();
flat_node.patch_children(&OperandPatch::Insert {
position: OperandPosition::At(0),
node_id: node_b.clone(),
});
flat_node.patch_children(&OperandPatch::Insert {
position: OperandPosition::Before(node_b),
node_id: node_a.clone(),
});
flat_node.patch_children(&OperandPatch::Insert {
position: OperandPosition::After(node_b),
node_id: node_c.clone(),
});
flat_node.patch_children(&OperandPatch::Move {
node_id: node_b,
position: OperandPosition::First,
});
flat_node.patch_children(&OperandPatch::Move {
node_id: node_a,
position: OperandPosition::Last,
});
assert_eq!(flat_node.operands, vec![node_b, node_c, node_a]);
}
}