Skip to content

Commit

Permalink
fix(edit): Deprecate 'Document'
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Mar 11, 2024
1 parent 3abcf90 commit bae685b
Show file tree
Hide file tree
Showing 28 changed files with 124 additions and 122 deletions.
2 changes: 1 addition & 1 deletion crates/benchmarks/benches/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ mod toml_edit {
use super::*;

#[divan::bench(args=MANIFESTS)]
fn document(sample: &Data) -> ::toml_edit::Document {
fn document(sample: &Data) -> ::toml_edit::DocumentMut {
sample.content().parse().unwrap()
}

Expand Down
4 changes: 2 additions & 2 deletions crates/benchmarks/benches/linear.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ mod map {
bencher
.with_inputs(|| gen(num_entries))
.input_counter(divan::counter::BytesCount::of_str)
.bench_values(|sample| sample.parse::<toml_edit::Document>().unwrap())
.bench_values(|sample| sample.parse::<toml_edit::DocumentMut>().unwrap())
}

#[divan::bench(args = NUM_ENTRIES)]
Expand Down Expand Up @@ -45,7 +45,7 @@ mod array {
bencher
.with_inputs(|| gen(num_entries))
.input_counter(divan::counter::BytesCount::of_str)
.bench_values(|sample| sample.parse::<toml_edit::Document>().unwrap())
.bench_values(|sample| sample.parse::<toml_edit::DocumentMut>().unwrap())
}

#[divan::bench(args = NUM_ENTRIES)]
Expand Down
2 changes: 1 addition & 1 deletion crates/benchmarks/examples/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ fn main() -> Result<(), lexopt::Error> {

match args.parser {
Parser::Document => {
let _doc = CARGO_MANIFEST.parse::<toml_edit::Document>().unwrap();
let _doc = CARGO_MANIFEST.parse::<toml_edit::DocumentMut>().unwrap();
#[cfg(debug_assertions)] // Don't interefere with profiling
dbg!(_doc);
}
Expand Down
2 changes: 1 addition & 1 deletion crates/toml/src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub(crate) struct DocumentFormatter {
}

impl toml_edit::visit_mut::VisitMut for DocumentFormatter {
fn visit_document_mut(&mut self, node: &mut toml_edit::Document) {
fn visit_document_mut(&mut self, node: &mut toml_edit::DocumentMut) {
toml_edit::visit_mut::visit_document_mut(self, node);
}

Expand Down
6 changes: 3 additions & 3 deletions crates/toml/src/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ where
/// To serialize TOML values, instead of documents, see [`ValueSerializer`].
///
/// For greater customization, instead serialize to a
/// [`toml_edit::Document`](https://docs.rs/toml_edit/latest/toml_edit/struct.Document.html).
/// [`toml_edit::DocumentMut`](https://docs.rs/toml_edit/latest/toml_edit/struct.DocumentMut.html).
#[cfg(feature = "display")]
pub fn to_string_pretty<T: ?Sized>(value: &T) -> Result<String, Error>
where
Expand Down Expand Up @@ -161,7 +161,7 @@ impl<'d> Serializer<'d> {
/// Apply a default "pretty" policy to the document
///
/// For greater customization, instead serialize to a
/// [`toml_edit::Document`](https://docs.rs/toml_edit/latest/toml_edit/struct.Document.html).
/// [`toml_edit::DocumentMut`](https://docs.rs/toml_edit/latest/toml_edit/struct.DocumentMut.html).
pub fn pretty(dst: &'d mut String) -> Self {
let mut ser = Serializer::new(dst);
ser.settings.multiline_array = true;
Expand Down Expand Up @@ -923,7 +923,7 @@ mod internal {
use toml_edit::visit_mut::VisitMut as _;
settings.visit_table_mut(&mut table);

let doc: toml_edit::Document = table.into();
let doc: toml_edit::DocumentMut = table.into();
write!(dst, "{}", doc).unwrap();

Ok(())
Expand Down
12 changes: 6 additions & 6 deletions crates/toml_edit/examples/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use std::collections::BTreeSet;
use toml_edit::visit::*;
use toml_edit::visit_mut::*;
use toml_edit::{Array, Document, InlineTable, Item, KeyMut, Table, Value};
use toml_edit::{Array, DocumentMut, InlineTable, Item, KeyMut, Table, Value};

/// This models the visit state for dependency keys in a `Cargo.toml`.
///
Expand Down Expand Up @@ -223,7 +223,7 @@ cargo-test-macro = { path = "crates/cargo-test-macro" }
flate2 = { version = "0.4" }
"#;

fn visit_example(document: &Document) -> BTreeSet<&str> {
fn visit_example(document: &DocumentMut) -> BTreeSet<&str> {
let mut visitor = DependencyNameVisitor {
state: VisitState::Root,
names: BTreeSet::new(),
Expand All @@ -234,7 +234,7 @@ fn visit_example(document: &Document) -> BTreeSet<&str> {
visitor.names
}

fn visit_mut_example(document: &mut Document) {
fn visit_mut_example(document: &mut DocumentMut) {
let mut visitor = NormalizeDependencyTablesVisitor {
state: VisitState::Root,
};
Expand All @@ -243,7 +243,7 @@ fn visit_mut_example(document: &mut Document) {
}

fn main() {
let mut document: Document = INPUT.parse().expect("input is valid TOML");
let mut document: DocumentMut = INPUT.parse().expect("input is valid TOML");

println!("** visit example");
println!("{:?}", visit_example(&document));
Expand All @@ -256,7 +256,7 @@ fn main() {
#[cfg(test)]
#[test]
fn visit_correct() {
let document: Document = INPUT.parse().expect("input is valid TOML");
let document: DocumentMut = INPUT.parse().expect("input is valid TOML");

let names = visit_example(&document);
let expected = vec![
Expand All @@ -277,7 +277,7 @@ fn visit_correct() {
#[cfg(test)]
#[test]
fn visit_mut_correct() {
let mut document: Document = INPUT.parse().expect("input is valid TOML");
let mut document: DocumentMut = INPUT.parse().expect("input is valid TOML");

visit_mut_example(&mut document);
assert_eq!(format!("{}", document), VISIT_MUT_OUTPUT);
Expand Down
18 changes: 9 additions & 9 deletions crates/toml_edit/src/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ impl From<Error> for crate::TomlError {

impl std::error::Error for Error {}

/// Convert a TOML [documents][crate::Document] into `T`.
/// Convert a TOML [documents][crate::DocumentMut] into `T`.
#[cfg(feature = "parse")]
pub fn from_str<T>(s: &'_ str) -> Result<T, Error>
where
Expand All @@ -96,7 +96,7 @@ where
T::deserialize(de)
}

/// Convert a TOML [documents][crate::Document] into `T`.
/// Convert a TOML [documents][crate::DocumentMut] into `T`.
#[cfg(feature = "parse")]
pub fn from_slice<T>(s: &'_ [u8]) -> Result<T, Error>
where
Expand All @@ -106,7 +106,7 @@ where
from_str(s)
}

/// Convert a [Document][crate::Document] into `T`.
/// Convert a [DocumentMut][crate::DocumentMut] into `T`.
pub fn from_document<T>(d: impl Into<Deserializer>) -> Result<T, Error>
where
T: DeserializeOwned,
Expand All @@ -115,7 +115,7 @@ where
T::deserialize(deserializer)
}

/// Deserialization for TOML [documents][crate::Document].
/// Deserialization for TOML [documents][crate::DocumentMut].
pub struct Deserializer<S = String> {
root: crate::Item,
raw: Option<S>,
Expand All @@ -124,7 +124,7 @@ pub struct Deserializer<S = String> {
impl Deserializer {
/// Deserialization implementation for TOML.
#[deprecated(since = "0.22.6", note = "Replaced with `Deserializer::from`")]
pub fn new(input: crate::Document) -> Self {
pub fn new(input: crate::DocumentMut) -> Self {
Self::from(input)
}
}
Expand All @@ -139,9 +139,9 @@ impl<S: AsRef<str>> Deserializer<S> {
}
}

impl From<crate::Document> for Deserializer {
fn from(doc: crate::Document) -> Self {
let crate::Document { root, .. } = doc;
impl From<crate::DocumentMut> for Deserializer {
fn from(doc: crate::DocumentMut) -> Self {
let crate::DocumentMut { root, .. } = doc;
Self { root, raw: None }
}
}
Expand Down Expand Up @@ -272,7 +272,7 @@ impl<'de> serde::de::IntoDeserializer<'de, crate::de::Error> for Deserializer {
}
}

impl<'de> serde::de::IntoDeserializer<'de, crate::de::Error> for crate::Document {
impl<'de> serde::de::IntoDeserializer<'de, crate::de::Error> for crate::DocumentMut {
type Deserializer = Deserializer;

fn into_deserializer(self) -> Self::Deserializer {
Expand Down
2 changes: 1 addition & 1 deletion crates/toml_edit/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl<S: AsRef<str>> ImDocument<S> {
impl<S: AsRef<str>> ImDocument<S> {
/// # Panics
///
/// If run on on a `Document` not generated by the parser
/// If run on on a [`DocumentMut`] 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());
Expand Down
4 changes: 2 additions & 2 deletions crates/toml_edit/src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::table::{DEFAULT_KEY_DECOR, DEFAULT_KEY_PATH_DECOR, DEFAULT_TABLE_DECO
use crate::value::{
DEFAULT_LEADING_VALUE_DECOR, DEFAULT_TRAILING_VALUE_DECOR, DEFAULT_VALUE_DECOR,
};
use crate::Document;
use crate::DocumentMut;
use crate::{Array, InlineTable, Item, Table, Value};

pub(crate) fn encode_key(this: &Key, buf: &mut dyn Write, input: Option<&str>) -> Result {
Expand Down Expand Up @@ -195,7 +195,7 @@ pub(crate) fn encode_value(
}
}

impl Display for Document {
impl Display for DocumentMut {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
let mut path = Vec::new();
let mut last_position = 0;
Expand Down
6 changes: 3 additions & 3 deletions crates/toml_edit/src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::ops;

use crate::key::Key;
use crate::table::TableKeyValue;
use crate::Document;
use crate::DocumentMut;
use crate::{value, InlineTable, InternalString, Item, Table, Value};

// copied from
Expand Down Expand Up @@ -141,15 +141,15 @@ impl<'s> ops::IndexMut<&'s str> for InlineTable {
}
}

impl<'s> ops::Index<&'s str> for Document {
impl<'s> ops::Index<&'s str> for DocumentMut {
type Output = Item;

fn index(&self, key: &'s str) -> &Item {
self.root.index(key)
}
}

impl<'s> ops::IndexMut<&'s str> for Document {
impl<'s> ops::IndexMut<&'s str> for DocumentMut {
fn index_mut(&mut self, key: &'s str) -> &mut Item {
self.root.index_mut(key)
}
Expand Down
4 changes: 2 additions & 2 deletions crates/toml_edit/src/inline_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ impl InlineTable {
/// ```
/// # #[cfg(feature = "parse")] {
/// # #[cfg(feature = "display")] {
/// use toml_edit::Document;
/// let mut doc = "[a]\n[a.b]\n".parse::<Document>().expect("invalid toml");
/// use toml_edit::DocumentMut;
/// let mut doc = "[a]\n[a.b]\n".parse::<DocumentMut>().expect("invalid toml");
///
/// doc["a"].as_table_mut().unwrap().set_implicit(true);
/// assert_eq!(doc.to_string(), "[a.b]\n");
Expand Down
11 changes: 6 additions & 5 deletions crates/toml_edit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
//! ```rust
//! # #[cfg(feature = "parse")] {
//! # #[cfg(feature = "display")] {
//! use toml_edit::{Document, value};
//! use toml_edit::{DocumentMut, value};
//!
//! let toml = r#"
//! "hello" = 'toml!' # comment
//! ['a'.b]
//! "#;
//! let mut doc = toml.parse::<Document>().expect("invalid doc");
//! let mut doc = toml.parse::<DocumentMut>().expect("invalid doc");
//! assert_eq!(doc.to_string(), toml);
//! // let's add a new key/value pair inside a.b: c = {d = "hello"}
//! doc["a"]["b"]["c"]["d"] = value("hello");
Expand All @@ -43,7 +43,7 @@
//! By default, values are created with default formatting
//! ```rust
//! # #[cfg(feature = "display")] {
//! let mut doc = toml_edit::Document::new();
//! let mut doc = toml_edit::DocumentMut::new();
//! doc["foo"] = toml_edit::value("bar");
//! let expected = r#"foo = "bar"
//! "#;
Expand All @@ -54,7 +54,7 @@
//! You can choose a custom TOML representation by parsing the value.
//! ```rust
//! # #[cfg(feature = "display")] {
//! let mut doc = toml_edit::Document::new();
//! let mut doc = toml_edit::DocumentMut::new();
//! doc["foo"] = "'bar'".parse::<toml_edit::Item>().unwrap();
//! let expected = r#"foo = 'bar'
//! "#;
Expand Down Expand Up @@ -100,7 +100,8 @@ pub use crate::array::{Array, ArrayIntoIter, ArrayIter, ArrayIterMut};
pub use crate::array_of_tables::{
ArrayOfTables, ArrayOfTablesIntoIter, ArrayOfTablesIter, ArrayOfTablesIterMut,
};
/// Type representing a TOML document
/// Deprecated, replaced with [`DocumentMut`]
#[deprecated(since = "0.22.6", note = "Replaced with `DocumentMut`")]
pub type Document = DocumentMut;
pub use crate::document::DocumentMut;
pub use crate::document::ImDocument;
Expand Down
2 changes: 1 addition & 1 deletion crates/toml_edit/src/raw_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl RawString {

/// Access the underlying string
///
/// This generally requires a [`Document`][crate::Document].
/// This generally requires a [`DocumentMut`][crate::DocumentMut].
pub fn as_str(&self) -> Option<&str> {
match &self.0 {
RawStringInner::Empty => Some(""),
Expand Down
2 changes: 1 addition & 1 deletion crates/toml_edit/src/ser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ where
/// Serialize the given data structure into a TOML document.
///
/// This would allow custom formatting to be applied, mixing with format preserving edits, etc.
pub fn to_document<T: ?Sized>(value: &T) -> Result<crate::Document, Error>
pub fn to_document<T: ?Sized>(value: &T) -> Result<crate::DocumentMut, Error>
where
T: serde::ser::Serialize,
{
Expand Down
2 changes: 1 addition & 1 deletion crates/toml_edit/src/ser/pretty.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pub(crate) struct Pretty;

impl crate::visit_mut::VisitMut for Pretty {
fn visit_document_mut(&mut self, node: &mut crate::Document) {
fn visit_document_mut(&mut self, node: &mut crate::DocumentMut) {
crate::visit_mut::visit_document_mut(self, node);
}

Expand Down
8 changes: 4 additions & 4 deletions crates/toml_edit/src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ impl Table {
/// ```
/// # #[cfg(feature = "parse")] {
/// # #[cfg(feature = "display")] {
/// use toml_edit::Document;
/// let mut doc = "[a]\n[a.b]\n".parse::<Document>().expect("invalid toml");
/// use toml_edit::DocumentMut;
/// let mut doc = "[a]\n[a.b]\n".parse::<DocumentMut>().expect("invalid toml");
///
/// doc["a"].as_table_mut().unwrap().set_implicit(true);
/// assert_eq!(doc.to_string(), "[a.b]\n");
Expand All @@ -194,12 +194,12 @@ impl Table {
self.dotted
}

/// Sets the position of the `Table` within the `Document`.
/// Sets the position of the `Table` within the [`DocumentMut`][crate::DocumentMut].
pub fn set_position(&mut self, doc_position: usize) {
self.doc_position = Some(doc_position);
}

/// The position of the `Table` within the `Document`.
/// The position of the `Table` within the [`DocumentMut`][crate::DocumentMut].
///
/// Returns `None` if the `Table` was created manually (i.e. not via parsing)
/// in which case its position is set automatically. This can be overridden with
Expand Down
9 changes: 5 additions & 4 deletions crates/toml_edit/src/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
//! the-force = { value = "surrounds-you" }
//! "#;
//!
//! let mut document: Document = input.parse().unwrap();
//! let mut document: DocumentMut = input.parse().unwrap();
//! let mut visitor = StringCollector::default();
//! visitor.visit_document(&document);
//!
Expand All @@ -75,14 +75,15 @@
//! [on GitHub](https://github.com/toml-rs/toml/blob/main/crates/toml_edit/examples/visit.rs).

use crate::{
Array, ArrayOfTables, Datetime, Document, Formatted, InlineTable, Item, Table, TableLike, Value,
Array, ArrayOfTables, Datetime, DocumentMut, Formatted, InlineTable, Item, Table, TableLike,
Value,
};

/// Document tree traversal to mutate an exclusive borrow of a document tree in-place.
///
/// See the [module documentation](self) for details.
pub trait Visit<'doc> {
fn visit_document(&mut self, node: &'doc Document) {
fn visit_document(&mut self, node: &'doc DocumentMut) {
visit_document(self, node);
}

Expand Down Expand Up @@ -139,7 +140,7 @@ pub trait Visit<'doc> {
}
}

pub fn visit_document<'doc, V>(v: &mut V, node: &'doc Document)
pub fn visit_document<'doc, V>(v: &mut V, node: &'doc DocumentMut)
where
V: Visit<'doc> + ?Sized,
{
Expand Down
Loading

0 comments on commit bae685b

Please sign in to comment.