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
//! TOML arbitrary serialization support for Tree.
use crate::tree::Tree;
impl Tree {
/// Deserializes arbitrary TOML data into a tree structure.
///
/// Requires the `arbitrary-toml` feature.
///
/// This function can parse any TOML file and convert it to a Tree representation,
/// where tables become nodes and values become leaves.
///
/// # Examples
///
/// ```
/// use treelog::Tree;
///
/// let toml_str = r#"
/// [package]
/// name = "treelog"
/// version = "0.0.4"
/// "#;
/// let tree = Tree::from_arbitrary_toml(toml_str).unwrap();
/// ```
pub fn from_arbitrary_toml(toml_str: &str) -> Result<Self, toml::de::Error> {
let value: toml::Value = toml::from_str(toml_str)?;
Ok(Self::from_toml_value(&value))
}
// Helper functions for TOML conversion
fn from_toml_value(value: &toml::Value) -> Self {
match value {
toml::Value::String(s) => Tree::new_leaf(format!("\"{}\"", s)),
toml::Value::Integer(i) => Tree::new_leaf(i.to_string()),
toml::Value::Float(f) => Tree::new_leaf(f.to_string()),
toml::Value::Boolean(b) => Tree::new_leaf(b.to_string()),
toml::Value::Datetime(dt) => Tree::new_leaf(dt.to_string()),
toml::Value::Array(arr) => {
let children: Vec<Tree> = arr
.iter()
.enumerate()
.map(|(idx, val)| {
let child = Self::from_toml_value(val);
Tree::Node(format!("[{}]", idx), vec![child])
})
.collect();
if children.is_empty() {
Tree::new_leaf("[]")
} else {
Tree::Node("array".to_string(), children)
}
}
toml::Value::Table(table) => {
let children: Vec<Tree> = table
.iter()
.map(|(key, val)| {
let child = Self::from_toml_value(val);
if child.is_leaf() {
// If the child is a leaf, we can merge it into the node label
let leaf_lines = child.lines().unwrap();
if leaf_lines.len() == 1 {
Tree::new_leaf(format!("{} = {}", key, leaf_lines[0]))
} else {
Tree::Node(key.clone(), vec![child])
}
} else {
Tree::Node(key.clone(), vec![child])
}
})
.collect();
if children.is_empty() {
Tree::new_leaf("{}")
} else {
Tree::Node("table".to_string(), children)
}
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_from_arbitrary_toml() {
let toml_str = r#"
[package]
name = "treelog"
version = "0.0.4"
"#;
let tree = Tree::from_arbitrary_toml(toml_str);
assert!(tree.is_ok());
let tree = tree.unwrap();
assert!(tree.is_node());
}
#[test]
fn test_from_arbitrary_toml_array() {
let toml_str = r#"
dependencies = ["serde", "toml"]
"#;
let tree = Tree::from_arbitrary_toml(toml_str);
assert!(tree.is_ok());
}
}