diff --git a/crates/toml_edit/src/document.rs b/crates/toml_edit/src/document.rs index df8888c7..6217fbf6 100644 --- a/crates/toml_edit/src/document.rs +++ b/crates/toml_edit/src/document.rs @@ -25,6 +25,14 @@ impl> ImDocument { pub fn parse(raw: S) -> Result { crate::parser::parse_document(raw) } + + /// # Panics + /// + /// If run on on a `Document` not generated by the parser + pub(crate) fn despan(&mut self) { + self.root.despan(self.raw.as_ref()); + self.trailing.despan(self.raw.as_ref()); + } } impl ImDocument { @@ -56,16 +64,17 @@ impl> ImDocument { } } -impl> ImDocument { +impl> ImDocument { /// Allow editing of the [`Document`] - pub fn into_mut(self) -> Document { - let mut doc = Document { + pub fn into_mut(mut self) -> Document { + self.despan(); + Document { root: self.root, trailing: self.trailing, - raw: Some(self.raw.into()), - }; - doc.despan(); - doc + // prevent a child of `ImDocument` from being inserted into `Document` and having the + // spans evaluated when using `crate::encode` + raw: None, + } } } @@ -148,14 +157,6 @@ impl Document { pub fn trailing(&self) -> &RawString { &self.trailing } - - /// # Panics - /// - /// If run on on a `Document` not generated by the parser - pub(crate) fn despan(&mut self) { - self.root.despan(self.raw.as_deref().unwrap()); - self.trailing.despan(self.raw.as_deref().unwrap()); - } } impl Default for Document {