diff --git a/crates/toml_edit/src/parser/document.rs b/crates/toml_edit/src/parser/document.rs index d591e47e..2813d176 100644 --- a/crates/toml_edit/src/parser/document.rs +++ b/crates/toml_edit/src/parser/document.rs @@ -27,7 +27,7 @@ pub(crate) fn document(s: &[u8]) -> Result { // Remove BOM if present let s = s.strip_prefix(b"\xEF\xBB\xBF").unwrap_or(s); - let mut parser = RefCell::new(ParseState::default()); + let parser = RefCell::new(ParseState::default()); let input = Stream::new(s); let parsed = parse_ws(&parser) @@ -56,13 +56,11 @@ pub(crate) fn document(s: &[u8]) -> Result { s, )), Ok(..) => { - parser - .get_mut() - .finalize_table() + let doc = parser + .into_inner() + .into_document() .map_err(|e| TomlError::custom(e.to_string()))?; - let trailing = parser.borrow().trailing.as_str().into(); - parser.get_mut().document.trailing = trailing; - Ok(parser.into_inner().document) + Ok(doc) } Err(e) => Err(TomlError::new(e, s)), } diff --git a/crates/toml_edit/src/parser/state.rs b/crates/toml_edit/src/parser/state.rs index ab05692c..def0e151 100644 --- a/crates/toml_edit/src/parser/state.rs +++ b/crates/toml_edit/src/parser/state.rs @@ -5,15 +5,22 @@ use crate::table::TableKeyValue; use crate::{ArrayOfTables, Document, InternalString, Item, Table}; pub(crate) struct ParseState { - pub(crate) document: Document, - pub(crate) trailing: String, - pub(crate) current_table_position: usize, - pub(crate) current_table: Table, - pub(crate) current_is_array: bool, - pub(crate) current_table_path: Vec, + document: Document, + trailing: String, + current_table_position: usize, + current_table: Table, + current_is_array: bool, + current_table_path: Vec, } impl ParseState { + pub(crate) fn into_document(mut self) -> Result { + self.finalize_table()?; + let trailing = self.trailing.as_str().into(); + self.document.trailing = trailing; + Ok(self.document) + } + pub(crate) fn on_ws(&mut self, w: &str) { self.trailing.push_str(w); }