Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: toml-rs/toml
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v0.6.0
Choose a base ref
...
head repository: toml-rs/toml
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v0.7.0
Choose a head ref
  • 5 commits
  • 14 files changed
  • 1 contributor

Commits on Oct 14, 2021

  1. chore: Update release process

    epage committed Oct 14, 2021
    Copy the full SHA
    406646d View commit details

Commits on Oct 18, 2021

  1. docs: Clarify role of toml_edit::easy

    Fixes #239
    epage committed Oct 18, 2021
    Copy the full SHA
    ccd8336 View commit details

Commits on Nov 2, 2021

  1. Apply feedkback from porting cargo (#240)

    * fix!: Make `Document::root` private
    
    This gives us more implementation flexibility
    
    BREAKING CHANGE: Instead of dealing with `Document::root`, use
    `Document::as_table()` or `Document::as_table_mut()`.
    
    * refactor: Use types to enforce root item's type
    
    * feat: Make it easier to work with root table
    
    In porting cargo to toml_edit, I found it would be nice if we allowed
    the document to be used as the table it wraps.
    
    * fix!: Unify the normal/easy `Index` traits
    
    This trait isn't normally interacted with by users
    
    * feat: Non-panicking indexing on Items
    
    This is being pulled over from the `easy` API.  Found this would be
    useful inside of cargo.  Some direct document interation is needed
    before using serde to convert it to native types.
    
    * style: Make clippy happy
    epage authored Nov 2, 2021
    Copy the full SHA
    43a4a30 View commit details
  2. docs: Update changelog

    epage committed Nov 2, 2021
    Copy the full SHA
    60405fe View commit details
  3. chore: Release

    epage committed Nov 2, 2021
    Copy the full SHA
    7f97583 View commit details
Showing with 197 additions and 89 deletions.
  1. +17 −1 CHANGELOG.md
  2. +1 −1 Cargo.toml
  3. +1 −1 release.toml
  4. +20 −12 src/document.rs
  5. +11 −16 src/easy/de/mod.rs
  6. +71 −0 src/easy/de/table.rs
  7. +14 −16 src/easy/macros.rs
  8. +6 −1 src/easy/mod.rs
  9. +2 −2 src/easy/ser/table.rs
  10. +21 −36 src/index.rs
  11. +28 −0 src/item.rs
  12. +2 −0 src/lib.rs
  13. +1 −1 tests/decoder.rs
  14. +2 −2 tests/encoder.rs
18 changes: 17 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -7,6 +7,21 @@ The format is based on [Keep a Changelog].
<!-- next-header -->
## [Unreleased] - ReleaseDate

## [0.7.0] - 2021-11-02

### Breaking Changes

- `Document::root` is now private

#### Features

- `Document` now derefs to `Table` for easier access
- `Item` now has `get` / `get_mut` like `easy::Value`

#### Fixes

- Clarified role of `toml_edit::easy`

## [0.6.0] - 2021-10-14

#### Features
@@ -114,7 +129,8 @@ This release was sponsored by Futurewei
- `array.push` now returns a `Result`.

<!-- next-url -->
[Unreleased]: https://github.com/ordian/toml_edit/compare/v0.6.0...HEAD
[Unreleased]: https://github.com/ordian/toml_edit/compare/v0.7.0...HEAD
[0.7.0]: https://github.com/ordian/toml_edit/compare/v0.6.0...v0.7.0
[0.6.0]: https://github.com/ordian/toml_edit/compare/v0.5.0...v0.6.0
[0.5.0]: https://github.com/ordian/toml_edit/compare/v0.4.0...v0.5.0
[0.4.0]: https://github.com/ordian/toml_edit/compare/v0.3.1...v0.4.0
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "toml_edit"
version = "0.6.0"
version = "0.7.0"
readme = "README.md"
license = "MIT/Apache-2.0"
keywords = ["toml"]
2 changes: 1 addition & 1 deletion release.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pre-release-commit-message = "chore: Release"
no-dev-version = true
dev-version = false
tag-message = "{{tag_name}}"
tag-name = "{{prefix}}v{{version}}"
consolidate-commits = true
32 changes: 20 additions & 12 deletions src/document.rs
Original file line number Diff line number Diff line change
@@ -2,13 +2,12 @@ use std::str::FromStr;

use crate::parser;
use crate::table::Iter;
use crate::{InternalString, Item, Table};
use crate::{InternalString, Table};

/// Type representing a TOML document
#[derive(Debug, Clone)]
pub struct Document {
/// Root should always be `Item::Table`.
pub root: Item,
pub(crate) root: Table,
// Trailing comments and whitespaces
pub(crate) trailing: InternalString,
}
@@ -21,22 +20,17 @@ impl Document {

/// Returns a reference to the root table.
pub fn as_table(&self) -> &Table {
self.root.as_table().expect("root should always be a table")
&self.root
}

/// Returns a mutable reference to the root table.
pub fn as_table_mut(&mut self) -> &mut Table {
self.root
.as_table_mut()
.expect("root should always be a table")
&mut self.root
}

/// Returns an iterator over the root table.
pub fn iter(&self) -> Iter<'_> {
self.root
.as_table()
.expect("root should always be a table")
.iter()
self.root.iter()
}

/// Set whitespace after last element
@@ -53,7 +47,7 @@ impl Document {
impl Default for Document {
fn default() -> Self {
Self {
root: Item::Table(Table::with_pos(Some(0))),
root: Table::with_pos(Some(0)),
trailing: Default::default(),
}
}
@@ -67,3 +61,17 @@ impl FromStr for Document {
parser::TomlParser::parse(s.as_bytes())
}
}

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

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

impl std::ops::DerefMut for Document {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.root
}
}
27 changes: 11 additions & 16 deletions src/easy/de/mod.rs
Original file line number Diff line number Diff line change
@@ -113,7 +113,7 @@ impl<'de, 'a> serde::Deserializer<'de> for Deserializer {
where
V: serde::de::Visitor<'de>,
{
ItemDeserializer::new(self.input.root).deserialize_any(visitor)
TableDeserializer::new(self.input.root).deserialize_any(visitor)
}

// `None` is interpreted as a missing field so be sure to implement `Some`
@@ -135,21 +135,16 @@ impl<'de, 'a> serde::Deserializer<'de> for Deserializer {
where
V: serde::de::Visitor<'de>,
{
match self.input.root {
crate::Item::Table(v) => {
if v.is_empty() {
Err(crate::easy::de::Error::custom(
"wanted exactly 1 element, found 0 elements",
))
} else if v.len() != 1 {
Err(crate::easy::de::Error::custom(
"wanted exactly 1 element, more than 1 element",
))
} else {
visitor.visit_enum(crate::easy::de::TableMapAccess::new(v))
}
}
_ => Err(crate::easy::de::Error::custom("wanted table")),
if self.input.root.is_empty() {
Err(crate::easy::de::Error::custom(
"wanted exactly 1 element, found 0 elements",
))
} else if self.input.root.len() != 1 {
Err(crate::easy::de::Error::custom(
"wanted exactly 1 element, more than 1 element",
))
} else {
visitor.visit_enum(crate::easy::de::TableMapAccess::new(self.input.root))
}
}

71 changes: 71 additions & 0 deletions src/easy/de/table.rs
Original file line number Diff line number Diff line change
@@ -2,6 +2,77 @@ use serde::de::IntoDeserializer;

use crate::easy::de::Error;

pub(crate) struct TableDeserializer {
input: crate::Table,
}

impl TableDeserializer {
pub(crate) fn new(input: crate::Table) -> Self {
Self { input }
}
}

impl<'de, 'a> serde::Deserializer<'de> for TableDeserializer {
type Error = Error;

fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: serde::de::Visitor<'de>,
{
visitor.visit_map(crate::easy::de::TableMapAccess::new(self.input))
}

// `None` is interpreted as a missing field so be sure to implement `Some`
// as a present field.
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Error>
where
V: serde::de::Visitor<'de>,
{
visitor.visit_some(self)
}

fn deserialize_struct<V>(
self,
_name: &'static str,
_fields: &'static [&'static str],
visitor: V,
) -> Result<V::Value, Error>
where
V: serde::de::Visitor<'de>,
{
self.deserialize_any(visitor)
}

// Called when the type to deserialize is an enum, as opposed to a field in the type.
fn deserialize_enum<V>(
self,
_name: &'static str,
_variants: &'static [&'static str],
visitor: V,
) -> Result<V::Value, Error>
where
V: serde::de::Visitor<'de>,
{
if self.input.is_empty() {
Err(crate::easy::de::Error::custom(
"wanted exactly 1 element, found 0 elements",
))
} else if self.input.len() != 1 {
Err(crate::easy::de::Error::custom(
"wanted exactly 1 element, more than 1 element",
))
} else {
visitor.visit_enum(crate::easy::de::TableMapAccess::new(self.input))
}
}

