newick_rs/
simple_tree.rs

1use crate::tree::{FromNewick, ToNewick};
2use serde::{Deserialize, Serialize};
3
4#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
5pub struct SimpleTree {
6    pub name: String,
7    pub length: Option<f64>,
8    pub children: Vec<SimpleTree>,
9}
10
11impl SimpleTree {
12    pub fn new(name: String, length: Option<f64>, children: Vec<SimpleTree>) -> Self {
13        Self {
14            name,
15            length,
16            children,
17        }
18    }
19}
20
21impl FromNewick for SimpleTree {
22    fn leaf(name: String) -> Self {
23        Self::new(name, None, Vec::new())
24    }
25
26    fn internal(name: String, children: Vec<Self>) -> Self {
27        Self::new(name, None, children)
28    }
29
30    fn update_length(self, length: Option<f64>) -> Self {
31        Self::new(self.name, length, self.children)
32    }
33}
34
35impl ToNewick for SimpleTree {
36    type Child = SimpleTree;
37
38    fn get_name(&self) -> String {
39        self.name.clone()
40    }
41
42    fn get_children<'a>(&'a self) -> Vec<&'a Self::Child> {
43        self.children.iter().collect()
44    }
45
46    fn get_length(&self) -> Option<f64> {
47        self.length
48    }
49}