makepad_live_compiler/
live_node_reader.rs

1use {
2    std::{
3        ops::Deref,
4        ops::DerefMut,
5    },
6    crate::{
7        live_node_vec::*,
8        live_node::{ LiveNode, LiveProp},
9    }
10};
11
12const MAX_CLONE_STACK_DEPTH_SAFETY: usize = 100;
13
14pub struct LiveNodeReader<'a> {
15    eot: bool,
16    depth: usize,
17    index: usize,
18    nodes: &'a[LiveNode]
19}
20
21impl<'a> LiveNodeReader<'a> {
22    pub fn new(index: usize, nodes: &'a[LiveNode]) -> Self {
23        
24        Self {
25            eot: false,
26            depth: 0,
27            index,
28            nodes
29        }
30    }
31    
32    pub fn index_option(&self, index: Option<usize>, depth_change: isize) -> Option<Self> {
33        if self.eot {panic!();}
34        index.map(|index| Self {
35            eot: self.eot,
36            depth: (self.depth as isize + depth_change) as usize,
37            index,
38            nodes: self.nodes
39        })
40    }
41    
42    pub fn node(&self) -> &LiveNode {
43        if self.eot {panic!();}
44        &self.nodes[self.index]
45    }
46    
47    pub fn parent(&self) -> Option<Self> {self.index_option(self.nodes.parent(self.index), -1)}
48    pub fn append_child_index(&self) -> usize {self.nodes.append_child_index(self.index)}
49    pub fn first_child(&self) -> Option<Self> {self.index_option(self.nodes.first_child(self.index), 1)}
50    pub fn last_child(&self) -> Option<Self> {self.index_option(self.nodes.last_child(self.index), 1)}
51    pub fn next_child(&self) -> Option<Self> {self.index_option(self.nodes.next_child(self.index), 0)}
52    
53    pub fn node_slice(&self) -> &[LiveNode] {
54        if self.eot {panic!()}
55        self.nodes.node_slice(self.index)
56    }
57    
58    pub fn children_slice(&self) -> &[LiveNode] {
59        if self.eot {panic!()}
60        self.nodes.children_slice(self.index)
61    }
62    
63    pub fn child_by_number(&self, child_number: usize) -> Option<Self> {
64        self.index_option(self.nodes.child_by_number(self.index, child_number), 1)
65    }
66    
67    pub fn child_by_name(&self, name: LiveProp) -> Option<Self> {
68        self.index_option(self.nodes.child_by_name(self.index, name), 1)
69    }
70    
71    fn child_by_path(&self, path: &[LiveProp]) -> Option<Self> {
72        self.index_option(self.nodes.child_by_path(self.index, path), 1)
73    }
74    
75    pub fn scope_up_by_name(&self, name: LiveProp) -> Option<Self> {
76        self.index_option(self.nodes.scope_up_by_name(self.index, name), 0)
77    }
78    
79    pub fn count_children(&self) -> usize {self.nodes.count_children(self.index)}
80    pub fn clone_child(&self, out_vec: &mut Vec<LiveNode>) {
81        if self.eot {panic!();}
82        self.nodes.clone_child(self.index, out_vec)
83    }
84    
85    pub fn to_string(&self, max_depth: usize) -> String {
86        if self.eot {panic!();}
87        self.nodes.to_string(self.index, max_depth)
88    }
89    
90    pub fn skip(&mut self) {
91        if self.eot {panic!();}
92        self.index = self.nodes.skip_node(self.index);
93        // check eot
94        if self.nodes[self.index].is_close() { // standing on a close node
95            if self.depth == 1 {
96                self.eot = true;
97                self.index += 1;
98            }
99        }
100    }
101    
102    pub fn walk(&mut self) {
103        if self.eot {panic!();}
104        if self.nodes[self.index].is_open() {
105            self.depth += 1;
106        }
107        else if self.nodes[self.index].is_close() {
108            if self.depth == 0 {panic!()}
109            self.depth -= 1;
110            if self.depth == 0 {
111                self.eot = true;
112            }
113        }
114        self.index += 1;
115    }
116    
117    pub fn is_eot(&self) -> bool {
118        self.eot
119    }
120    
121    pub fn index(&self) -> usize {
122        self.index
123    }
124    
125    pub fn depth(&self) -> usize {
126        self.depth
127    }
128    
129    pub fn nodes(&self) -> &[LiveNode] {
130        self.nodes
131    }
132    
133}
134
135impl<'a> Deref for LiveNodeReader<'a> {
136    type Target = LiveNode;
137    fn deref(&self) -> &Self::Target {&self.nodes[self.index]}
138}
139
140
141pub struct LiveNodeMutReader<'a> {
142    eot: bool,
143    depth: usize,
144    index: usize,
145    nodes: &'a mut [LiveNode]
146}
147
148impl<'a> LiveNodeMutReader<'a> {
149    pub fn new(index: usize, nodes: &'a mut [LiveNode]) -> Self {
150        Self {
151            eot: false,
152            depth: 0,
153            index,
154            nodes
155        }
156    }
157    
158    pub fn node(&mut self) -> &mut LiveNode {
159        if self.eot {panic!();}
160        &mut self.nodes[self.index]
161    }
162    
163    pub fn node_slice(&self) -> &[LiveNode] {
164        if self.eot {panic!()}
165        self.nodes.node_slice(self.index)
166    }
167    
168    pub fn children_slice(&self) -> &[LiveNode] {
169        if self.eot {panic!()}
170        self.nodes.children_slice(self.index)
171    }
172    
173    pub fn count_children(&self) -> usize {self.nodes.count_children(self.index)}
174    
175    pub fn clone_child(&self, out_vec: &mut Vec<LiveNode>) {
176        if self.eot {panic!();}
177        self.nodes.clone_child(self.index, out_vec)
178    }
179    
180    pub fn to_string(&self, max_depth: usize) -> String {
181        if self.eot {panic!();}
182        self.nodes.to_string(self.index, max_depth)
183    }
184    
185    pub fn skip(&mut self) {
186        if self.eot {panic!();}
187        self.index = self.nodes.skip_node(self.index);
188        if self.nodes[self.index].is_close() { // standing on a close node
189            if self.depth == 1 {
190                self.eot = true;
191                self.index += 1;
192            }
193        }
194    }
195    
196    pub fn walk(&mut self) {
197        if self.eot {panic!();}
198        if self.nodes[self.index].is_open() {
199            self.depth += 1;
200        }
201        else if self.nodes[self.index].value.is_close() {
202            if self.depth == 0 {panic!()}
203            self.depth -= 1;
204            if self.depth == 0 {
205                self.eot = true;
206            }
207        }
208        self.index += 1;
209    }
210    
211    pub fn is_eot(&mut self) -> bool {
212        self.eot
213    }
214    
215    pub fn index(&mut self) -> usize {
216        self.index
217    }
218    
219    pub fn depth(&mut self) -> usize {
220        self.depth
221    }
222    
223    pub fn nodes(&mut self) -> &mut [LiveNode] {
224        self.nodes
225    }
226    
227}
228
229impl<'a> Deref for LiveNodeMutReader<'a> {
230    type Target = LiveNode;
231    fn deref(&self) -> &Self::Target {&self.nodes[self.index]}
232}
233
234impl<'a> DerefMut for LiveNodeMutReader<'a> {
235    fn deref_mut(&mut self) -> &mut Self::Target {&mut self.nodes[self.index]}
236}