Skip to content

Commit

Permalink
fix(edit)!: Allow disabling parser or display
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Nov 6, 2023
1 parent 5b53ff1 commit a42d946
Show file tree
Hide file tree
Showing 27 changed files with 119 additions and 14 deletions.
4 changes: 2 additions & 2 deletions crates/toml/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ pre-release-replacements = [

[features]
default = ["parse", "display"]
parse = ["dep:toml_edit"]
display = ["dep:toml_edit"]
parse = ["dep:toml_edit", "toml_edit?/parse"]
display = ["dep:toml_edit", "toml_edit?/display"]

# Use indexmap rather than BTreeMap as the map type of toml::Value.
# This allows data to be read into a Value and written back to a TOML string
Expand Down
15 changes: 13 additions & 2 deletions crates/toml_edit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ keywords = ["encoding", "toml"]
categories = ["encoding", "parser-implementations", "parsing", "config"]
description = "Yet another format-preserving TOML parser."
authors = ["Andronik Ordian <write@reusable.software>", "Ed Page <eopage@gmail.com>"]
autotests = false
repository.workspace = true
license.workspace = true
edition.workspace = true
Expand All @@ -26,7 +27,9 @@ pre-release-replacements = [
]

[features]
default = []
default = ["parse", "display"]
parse = ["dep:winnow"]
display = []
perf = ["dep:kstring"]
serde = ["dep:serde", "toml_datetime/serde", "dep:serde_spanned"]
# Provide a method disable_recursion_limit to parse arbitrarily deep structures
Expand All @@ -38,7 +41,7 @@ unbounded = []

[dependencies]
indexmap = { version = "2.0.0", features = ["std"] }
winnow = "0.5.0"
winnow = { version = "0.5.0", optional = true }
serde = { version = "1.0.145", optional = true }
kstring = { version = "2.0.0", features = ["max_inline"], optional = true }
toml_datetime = { version = "0.6.5", path = "../toml_datetime" }
Expand All @@ -51,18 +54,26 @@ toml-test-data = "1.4.0"
libtest-mimic = "0.6.0"
snapbox = { version = "0.4.11", features = ["harness"] }

[[test]]
name = "testsuite"
required-features = ["parse", "display"]

[[test]]
name = "decoder_compliance"
required-features = ["parse"]
harness = false

[[test]]
name = "encoder_compliance"
required-features = ["parse", "display"]
harness = false

[[test]]
name = "invalid"
required-features = ["parse"]
harness = false

[[example]]
name = "visit"
required-features = ["parse", "display"]
test = true
1 change: 1 addition & 0 deletions crates/toml_edit/src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,7 @@ impl Array {
}
}

#[cfg(feature = "display")]
impl std::fmt::Display for Array {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
crate::encode::Encode::encode(self, f, None, ("", ""))
Expand Down
1 change: 1 addition & 0 deletions crates/toml_edit/src/array_of_tables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ impl<'s> IntoIterator for &'s ArrayOfTables {
}
}

