use std::str::FromStr;
use crate::parser;
use crate::table::Iter;
use crate::{InternalString, Item, Table};
#[derive(Debug, Clone)]
pub struct Document {
pub root: Item,
pub(crate) trailing: InternalString,
}
impl Document {
pub fn new() -> Self {
Default::default()
}
pub fn as_table(&self) -> &Table {
self.root.as_table().expect("root should always be a table")
}
pub fn as_table_mut(&mut self) -> &mut Table {
self.root
.as_table_mut()
.expect("root should always be a table")
}
pub fn iter(&self) -> Iter<'_> {
self.root
.as_table()
.expect("root should always be a table")
.iter()
}
pub fn set_trailing(&mut self, trailing: &str) {
self.trailing = InternalString::from(trailing);
}
pub fn trailing(&self) -> &str {
self.trailing.as_str()
}
}
impl Default for Document {
fn default() -> Self {
Self {
root: Item::Table(Table::with_pos(Some(0))),
trailing: Default::default(),
}
}
}
impl FromStr for Document {
type Err = parser::TomlError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
parser::TomlParser::parse(s.as_bytes())
}
}