1use std::str::FromStr;
2
3use crate::parser;
4use crate::table::Iter;
5use crate::{Item, RawString, Table};
6
7/// Type representing a TOML document
8#[derive(Debug, Clone)]
9pub struct Document {
10 pub(crate) root: Item,
11 // Trailing comments and whitespaces
12 pub(crate) trailing: RawString,
13 pub(crate) original: Option<String>,
14 pub(crate) span: Option<std::ops::Range<usize>>,
15}
16
17impl Document {
18 /// Creates an empty document
19 pub fn new() -> Self {
20 Default::default()
21 }
22
23 /// Returns a reference to the root item.
24 pub fn as_item(&self) -> &Item {
25 &self.root
26 }
27
28 /// Returns a mutable reference to the root item.
29 pub fn as_item_mut(&mut self) -> &mut Item {
30 &mut self.root
31 }
32
33 /// Returns a reference to the root table.
34 pub fn as_table(&self) -> &Table {
35 self.root.as_table().expect("root should always be a table")
36 }
37
38 /// Returns a mutable reference to the root table.
39 pub fn as_table_mut(&mut self) -> &mut Table {
40 self.root
41 .as_table_mut()
42 .expect("root should always be a table")
43 }
44
45 /// Returns an iterator over the root table.
46 pub fn iter(&self) -> Iter<'_> {
47 self.as_table().iter()
48 }
49
50 /// Set whitespace after last element
51 pub fn set_trailing(&mut self, trailing: impl Into<RawString>) {
52 self.trailing = trailing.into();
53 }
54
55 /// Whitespace after last element
56 pub fn trailing(&self) -> &RawString {
57 &self.trailing
58 }
59
60 /// # Panics
61 ///
62 /// If run on on a `Document` not generated by the parser
63 pub(crate) fn despan(&mut self) {
64 self.span = None;
65 self.root.despan(self.original.as_deref().unwrap());
66 self.trailing.despan(self.original.as_deref().unwrap());
67 }
68}
69
70impl Default for Document {
71 fn default() -> Self {
72 Self {
73 root: Item::Table(Table::with_pos(doc_position:Some(0))),
74 trailing: Default::default(),
75 original: Default::default(),
76 span: Default::default(),
77 }
78 }
79}
80
81impl FromStr for Document {
82 type Err = crate::TomlError;
83
84 /// Parses a document from a &str
85 fn from_str(s: &str) -> Result<Self, Self::Err> {
86 let mut d: Document = parser::parse_document(raw:s)?;
87 d.despan();
88 Ok(d)
89 }
90}
91
92impl std::ops::Deref for Document {
93 type Target = Table;
94
95 fn deref(&self) -> &Self::Target {
96 self.as_table()
97 }
98}
99
100impl std::ops::DerefMut for Document {
101 fn deref_mut(&mut self) -> &mut Self::Target {
102 self.as_table_mut()
103 }
104}
105
106impl From<Table> for Document {
107 fn from(root: Table) -> Self {
108 Self {
109 root: Item::Table(root),
110 ..Default::default()
111 }
112 }
113}
114