serde::forward_to_deserialize_any! {
bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string seq
bytes byte_buf map unit newtype_struct
ignored_any unit_struct tuple_struct tuple identifier
}
}

pub(crate) struct TableMapAccess {
iter: indexmap::map::IntoIter<crate::InternalString, crate::table::TableKeyValue>,
value: Option<crate::Item>,
30 changes: 14 additions & 16 deletions src/easy/macros.rs
Original file line number Diff line number Diff line change
@@ -7,26 +7,24 @@ use crate::easy::value::{Array, Table, Value};
/// [`toml_edit::easy::Value`]: value/enum.Value.html
///
/// ```rust
/// fn main() {
/// let cargo_toml = toml_edit::easy::toml! {
/// [package]
/// name = "toml"
/// version = "0.4.5"
/// authors = ["Alex Crichton <alex@alexcrichton.com>"]
/// let cargo_toml = toml_edit::easy::toml! {
/// [package]
/// name = "toml"
/// version = "0.4.5"
/// authors = ["Alex Crichton <alex@alexcrichton.com>"]
///
/// [badges]
/// travis-ci = { repository = "alexcrichton/toml-rs" }
/// [badges]
/// travis-ci = { repository = "alexcrichton/toml-rs" }
///
/// [dependencies]
/// serde = "1.0"
/// [dependencies]
/// serde = "1.0"
///
/// [dev-dependencies]
/// serde_derive = "1.0"
/// serde_json = "1.0"
/// };
/// [dev-dependencies]
/// serde_derive = "1.0"
/// serde_json = "1.0"
/// };
///
/// println!("{:#?}", cargo_toml);
/// }
/// println!("{:#?}", cargo_toml);
/// ```
#[macro_export]
macro_rules! toml {
7 changes: 6 additions & 1 deletion src/easy/mod.rs
Original file line number Diff line number Diff line change
@@ -2,7 +2,12 @@
//!
//! This library implements a [TOML] v0.5.0 compatible parser,
//! primarily supporting the [`serde`] library for encoding/decoding
//! various types in Rust.
//! various types in Rust
//!
//! **NOTE:** Unlike the core `toml_edit` API, this is not format preserving but is a drop-in
//! replacement for [toml-rs](https://github.com/alexcrichton/toml-rs) for those needing both
//! `toml_edit` and `toml-rs` but want either consistency in behavior or to reduce their
//! dependencies.
//!
//! TOML itself is a simple, ergonomic, and readable configuration format:
//!
4 changes: 2 additions & 2 deletions src/easy/ser/table.rs
Original file line number Diff line number Diff line change
@@ -39,7 +39,7 @@ impl serde::ser::SerializeMap for SerializeDocument {

fn end(self) -> Result<Self::Ok, Self::Error> {
self.inner.end().map(|root| crate::Document {
root: crate::Item::Table(root),
root,
..Default::default()
})
}
@@ -62,7 +62,7 @@ impl serde::ser::SerializeStruct for SerializeDocument {

fn end(self) -> Result<Self::Ok, Self::Error> {
self.inner.end().map(|root| crate::Document {
root: crate::Item::Table(root),
root,
..Default::default()
})
}
Loading