From ef9fd0a0bda34c2a1b9fc55baa2ec5bf79726d12 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 6 Nov 2023 14:19:41 -0600 Subject: [PATCH] fix(edit)!: Allow disabling parser or display --- crates/toml/Cargo.toml | 4 +-- crates/toml_edit/Cargo.toml | 15 ++++++-- crates/toml_edit/src/array.rs | 7 ++++ crates/toml_edit/src/array_of_tables.rs | 1 + crates/toml_edit/src/de/mod.rs | 3 ++ crates/toml_edit/src/de/value.rs | 1 + crates/toml_edit/src/document.rs | 1 + crates/toml_edit/src/error.rs | 1 + crates/toml_edit/src/inline_table.rs | 5 +++ crates/toml_edit/src/item.rs | 4 +++ crates/toml_edit/src/key.rs | 38 ++++++++++++++++----- crates/toml_edit/src/lib.rs | 10 ++++++ crates/toml_edit/src/parser/array.rs | 2 ++ crates/toml_edit/src/parser/datetime.rs | 2 ++ crates/toml_edit/src/parser/error.rs | 17 +++++++-- crates/toml_edit/src/parser/inline_table.rs | 2 ++ crates/toml_edit/src/parser/key.rs | 2 ++ crates/toml_edit/src/parser/mod.rs | 2 ++ crates/toml_edit/src/parser/numbers.rs | 2 ++ crates/toml_edit/src/parser/strings.rs | 2 ++ crates/toml_edit/src/parser/trivia.rs | 2 ++ crates/toml_edit/src/parser/value.rs | 2 ++ crates/toml_edit/src/raw_string.rs | 2 ++ crates/toml_edit/src/repr.rs | 18 ++++++++++ crates/toml_edit/src/ser/mod.rs | 3 ++ crates/toml_edit/src/table.rs | 5 +++ crates/toml_edit/src/value.rs | 6 ++++ crates/toml_edit/src/visit.rs | 2 ++ crates/toml_edit/src/visit_mut.rs | 4 +++ 29 files changed, 151 insertions(+), 14 deletions(-) diff --git a/crates/toml/Cargo.toml b/crates/toml/Cargo.toml index 51d613ec..b8f88623 100644 --- a/crates/toml/Cargo.toml +++ b/crates/toml/Cargo.toml @@ -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 diff --git a/crates/toml_edit/Cargo.toml b/crates/toml_edit/Cargo.toml index 14dc8adb..2bd6ddbc 100644 --- a/crates/toml_edit/Cargo.toml +++ b/crates/toml_edit/Cargo.toml @@ -5,6 +5,7 @@ keywords = ["encoding", "toml"] categories = ["encoding", "parser-implementations", "parsing", "config"] description = "Yet another format-preserving TOML parser." authors = ["Andronik Ordian ", "Ed Page "] +autotests = false repository.workspace = true license.workspace = true edition.workspace = true @@ -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 @@ -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" } @@ -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 diff --git a/crates/toml_edit/src/array.rs b/crates/toml_edit/src/array.rs index 97033de5..69e7f3cc 100644 --- a/crates/toml_edit/src/array.rs +++ b/crates/toml_edit/src/array.rs @@ -183,9 +183,11 @@ impl Array { /// # Examples /// /// ```rust + /// # #[cfg(feature = "parse")] { /// let formatted_value = "'literal'".parse::().unwrap(); /// let mut arr = toml_edit::Array::new(); /// arr.push_formatted(formatted_value); + /// # } /// ``` pub fn push_formatted(&mut self, v: Value) { self.values.push(Item::Value(v)); @@ -223,12 +225,14 @@ impl Array { /// # Examples /// /// ```rust + /// # #[cfg(feature = "parse")] { /// let mut arr = toml_edit::Array::new(); /// arr.push(1); /// arr.push("foo"); /// /// let formatted_value = "'start'".parse::().unwrap(); /// arr.insert_formatted(0, formatted_value); + /// # } /// ``` pub fn insert_formatted(&mut self, index: usize, v: Value) { self.values.insert(index, Item::Value(v)) @@ -269,12 +273,14 @@ impl Array { /// # Examples /// /// ```rust + /// # #[cfg(feature = "parse")] { /// let mut arr = toml_edit::Array::new(); /// arr.push(1); /// arr.push("foo"); /// /// let formatted_value = "'start'".parse::().unwrap(); /// arr.replace_formatted(0, formatted_value); + /// # } /// ``` pub fn replace_formatted(&mut self, index: usize, v: Value) -> Value { match mem::replace(&mut self.values[index], Item::Value(v)) { @@ -383,6 +389,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, ("", "")) diff --git a/crates/toml_edit/src/array_of_tables.rs b/crates/toml_edit/src/array_of_tables.rs index c4d71948..2e602a2c 100644 --- a/crates/toml_edit/src/array_of_tables.rs +++ b/crates/toml_edit/src/array_of_tables.rs @@ -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 diff --git a/crates/toml_edit/src/de/mod.rs b/crates/toml_edit/src/de/mod.rs index 09ea1209..9b8a2c7b 100644 --- a/crates/toml_edit/src/de/mod.rs +++ b/crates/toml_edit/src/de/mod.rs @@ -87,6 +87,7 @@ impl From for crate::TomlError { impl std::error::Error for Error {} /// Convert a value into `T`. +#[cfg(feature = "parse")] pub fn from_str(s: &'_ str) -> Result where T: DeserializeOwned, @@ -96,6 +97,7 @@ where } /// Convert a value into `T`. +#[cfg(feature = "parse")] pub fn from_slice(s: &'_ [u8]) -> Result where T: DeserializeOwned, @@ -125,6 +127,7 @@ impl Deserializer { } } +#[cfg(feature = "parse")] impl std::str::FromStr for Deserializer { type Err = Error; diff --git a/crates/toml_edit/src/de/value.rs b/crates/toml_edit/src/de/value.rs index d3cf87fc..ba6ce6db 100644 --- a/crates/toml_edit/src/de/value.rs +++ b/crates/toml_edit/src/de/value.rs @@ -241,6 +241,7 @@ impl crate::Item { } } +#[cfg(feature = "parse")] impl std::str::FromStr for ValueDeserializer { type Err = Error; diff --git a/crates/toml_edit/src/document.rs b/crates/toml_edit/src/document.rs index a0d6e5ee..f20e61a9 100644 --- a/crates/toml_edit/src/document.rs +++ b/crates/toml_edit/src/document.rs @@ -77,6 +77,7 @@ impl Default for Document { } } +#[cfg(feature = "parse")] impl FromStr for Document { type Err = crate::TomlError; diff --git a/crates/toml_edit/src/error.rs b/crates/toml_edit/src/error.rs index 82c9d245..a9830198 100644 --- a/crates/toml_edit/src/error.rs +++ b/crates/toml_edit/src/error.rs @@ -11,6 +11,7 @@ pub struct TomlError { } impl TomlError { + #[cfg(feature = "parse")] pub(crate) fn new( error: winnow::error::ParseError< crate::parser::prelude::Input<'_>, diff --git a/crates/toml_edit/src/inline_table.rs b/crates/toml_edit/src/inline_table.rs index cbd64adb..136c1e8f 100644 --- a/crates/toml_edit/src/inline_table.rs +++ b/crates/toml_edit/src/inline_table.rs @@ -146,11 +146,15 @@ impl InlineTable { /// In the document above, tables `target` and `target."x86_64/windows.json"` are implicit. /// /// ``` + /// # #[cfg(feature = "parse")] { + /// # #[cfg(feature = "display")] { /// use toml_edit::Document; /// let mut doc = "[a]\n[a.b]\n".parse::().expect("invalid toml"); /// /// doc["a"].as_table_mut().unwrap().set_implicit(true); /// assert_eq!(doc.to_string(), "[a.b]\n"); + /// # } + /// # } /// ``` pub(crate) fn set_implicit(&mut self, implicit: bool) { self.implicit = implicit; @@ -411,6 +415,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, ("", "")) diff --git a/crates/toml_edit/src/item.rs b/crates/toml_edit/src/item.rs index a1405631..b58806e4 100644 --- a/crates/toml_edit/src/item.rs +++ b/crates/toml_edit/src/item.rs @@ -329,6 +329,7 @@ impl Clone for Item { } } +#[cfg(feature = "parse")] impl FromStr for Item { type Err = crate::TomlError; @@ -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 { @@ -358,6 +360,7 @@ impl std::fmt::Display for Item { /// /// # Examples /// ```rust +/// # #[cfg(feature = "display")] { /// # use snapbox::assert_eq; /// # use toml_edit::*; /// let mut table = Table::default(); @@ -372,6 +375,7 @@ impl std::fmt::Display for Item { /// key2 = 42 /// key3 = ["hello", '\, world'] /// "#); +/// # } /// ``` pub fn value>(v: V) -> Item { Item::Value(v.into()) diff --git a/crates/toml_edit/src/key.rs b/crates/toml_edit/src/key.rs index 33ee1c65..2f99f708 100644 --- a/crates/toml_edit/src/key.rs +++ b/crates/toml_edit/src/key.rs @@ -46,6 +46,7 @@ impl Key { /// Parse a TOML key expression /// /// Unlike `"".parse()`, this supports dotted keys. + #[cfg(feature = "parse")] pub fn parse(repr: &str) -> Result, crate::TomlError> { Self::try_parse_path(repr) } @@ -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()) @@ -124,12 +127,14 @@ impl Key { self.decor.clear(); } + #[cfg(feature = "parse")] fn try_parse_simple(s: &str) -> Result { let mut key = crate::parser::parse_key(s)?; key.despan(s); Ok(key) } + #[cfg(feature = "parse")] fn try_parse_path(s: &str) -> Result, crate::TomlError> { let mut keys = crate::parser::parse_key_path(s)?; for key in &mut keys { @@ -206,12 +211,14 @@ impl PartialEq 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; @@ -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), @@ -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 { self.key.display_repr() } @@ -344,6 +365,7 @@ impl<'s> PartialEq 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) diff --git a/crates/toml_edit/src/lib.rs b/crates/toml_edit/src/lib.rs index 9b973682..25e3d204 100644 --- a/crates/toml_edit/src/lib.rs +++ b/crates/toml_edit/src/lib.rs @@ -14,6 +14,8 @@ //! # Example //! //! ```rust +//! # #[cfg(feature = "parse")] { +//! # #[cfg(feature = "display")] { //! use toml_edit::{Document, value}; //! //! let toml = r#" @@ -32,26 +34,32 @@ //! c = { d = "hello" } //! "#; //! assert_eq!(doc.to_string(), expected); +//! # } +//! # } //! ``` //! //! ## Controlling formatting //! //! By default, values are created with default formatting //! ```rust +//! # #[cfg(feature = "display")] { //! let mut doc = toml_edit::Document::new(); //! doc["foo"] = toml_edit::value("bar"); //! let expected = r#"foo = "bar" //! "#; //! assert_eq!(doc.to_string(), expected); +//! # } //! ``` //! //! You can choose a custom TOML representation by parsing the value. //! ```rust +//! # #[cfg(feature = "display")] { //! let mut doc = toml_edit::Document::new(); //! doc["foo"] = "'bar'".parse::().unwrap(); //! let expected = r#"foo = 'bar' //! "#; //! assert_eq!(doc.to_string(), expected); +//! # } //! ``` //! //! ## Limitations @@ -65,6 +73,7 @@ mod array; mod array_of_tables; mod document; +#[cfg(feature = "display")] mod encode; mod error; mod index; @@ -72,6 +81,7 @@ mod inline_table; mod internal_string; mod item; mod key; +#[cfg(feature = "parse")] mod parser; mod raw_string; mod repr; diff --git a/crates/toml_edit/src/parser/array.rs b/crates/toml_edit/src/parser/array.rs index e3b1f3f5..07831919 100644 --- a/crates/toml_edit/src/parser/array.rs +++ b/crates/toml_edit/src/parser/array.rs @@ -81,6 +81,8 @@ pub(crate) fn array_value<'i>( } #[cfg(test)] +#[cfg(feature = "parse")] +#[cfg(feature = "display")] mod test { use super::*; diff --git a/crates/toml_edit/src/parser/datetime.rs b/crates/toml_edit/src/parser/datetime.rs index 636620b0..945dc693 100644 --- a/crates/toml_edit/src/parser/datetime.rs +++ b/crates/toml_edit/src/parser/datetime.rs @@ -263,6 +263,8 @@ pub(crate) fn unsigned_digits<'i, const MIN: usize, const MAX: usize>( const DIGIT: RangeInclusive = b'0'..=b'9'; #[cfg(test)] +#[cfg(feature = "parse")] +#[cfg(feature = "display")] mod test { use super::*; diff --git a/crates/toml_edit/src/parser/error.rs b/crates/toml_edit/src/parser/error.rs index fd4a2114..22e8e662 100644 --- a/crates/toml_edit/src/parser/error.rs +++ b/crates/toml_edit/src/parser/error.rs @@ -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()), } } diff --git a/crates/toml_edit/src/parser/inline_table.rs b/crates/toml_edit/src/parser/inline_table.rs index d66eadd6..c2e6619a 100644 --- a/crates/toml_edit/src/parser/inline_table.rs +++ b/crates/toml_edit/src/parser/inline_table.rs @@ -165,6 +165,8 @@ fn keyval<'i>( } #[cfg(test)] +#[cfg(feature = "parse")] +#[cfg(feature = "display")] mod test { use super::*; diff --git a/crates/toml_edit/src/parser/key.rs b/crates/toml_edit/src/parser/key.rs index 9ed76c8f..71b75630 100644 --- a/crates/toml_edit/src/parser/key.rs +++ b/crates/toml_edit/src/parser/key.rs @@ -88,6 +88,8 @@ const UNQUOTED_CHAR: ( const DOT_SEP: u8 = b'.'; #[cfg(test)] +#[cfg(feature = "parse")] +#[cfg(feature = "display")] mod test { use super::*; diff --git a/crates/toml_edit/src/parser/mod.rs b/crates/toml_edit/src/parser/mod.rs index 1440cdc0..e0322024 100644 --- a/crates/toml_edit/src/parser/mod.rs +++ b/crates/toml_edit/src/parser/mod.rs @@ -140,6 +140,8 @@ pub(crate) mod prelude { } #[cfg(test)] +#[cfg(feature = "parse")] +#[cfg(feature = "display")] mod test { use super::*; diff --git a/crates/toml_edit/src/parser/numbers.rs b/crates/toml_edit/src/parser/numbers.rs index 4c77f51c..96815268 100644 --- a/crates/toml_edit/src/parser/numbers.rs +++ b/crates/toml_edit/src/parser/numbers.rs @@ -319,6 +319,8 @@ pub(crate) const HEXDIG: (RangeInclusive, RangeInclusive, RangeInclusive (DIGIT, b'A'..=b'F', b'a'..=b'f'); #[cfg(test)] +#[cfg(feature = "parse")] +#[cfg(feature = "display")] mod test { use super::*; diff --git a/crates/toml_edit/src/parser/strings.rs b/crates/toml_edit/src/parser/strings.rs index dacaf582..675b5c67 100644 --- a/crates/toml_edit/src/parser/strings.rs +++ b/crates/toml_edit/src/parser/strings.rs @@ -363,6 +363,8 @@ fn mll_quotes<'i>( } #[cfg(test)] +#[cfg(feature = "parse")] +#[cfg(feature = "display")] mod test { use super::*; diff --git a/crates/toml_edit/src/parser/trivia.rs b/crates/toml_edit/src/parser/trivia.rs index a359805b..4575fb15 100644 --- a/crates/toml_edit/src/parser/trivia.rs +++ b/crates/toml_edit/src/parser/trivia.rs @@ -120,6 +120,8 @@ pub(crate) fn line_trailing(input: &mut Input<'_>) -> PResult) -> Result std::fmt::Result { let raw = self.to_str(input); for part in raw.split('\r') { @@ -88,6 +89,7 @@ impl RawString { Ok(()) } + #[cfg(feature = "display")] pub(crate) fn encode_with_default( &self, buf: &mut dyn std::fmt::Write, diff --git a/crates/toml_edit/src/repr.rs b/crates/toml_edit/src/repr.rs index c4535e5a..7b408f4b 100644 --- a/crates/toml_edit/src/repr.rs +++ b/crates/toml_edit/src/repr.rs @@ -44,11 +44,13 @@ where } /// Returns the default raw representation. + #[cfg(feature = "display")] pub fn default_repr(&self) -> Repr { self.value.to_repr() } /// Returns a raw representation. + #[cfg(feature = "display")] pub fn display_repr(&self) -> Cow { self.as_repr() .and_then(|r| r.as_raw().as_str()) @@ -103,6 +105,7 @@ where } } +#[cfg(feature = "display")] impl std::fmt::Display for Formatted where T: ValueRepr, @@ -114,9 +117,21 @@ where pub trait ValueRepr: crate::private::Sealed { /// The TOML representation of the value + #[cfg(feature = "display")] fn to_repr(&self) -> Repr; } +#[cfg(not(feature = "display"))] +mod inner { + use super::ValueRepr; + + impl ValueRepr for String {} + impl ValueRepr for i64 {} + impl ValueRepr for f64 {} + impl ValueRepr for bool {} + impl ValueRepr for toml_datetime::Datetime {} +} + /// TOML-encoded value #[derive(Eq, PartialEq, Clone, Hash)] pub struct Repr { @@ -144,6 +159,7 @@ impl Repr { self.raw_value.despan(input) } + #[cfg(feature = "display")] pub(crate) fn encode(&self, buf: &mut dyn std::fmt::Write, input: &str) -> std::fmt::Result { self.as_raw().encode(buf, input) } @@ -185,6 +201,7 @@ impl Decor { self.prefix.as_ref() } + #[cfg(feature = "display")] pub(crate) fn prefix_encode( &self, buf: &mut dyn std::fmt::Write, @@ -208,6 +225,7 @@ impl Decor { self.suffix.as_ref() } + #[cfg(feature = "display")] pub(crate) fn suffix_encode( &self, buf: &mut dyn std::fmt::Write, diff --git a/crates/toml_edit/src/ser/mod.rs b/crates/toml_edit/src/ser/mod.rs index 7f999302..ba31708b 100644 --- a/crates/toml_edit/src/ser/mod.rs +++ b/crates/toml_edit/src/ser/mod.rs @@ -84,6 +84,7 @@ impl std::error::Error for Error {} /// Serialization can fail if `T`'s implementation of `Serialize` decides to /// fail, if `T` contains a map with non-string keys, or if `T` attempts to /// serialize an unsupported datatype such as an enum, tuple, or tuple struct. +#[cfg(feature = "display")] pub fn to_vec(value: &T) -> Result, Error> where T: serde::ser::Serialize, @@ -127,6 +128,7 @@ where /// let toml = toml_edit::ser::to_string(&config).unwrap(); /// println!("{}", toml) /// ``` +#[cfg(feature = "display")] pub fn to_string(value: &T) -> Result where T: serde::ser::Serialize, @@ -138,6 +140,7 @@ where /// /// This is identical to `to_string` except the output string has a more /// "pretty" output. See `ValueSerializer::pretty` for more details. +#[cfg(feature = "display")] pub fn to_string_pretty(value: &T) -> Result where T: serde::ser::Serialize, diff --git a/crates/toml_edit/src/table.rs b/crates/toml_edit/src/table.rs index 89302890..0f44a8e4 100644 --- a/crates/toml_edit/src/table.rs +++ b/crates/toml_edit/src/table.rs @@ -165,11 +165,15 @@ impl Table { /// In the document above, tables `target` and `target."x86_64/windows.json"` are implicit. /// /// ``` + /// # #[cfg(feature = "parse")] { + /// # #[cfg(feature = "display")] { /// use toml_edit::Document; /// let mut doc = "[a]\n[a.b]\n".parse::().expect("invalid toml"); /// /// doc["a"].as_table_mut().unwrap().set_implicit(true); /// assert_eq!(doc.to_string(), "[a.b]\n"); + /// # } + /// # } /// ``` pub fn set_implicit(&mut self, implicit: bool) { self.implicit = implicit; @@ -413,6 +417,7 @@ impl Table { } } +#[cfg(feature = "display")] impl std::fmt::Display for Table { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { use crate::encode::Encode; diff --git a/crates/toml_edit/src/value.rs b/crates/toml_edit/src/value.rs index 38edc613..1b397d90 100644 --- a/crates/toml_edit/src/value.rs +++ b/crates/toml_edit/src/value.rs @@ -189,10 +189,12 @@ impl Value { /// Sets the prefix and the suffix for value. /// # Example /// ```rust + /// # #[cfg(feature = "display")] { /// let mut v = toml_edit::Value::from(42); /// assert_eq!(&v.to_string(), "42"); /// let d = v.decorated(" ", " "); /// assert_eq!(&d.to_string(), " 42 "); + /// # } /// ``` pub fn decorated(mut self, prefix: impl Into, suffix: impl Into) -> Self { self.decorate(prefix, suffix); @@ -230,6 +232,7 @@ impl Value { } } +#[cfg(feature = "parse")] impl FromStr for Value { type Err = crate::TomlError; @@ -346,6 +349,7 @@ impl, V: Into> FromIterator<(K, V)> for Value { } } +#[cfg(feature = "display")] impl std::fmt::Display for Value { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { crate::encode::Encode::encode(self, f, None, ("", "")) @@ -360,6 +364,8 @@ pub(crate) const DEFAULT_TRAILING_VALUE_DECOR: (&str, &str) = (" ", " "); pub(crate) const DEFAULT_LEADING_VALUE_DECOR: (&str, &str) = ("", ""); #[cfg(test)] +#[cfg(feature = "parse")] +#[cfg(feature = "display")] mod tests { use super::*; diff --git a/crates/toml_edit/src/visit.rs b/crates/toml_edit/src/visit.rs index 2a5a473d..6d7be0b8 100644 --- a/crates/toml_edit/src/visit.rs +++ b/crates/toml_edit/src/visit.rs @@ -43,6 +43,7 @@ //! This visitor stores every string in the document. //! //! ``` +//! # #[cfg(feature = "parse")] { //! # use toml_edit::*; //! use toml_edit::visit::*; //! @@ -67,6 +68,7 @@ //! visitor.visit_document(&document); //! //! assert_eq!(visitor.strings, vec!["sky-castle", "surrounds-you"]); +//! # } //! ``` //! //! For a more complex example where the visitor has internal state, see `examples/visit.rs` diff --git a/crates/toml_edit/src/visit_mut.rs b/crates/toml_edit/src/visit_mut.rs index 35cf3653..c823cfbc 100644 --- a/crates/toml_edit/src/visit_mut.rs +++ b/crates/toml_edit/src/visit_mut.rs @@ -45,6 +45,8 @@ //! 2 decimal points. //! //! ``` +//! # #[cfg(feature = "parse")] { +//! # #[cfg(feature = "display")] { //! # use toml_edit::*; //! use toml_edit::visit_mut::*; //! @@ -80,6 +82,8 @@ //! "#; //! //! assert_eq!(format!("{}", document), output); +//! # } +//! # } //! ``` //! //! For a more complex example where the visitor has internal state, see `examples/visit.rs`