1use crate::{Error, Value, config::CommentStyle, section::Section, util};
2
3#[derive(Debug, Clone)]
4pub struct TomlContent {
5 pub sections: Vec<Section>,
6}
7
8impl TomlContent {
9 pub fn merge_value(&mut self, value: Value) {
10 let values = value.flatten();
11 for value in &values {
12 if value.array_index.is_some() {
13 let section_key = util::key_parent(&value.key);
14 let mut new_section = None;
15 for section in &mut self.sections {
16 if section.key != section_key {
17 continue;
18 }
19 if section.array_index == value.array_index {
20 new_section = None;
21 break;
22 }
23 if new_section.is_none() {
24 let mut section = section.clone();
25 section.array_index = value.array_index;
26 new_section = Some(section)
27 }
28 }
29 if let Some(section) = new_section {
30 self.sections.push(section);
31 }
32 }
33 }
34 for value in values {
35 'f0: for section in &mut self.sections {
36 if section.array_index != value.array_index {
37 continue;
38 }
39 for block in &mut section.blocks {
40 if block.key == value.key && value.value.is_some() {
41 block.value = Some(value);
42 break 'f0;
43 }
44 }
45 }
46 }
47 }
48
49 pub fn config_commented(&mut self, commented: bool) {
50 for section in &mut self.sections {
51 section.meta.config.commented = commented;
52 for block in &mut section.blocks {
53 block.meta.config.commented = commented;
54 }
55 }
56 }
57
58 pub fn config_comment_style_hide(&mut self) {
59 let style = CommentStyle::Hide;
60 for section in &mut self.sections {
61 section.meta.config.comment_style = Some(style);
62 for block in &mut section.blocks {
63 block.meta.config.comment_style = Some(style);
64 }
65 }
66 }
67
68 pub fn render(&self) -> Result<String, Error> {
69 let mut lines = Vec::new();
70 for section in &self.sections {
71 let line = section.render()?;
72 if !line.trim().is_empty() {
73 lines.push(line);
74 }
75 }
76 Ok(lines.join("\n\n").trim().to_string())
77 }
78}