#[cfg(feature = "display")]
impl std::fmt::Display for ArrayOfTables {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
// HACK: Without the header, we don't really have a proper way of printing this
Expand Down
3 changes: 3 additions & 0 deletions crates/toml_edit/src/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ impl From<Error> for crate::TomlError {
impl std::error::Error for Error {}

/// Convert a value into `T`.
#[cfg(feature = "parse")]
pub fn from_str<T>(s: &'_ str) -> Result<T, Error>
where
T: DeserializeOwned,
Expand All @@ -96,6 +97,7 @@ where
}

/// Convert a value into `T`.
#[cfg(feature = "parse")]
pub fn from_slice<T>(s: &'_ [u8]) -> Result<T, Error>
where
T: DeserializeOwned,
Expand Down Expand Up @@ -125,6 +127,7 @@ impl Deserializer {
}
}

#[cfg(feature = "parse")]
impl std::str::FromStr for Deserializer {
type Err = Error;

Expand Down
1 change: 1 addition & 0 deletions crates/toml_edit/src/de/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ impl crate::Item {
}
}

#[cfg(feature = "parse")]
impl std::str::FromStr for ValueDeserializer {
type Err = Error;

Expand Down
1 change: 1 addition & 0 deletions crates/toml_edit/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ impl Default for Document {
}
}

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

Expand Down
1 change: 1 addition & 0 deletions crates/toml_edit/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub struct TomlError {
}

impl TomlError {
#[cfg(feature = "parse")]
pub(crate) fn new(
error: winnow::error::ParseError<
crate::parser::prelude::Input<'_>,
Expand Down
1 change: 1 addition & 0 deletions crates/toml_edit/src/inline_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,7 @@ impl InlineTable {
}
}

#[cfg(feature = "display")]
impl std::fmt::Display for InlineTable {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
crate::encode::Encode::encode(self, f, None, ("", ""))
Expand Down
2 changes: 2 additions & 0 deletions crates/toml_edit/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ impl Clone for Item {
}
}

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

Expand All @@ -339,6 +340,7 @@ impl FromStr for Item {
}
}

#[cfg(feature = "display")]
impl std::fmt::Display for Item {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match &self {
Expand Down
38 changes: 30 additions & 8 deletions crates/toml_edit/src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ impl Key {
/// Parse a TOML key expression
///
/// Unlike `"".parse<Key>()`, this supports dotted keys.
#[cfg(feature = "parse")]
pub fn parse(repr: &str) -> Result<Vec<Self>, crate::TomlError> {
Self::try_parse_path(repr)
}
Expand Down Expand Up @@ -81,11 +82,13 @@ impl Key {
}

/// Returns the default raw representation.
#[cfg(feature = "display")]
pub fn default_repr(&self) -> Repr {
to_key_repr(&self.key)
}

/// Returns a raw representation.
#[cfg(feature = "display")]
pub fn display_repr(&self) -> Cow<'_, str> {
self.as_repr()
.and_then(|r| r.as_raw().as_str())
Expand Down Expand Up @@ -124,12 +127,14 @@ impl Key {
self.decor.clear();
}

#[cfg(feature = "parse")]
fn try_parse_simple(s: &str) -> Result<Key, crate::TomlError> {
let mut key = crate::parser::parse_key(s)?;
key.despan(s);
Ok(key)
}

#[cfg(feature = "parse")]
fn try_parse_path(s: &str) -> Result<Vec<Key>, crate::TomlError> {
let mut keys = crate::parser::parse_key_path(s)?;
for key in &mut keys {
Expand Down Expand Up @@ -206,12 +211,14 @@ impl PartialEq<String> for Key {
}
}

#[cfg(feature = "display")]
impl std::fmt::Display for Key {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
crate::encode::Encode::encode(self, f, None, ("", ""))
}
}

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

Expand All @@ -223,16 +230,28 @@ impl FromStr for Key {
}
}

#[cfg(feature = "display")]
fn to_key_repr(key: &str) -> Repr {
if key
.as_bytes()
.iter()
.copied()
.all(crate::parser::key::is_unquoted_char)
&& !key.is_empty()
#[cfg(feature = "parse")]
{
if key
.as_bytes()
.iter()
.copied()
.all(crate::parser::key::is_unquoted_char)
&& !key.is_empty()
{
Repr::new_unchecked(key)
} else {
crate::encode::to_string_repr(
key,
Some(crate::encode::StringStyle::OnelineSingle),
Some(false),
)
}
}
#[cfg(not(feature = "parse"))]
{
Repr::new_unchecked(key)
} else {
crate::encode::to_string_repr(
key,
Some(crate::encode::StringStyle::OnelineSingle),
Expand Down Expand Up @@ -290,11 +309,13 @@ impl<'k> KeyMut<'k> {
}

/// Returns the default raw representation.
#[cfg(feature = "display")]
pub fn default_repr(&self) -> Repr {
self.key.default_repr()
}

/// Returns a raw representation.
#[cfg(feature = "display")]
pub fn display_repr(&self) -> Cow<str> {
self.key.display_repr()
}
Expand Down Expand Up @@ -344,6 +365,7 @@ impl<'s> PartialEq<String> for KeyMut<'s> {
}
}

#[cfg(feature = "display")]
impl<'k> std::fmt::Display for KeyMut<'k> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Display::fmt(&self.key, f)
Expand Down
2 changes: 2 additions & 0 deletions crates/toml_edit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,15 @@
mod array;
mod array_of_tables;
mod document;
#[cfg(feature = "display")]
mod encode;
mod error;
mod index;
mod inline_table;
mod internal_string;
mod item;
mod key;
#[cfg(feature = "parse")]
mod parser;
mod raw_string;
mod repr;
Expand Down
2 changes: 2 additions & 0 deletions crates/toml_edit/src/parser/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ pub(crate) fn array_value<'i>(
}

#[cfg(test)]
#[cfg(feature = "parse")]
#[cfg(feature = "display")]
mod test {
use super::*;

Expand Down
2 changes: 2 additions & 0 deletions crates/toml_edit/src/parser/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,8 @@ pub(crate) fn unsigned_digits<'i, const MIN: usize, const MAX: usize>(
const DIGIT: RangeInclusive<u8> = b'0'..=b'9';

#[cfg(test)]
#[cfg(feature = "parse")]
#[cfg(feature = "display")]
mod test {
use super::*;

Expand Down
17 changes: 15 additions & 2 deletions crates/toml_edit/src/parser/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,22 @@ impl CustomError {
pub(crate) fn duplicate_key(path: &[Key], i: usize) -> Self {
assert!(i < path.len());
let key = &path[i];
let repr = key.display_repr();
let repr = key
.as_repr()
.and_then(|key| key.as_raw().as_str())
.map(|s| s.to_owned())
.unwrap_or_else(|| {
#[cfg(feature = "display")]
{
key.default_repr().as_raw().as_str().unwrap().to_owned()
}
#[cfg(not(feature = "display"))]
{
format!("{:?}", key.get())
}
});
Self::DuplicateKey {
key: repr.into(),
key: repr,
table: Some(path[..i].to_vec()),
}
}
Expand Down
2 changes: 2 additions & 0 deletions crates/toml_edit/src/parser/inline_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ fn keyval<'i>(
}

#[cfg(test)]
#[cfg(feature = "parse")]
#[cfg(feature = "display")]
mod test {
use super::*;

Expand Down
2 changes: 2 additions & 0 deletions crates/toml_edit/src/parser/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ const UNQUOTED_CHAR: (
const DOT_SEP: u8 = b'.';

#[cfg(test)]
#[cfg(feature = "parse")]
#[cfg(feature = "display")]
mod test {
use super::*;

Expand Down
2 changes: 2 additions & 0 deletions crates/toml_edit/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ pub(crate) mod prelude {
}

#[cfg(test)]
#[cfg(feature = "parse")]
#[cfg(feature = "display")]
mod test {
use super::*;

Expand Down
2 changes: 2 additions & 0 deletions crates/toml_edit/src/parser/numbers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,8 @@ pub(crate) const HEXDIG: (RangeInclusive<u8>, RangeInclusive<u8>, RangeInclusive
(DIGIT, b'A'..=b'F', b'a'..=b'f');

#[cfg(test)]
#[cfg(feature = "parse")]
#[cfg(feature = "display")]
mod test {
use super::*;

Expand Down
2 changes: 2 additions & 0 deletions crates/toml_edit/src/parser/strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,8 @@ fn mll_quotes<'i>(
}

#[cfg(test)]
#[cfg(feature = "parse")]
#[cfg(feature = "display")]
mod test {
use super::*;

Expand Down
2 changes: 2 additions & 0 deletions crates/toml_edit/src/parser/trivia.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ pub(crate) fn line_trailing(input: &mut Input<'_>) -> PResult<std::ops::Range<us
}

#[cfg(test)]
#[cfg(feature = "parse")]
#[cfg(feature = "display")]
mod test {
use super::*;

Expand Down
2 changes: 2 additions & 0 deletions crates/toml_edit/src/parser/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ fn apply_raw(mut val: Value, span: std::ops::Range<usize>) -> Result<Value, std:
}

#[cfg(test)]
#[cfg(feature = "parse")]
#[cfg(feature = "display")]
mod test {
use super::*;

Expand Down

0 comments on commit a42d946

Please sign in to comment.