Skip to content

Commit

Permalink
Merge pull request #700 from epage/cleanup
Browse files Browse the repository at this point in the history
fix(edit): Deprecate 'Document'
  • Loading branch information
epage committed Mar 11, 2024
2 parents 8b6f77e + bae685b commit 523969d
Show file tree
Hide file tree
Showing 28 changed files with 143 additions and 149 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
20 changes: 10 additions & 10 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,10 +139,10 @@ impl<S: AsRef<str>> Deserializer<S> {
}
}

impl From<crate::Document> for Deserializer {
fn from(doc: crate::Document) -> Self {
let crate::Document { root, raw, .. } = doc;
Self { root, raw }
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
32 changes: 15 additions & 17 deletions 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 Expand Up @@ -67,15 +67,12 @@ impl<S: AsRef<str>> ImDocument<S> {
}

impl<S: AsRef<str>> ImDocument<S> {
/// Allow editing of the [`Document`]
pub fn into_mut(mut self) -> Document {
/// Allow editing of the [`DocumentMut`]
pub fn into_mut(mut self) -> DocumentMut {
self.despan();
Document {
DocumentMut {
root: self.root,
trailing: self.trailing,
// 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 @@ -110,14 +107,13 @@ impl<S> std::ops::Deref for ImDocument<S> {

/// Type representing a TOML document
#[derive(Debug, Clone)]
pub struct Document {
pub struct DocumentMut {
pub(crate) root: Item,
// Trailing comments and whitespaces
pub(crate) trailing: RawString,
pub(crate) raw: Option<String>,
}

impl Document {
impl DocumentMut {
/// Creates an empty document
pub fn new() -> Self {
Default::default()
Expand Down Expand Up @@ -161,18 +157,17 @@ impl Document {
}
}

impl Default for Document {
impl Default for DocumentMut {
fn default() -> Self {
Self {
root: Item::Table(Table::with_pos(Some(0))),
trailing: Default::default(),
raw: Default::default(),
}
}
}

#[cfg(feature = "parse")]
impl FromStr for Document {
impl FromStr for DocumentMut {
type Err = crate::TomlError;

/// Parses a document from a &str
Expand All @@ -182,21 +177,21 @@ impl FromStr for Document {
}
}

impl std::ops::Deref for Document {
impl std::ops::Deref for DocumentMut {
type Target = Table;

fn deref(&self) -> &Self::Target {
self.as_table()
}
}

impl std::ops::DerefMut for Document {
impl std::ops::DerefMut for DocumentMut {
fn deref_mut(&mut self) -> &mut Self::Target {
self.as_table_mut()
}
}

impl From<Table> for Document {
impl From<Table> for DocumentMut {
fn from(root: Table) -> Self {
Self {
root: Item::Table(root),
Expand All @@ -209,5 +204,8 @@ impl From<Table> for Document {
#[cfg(feature = "parse")]
#[cfg(feature = "display")]
fn default_roundtrip() {
Document::default().to_string().parse::<Document>().unwrap();
DocumentMut::default()
.to_string()
.parse::<DocumentMut>()
.unwrap();
}
16 changes: 4 additions & 12 deletions crates/toml_edit/src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ use std::fmt::{Display, Formatter, Result, Write};

use toml_datetime::*;

use crate::document::Document;
use crate::inline_table::DEFAULT_INLINE_KEY_DECOR;
use crate::key::Key;
use crate::repr::{Formatted, Repr, ValueRepr};
use crate::table::{DEFAULT_KEY_DECOR, DEFAULT_KEY_PATH_DECOR, DEFAULT_TABLE_DECOR};
use crate::value::{
DEFAULT_LEADING_VALUE_DECOR, DEFAULT_TRAILING_VALUE_DECOR, DEFAULT_VALUE_DECOR,
};
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 All @@ -212,17 +212,9 @@ impl Display for Document {
tables.sort_by_key(|&(id, _, _, _)| id);
let mut first_table = true;
for (_, table, path, is_array) in tables {
visit_table(
f,
self.raw.as_deref(),
table,
&path,
is_array,
&mut first_table,
)?;
visit_table(f, None, table, &path, is_array, &mut first_table)?;
}
self.trailing()
.encode_with_default(f, self.raw.as_deref(), "")
self.trailing().encode_with_default(f, None, "")
}
}

Expand Down
6 changes: 3 additions & 3 deletions crates/toml_edit/src/index.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::ops;

use crate::document::Document;
use crate::key::Key;
use crate::table::TableKeyValue;
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

0 comments on commit 523969d

Please sign in to comment.