From c90c1ed1fb93d65f51dd59593cc1e2b8c07f8379 Mon Sep 17 00:00:00 2001 From: Ethiraric Date: Sat, 30 Mar 2024 19:24:54 +0100 Subject: [PATCH] Use cargo features. --- parser/CHANGELOG.md | 10 ++++++++++ parser/Cargo.toml | 8 +++++++- parser/src/debug.rs | 6 +++--- parser/src/emitter.rs | 2 +- parser/src/lib.rs | 13 +++++++++++++ parser/src/yaml.rs | 7 +++++++ 6 files changed, 41 insertions(+), 5 deletions(-) diff --git a/parser/CHANGELOG.md b/parser/CHANGELOG.md index 603b5d3..4e3ebd2 100644 --- a/parser/CHANGELOG.md +++ b/parser/CHANGELOG.md @@ -12,6 +12,16 @@ * The inner `Yaml` variant doesn't match `Yaml::Array` for `usize` or `Yaml::Hash` for `&'a str` +- Use cargo features + + This allows for more fine-grained control over MSRV and to completely remove + debug code from the library when it is consumed. + + The `encoding` feature, governing the `YamlDecoder`, has been enabled by + default. Users of `@davvid`'s fork of `yaml-rust` or of `yaml-rust2` might + already use this. Users of the original `yaml-rust` crate may freely disable + this feature (`cargo <...> --no-default-features`) and lower MSRV to 1.65.0. + ## v0.8.0 **Breaking Changes**: diff --git a/parser/Cargo.toml b/parser/Cargo.toml index b75c474..f4756c1 100644 --- a/parser/Cargo.toml +++ b/parser/Cargo.toml @@ -12,10 +12,16 @@ description = "A fully YAML 1.2 compliant YAML parser" repository = "https://github.com/Ethiraric/yaml-rust2" readme = "README.md" edition = "2021" +rust-version = "1.65.0" + +[features] +default = [ "encoding" ] +debug_prints = [] +encoding = [ "dep:encoding_rs" ] [dependencies] arraydeque = "0.5.1" -encoding_rs = "0.8.33" +encoding_rs = { version = "0.8.33", optional = true } hashlink = "0.8" [dev-dependencies] diff --git a/parser/src/debug.rs b/parser/src/debug.rs index b43ea2e..c1411cb 100644 --- a/parser/src/debug.rs +++ b/parser/src/debug.rs @@ -7,17 +7,17 @@ //! build, debug helpers will only trigger if that variable is set when running the program. // If a debug build, use stuff in the debug submodule. -#[cfg(debug_assertions)] +#[cfg(feature = "debug_prints")] pub use debug::enabled; // Otherwise, just export dummies for publicly visible functions. /// Evaluates to nothing. -#[cfg(not(debug_assertions))] +#[cfg(not(feature = "debug_prints"))] macro_rules! debug_print { ($($arg:tt)*) => {{}}; } -#[cfg(debug_assertions)] +#[cfg(feature = "debug_prints")] #[macro_use] #[allow(clippy::module_inception)] mod debug { diff --git a/parser/src/emitter.rs b/parser/src/emitter.rs index 60eda8a..48c8b5c 100644 --- a/parser/src/emitter.rs +++ b/parser/src/emitter.rs @@ -345,7 +345,7 @@ impl<'a> YamlEmitter<'a> { /// Strings starting with any of the following characters must be quoted. /// :, &, *, ?, |, -, <, >, =, !, %, @ /// Strings containing any of the following characters must be quoted. -/// {, }, [, ], ,, #, ` +/// {, }, \[, t \], ,, #, ` /// /// If the string contains any of the following control characters, it must be escaped with double quotes: /// \0, \x01, \x02, \x03, \x04, \x05, \x06, \a, \b, \t, \n, \v, \f, \r, \x0e, \x0f, \x10, \x11, \x12, \x13, \x14, \x15, \x16, \x17, \x18, \x19, \x1a, \e, \x1c, \x1d, \x1e, \x1f, \N, \_, \L, \P diff --git a/parser/src/lib.rs b/parser/src/lib.rs index 5fc106b..a171691 100644 --- a/parser/src/lib.rs +++ b/parser/src/lib.rs @@ -29,6 +29,19 @@ //! emitter.dump(doc).unwrap(); // dump the YAML object to a String //! //! ``` +//! +//! # Features +//! #### `encoding` (_enabled by default_) +//! Enables encoding-aware decoding of Yaml documents. +//! +//! This bumps MSRV up to `1.70.0`. +//! +//! #### `debug_prints` +//! Enables the `debug` module and usage of debug prints in the scanner and the parser. Do not +//! enable if you are consuming the crate rather than working on it as this can significantly +//! decrease performance. +//! +//! This bumps MSRV up to 1.70.0. #![warn(missing_docs, clippy::pedantic)] diff --git a/parser/src/yaml.rs b/parser/src/yaml.rs index 9486d1c..3c429d5 100644 --- a/parser/src/yaml.rs +++ b/parser/src/yaml.rs @@ -6,6 +6,7 @@ use std::borrow::Cow; use std::ops::ControlFlow; use std::{collections::BTreeMap, convert::TryFrom, mem, ops::Index, ops::IndexMut}; +#[cfg(feature = "encoding")] use encoding_rs::{Decoder, DecoderResult, Encoding}; use hashlink::LinkedHashMap; @@ -279,6 +280,7 @@ impl YamlLoader { /// # Returns /// The function must return [`ControlFlow::Continue`] if decoding may continue or /// [`ControlFlow::Break`] if decoding must be aborted. An optional error string may be supplied. +#[cfg(feature = "encoding")] pub type YAMLDecodingTrapFn = fn( malformation_length: u8, bytes_read_after_malformation: u8, @@ -287,6 +289,7 @@ pub type YAMLDecodingTrapFn = fn( ) -> ControlFlow>; /// The behavior [`YamlDecoder`] must have when an decoding error occurs. +#[cfg(feature = "encoding")] #[derive(Copy, Clone, PartialEq, Eq)] pub enum YAMLDecodingTrap { /// Ignore the offending bytes, remove them from the output. @@ -315,11 +318,13 @@ pub enum YAMLDecodingTrap { /// .decode() /// .unwrap(); /// ``` +#[cfg(feature = "encoding")] pub struct YamlDecoder { source: T, trap: YAMLDecodingTrap, } +#[cfg(feature = "encoding")] impl YamlDecoder { /// Create a `YamlDecoder` decoding the given source. pub fn read(source: T) -> YamlDecoder { @@ -358,6 +363,7 @@ impl YamlDecoder { } /// Perform a loop of [`Decoder::decode_to_string`], reallocating `output` if needed. +#[cfg(feature = "encoding")] fn decode_loop( input: &[u8], output: &mut String, @@ -433,6 +439,7 @@ fn decode_loop( /// This allows the encoding to be deduced by the pattern of null (#x00) characters. // /// See spec at +#[cfg(feature = "encoding")] fn detect_utf16_endianness(b: &[u8]) -> &'static Encoding { if b.len() > 1 && (b[0] != b[1]) { if b[0] == 0 {