Skip to content

Commit

Permalink
fix(edit): Prevent corrupting a Document
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Mar 8, 2024
1 parent f5546d2 commit 885f0b9
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions crates/toml_edit/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ impl<S: AsRef<str>> ImDocument<S> {
pub fn parse(raw: S) -> Result<Self, crate::TomlError> {
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<S> ImDocument<S> {
Expand Down Expand Up @@ -56,16 +64,17 @@ impl<S: AsRef<str>> ImDocument<S> {
}
}

impl<S: Into<String>> ImDocument<S> {
impl<S: AsRef<str>> ImDocument<S> {
/// 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,
}
}
}

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 885f0b9

Please sign in to comment.