#[derive(Default, Clone, Debug)]
struct Node {
name: String,
source: String,
path: String,
leaf: bool,
children: Vec<RcNode>
}
use std::cell::RefCell;
use std::rc::Rc;
#[derive(Default, Clone, Debug)]
struct Node {
name: String,
source: String,
path: String,
leaf: bool,
children: Vec<RcNode>
}
type RcNode = Rc<RefCell<&'static Node>>;
impl Node {
fn fast(name: &str, source: &str) -> Node{
Node{
name: name.to_string(),
source: source.to_string(),
path: String::from(""),
leaf: true,
children: Vec::new()
}
}
fn insert(&'static self, node: Node){
let mut current = Rc::new(RefCell::new(self));
let mut temp;
for part in node.source.split(".") {
temp = node.children.iter().find(|&v| v.borrow().path == part);
match temp {
None => {
let tmp: RcNode = Rc::new(
RefCell::new(
&Node {
name: String::new(),
source: String::new(),
path: part.to_string(),
leaf: true,
children: Vec::new()
}
)
);
current.borrow_mut().children.push(
tmp
);
current = tmp;
}
Some(val) => {
current = val.clone()
}
}
}
}
}
fn main() {
let mut root = Node::default();
root.insert(Node::fast("First", "e"));
root.insert(Node::fast("First", "e.sources"));
root.insert(Node::fast("First", "e.sources.two"));
root.insert(Node::fast("First", "e.secret"));
root.insert(Node::fast("First", "e.secret.left"));
root.insert(Node::fast("First", "e.secret.right"));
print!("{:#?}", root);
}
'static
.use std::cell::RefCell;
use std::rc::Rc;
#[derive(Default, Clone, Debug)]
struct NodeInner {
name: String,
source: String,
path: String,
leaf: bool,
children: Vec<Node>,
}
#[derive(Default, Clone, Debug)]
struct Node(Rc<RefCell<NodeInner>>);
impl Node {
fn fast(name: impl Into<String>, source: impl Into<String>) -> Self {
Self(Rc::new(RefCell::new(NodeInner {
name: name.into(),
source: source.into(),
path: String::new(),
leaf: true,
children: Vec::new(),
})))
}
fn insert(&self, node: Node) {
let mut current = self.clone();
for part in node.0.borrow().source.split(".") {
let inner = node.0.borrow();
let found = inner.children.iter().find(|&v| v.0.borrow().path == part);
match found {
None => {
let tmp = Node::fast(String::new(), String::new());
current.0.borrow_mut().children.push(tmp.clone());
current = tmp;
}
Some(val) => current = val.clone(),
}
}
}
}
fn main() {
let mut root = Node::default();
root.insert(Node::fast("First", "e"));
root.insert(Node::fast("First", "e.sources"));
root.insert(Node::fast("First", "e.sources.two"));
root.insert(Node::fast("First", "e.secret"));
root.insert(Node::fast("First", "e.secret.left"));
root.insert(Node::fast("First", "e.secret.right"));
print!("{:#?}", root);
}