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 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
use std::collections::HashMap as Map;
//---- Structs ----//
/// Trait to define structs that model a node content.
pub trait NodeContent {
/// Constructor.
///
/// # Arguments
///
/// * `content` - Node content.
///
/// # Return
///
/// * An [`Option`] with the node content.
///
fn new(content: &str) -> Option<Self> where Self: Sized;
/// Get node value.
///
/// # Return
///
/// * Node value.
///
fn get_val(&self) -> &str;
/// Generate node content.
///
/// Use by serializers to create back the string of a node that is parsed by a NodeContent implementer.
///
/// # Return
///
/// * Node content.
///
fn gen_content(&self) -> String;
}
/// Default [`NodeContent`] struct.
///
/// It simply holds the content as is, without parsing or modifying it.
#[derive(Debug)]
pub struct RawNode {
/// Node content.
content: String
}
impl NodeContent for RawNode {
fn new(content: &str) -> Option<Self> {
Some(
Self {
content: String::from(content)
}
)
}
fn get_val(&self) -> &str {
&self.content
}
fn gen_content(&self) -> String {
String::from(self.get_val())
}
}
/// Struct that contains a tree node.
#[derive(Debug)]
pub struct Node<T: NodeContent = RawNode> {
/// Node content.
content: T,
/// Nodel level.
level: usize,
/// Parent node index in the tree array.
parent_position: Option<usize>,
// Map of content/node index, to find a child by name.
child_map: Map<String, usize>,
/// Index of current node in the parent [`children`][`Node::children`] array.
parents_children_pos: Option<usize>,
/// Array that contains indexes of of children nodes.
children: Vec<usize>
}
//---- Implementations ----//
impl<T: NodeContent> Node<T> {
/// Create new root node.
///
/// # Arguments
///
/// * `content` - Node content.
///
/// # Return
///
/// * Node struct or None if content parsing fails.
///
pub fn new_root(content: &str) -> Option<Self> {
Self::new_node(content, 1)
}
/// Create new node.
///
/// # Arguments
///
/// * `content` - Node content.
/// * `level` - Node level.
///
/// # Return
///
/// * Node struct or None if content parsing fails.
///
pub fn new_node(content: &str, level: usize) -> Option<Self> {
if let Some(content_node) = NodeContent::new(content) {
Some(
Node {
content: content_node,
level,
parent_position: None,
child_map: Map::new(),
parents_children_pos: None,
children: vec!()
}
)
}
else {
None
}
}
/// Set content.
///
/// # Arguments
///
/// * `content` - Node content.
///
/// # Return
///
/// * Nothing.
///
pub fn set_content(&mut self, content: T) {
self.content = content;
}
/// Get content. Move self.
///
/// # Return
///
/// * Node content.
///
pub fn get_content(self) -> T {
self.content
}
/// Get content reference.
///
/// # Return
///
/// * Node content reference.
///
pub fn get_content_ref(&self) -> &T {
&self.content
}
/// Set level.
///
/// # Arguments
///
/// * `level` - Node level.
///
/// # Return
///
/// * Nothing.
///
pub fn set_level(&mut self, level: usize) {
self.level = level;
}
/// Get level.
///
/// # Return
///
/// * Node level.
///
pub fn get_level(&self) -> usize {
self.level
}
/// Get number of children.
///
/// # Return
///
/// * Number of children.
///
pub fn get_num_chuildren(&self) -> usize {
self.children.len()
}
/// Set parent node position.
///
/// # Arguments
///
/// * `parent_position` - Parent node position.
///
/// # Return
///
/// * Nothing.
///
pub fn set_parent_position(&mut self, parent_position: usize) {
self.parent_position = Some(parent_position);
}
/// Get parent node position.
///
/// # Return
///
/// * Parent node position..
///
pub fn get_parent_position(&self) -> Option<usize> {
self.parent_position
}
/// Set parent's children array position.
///
/// # Arguments
///
/// * `parents_children_pos` - Position of current node in parent's children array.
///
/// # Return
///
/// * Nothing.
///
pub fn set_parents_children_pos(&mut self, parents_children_pos: usize) {
self.parents_children_pos = Some(parents_children_pos);
}
/// Get parent's children array position.
///
/// * Position of current node in parent's children array.
///
pub fn get_parents_children_pos(&self) -> Option<usize> {
self.parents_children_pos
}
/// Add new child.
///
/// # Arguments
///
/// * `node_content` - Node content.
/// * `node_index` - Node index.
///
/// # Return
///
/// * Nothing.
///
pub fn add_child(&mut self, node_content: String, node_index: usize) {
self.children.push(node_index);
self.child_map.insert(node_content, node_index);
}
/// Remove child.
///
/// # Arguments
///
/// * `node_index` - Node index.
///
/// # Return
///
/// * Nothing.
///
pub fn remove_child(&mut self, node_content: &str, node_index: usize) {
self.child_map.remove(node_content);
self.children[node_index] = usize::MAX;
}
/// Update child map.
///
/// # Arguments
///
/// * `node_content` - Current node content.
/// * `new_node_content` - New node content.
///
/// # Return
///
/// * An [`Option`] with the node index.
///
pub fn update_child(&mut self, node_content: &str, new_node_content: &str) -> Option<usize> {
if let Some(node_index) = self.child_map.remove(node_content) {
self.child_map.insert(String::from(new_node_content), node_index);
return Some(node_index);
}
None
}
/// Get child index using node content.
///
/// # Arguments
///
/// * `node_content` - Current node content.
///
/// # Return
///
/// * An [`Option`] with the node index.
///
pub fn get_child(&self, node_content: &str) -> Option<usize> {
if let Some(node_index) = self.child_map.get(node_content) {
Some(*node_index)
}
else {
None
}
}
/// Get children array reference.
///
/// # Return
///
/// * Array ref.
///
pub fn get_children_ref(&self) -> &[usize] {
&self.children
}
}