Use cargo features.

This commit is contained in:
Ethiraric 2024-03-30 19:24:54 +01:00
parent 1fb76ae745
commit c90c1ed1fb
6 changed files with 41 additions and 5 deletions

View file

@ -12,6 +12,16 @@
* The inner `Yaml` variant doesn't match `Yaml::Array` for `usize` or * The inner `Yaml` variant doesn't match `Yaml::Array` for `usize` or
`Yaml::Hash` for `&'a str` `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 ## v0.8.0
**Breaking Changes**: **Breaking Changes**:

View file

@ -12,10 +12,16 @@ description = "A fully YAML 1.2 compliant YAML parser"
repository = "https://github.com/Ethiraric/yaml-rust2" repository = "https://github.com/Ethiraric/yaml-rust2"
readme = "README.md" readme = "README.md"
edition = "2021" edition = "2021"
rust-version = "1.65.0"
[features]
default = [ "encoding" ]
debug_prints = []
encoding = [ "dep:encoding_rs" ]
[dependencies] [dependencies]
arraydeque = "0.5.1" arraydeque = "0.5.1"
encoding_rs = "0.8.33" encoding_rs = { version = "0.8.33", optional = true }
hashlink = "0.8" hashlink = "0.8"
[dev-dependencies] [dev-dependencies]

View file

@ -7,17 +7,17 @@
//! build, debug helpers will only trigger if that variable is set when running the program. //! 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. // If a debug build, use stuff in the debug submodule.
#[cfg(debug_assertions)] #[cfg(feature = "debug_prints")]
pub use debug::enabled; pub use debug::enabled;
// Otherwise, just export dummies for publicly visible functions. // Otherwise, just export dummies for publicly visible functions.
/// Evaluates to nothing. /// Evaluates to nothing.
#[cfg(not(debug_assertions))] #[cfg(not(feature = "debug_prints"))]
macro_rules! debug_print { macro_rules! debug_print {
($($arg:tt)*) => {{}}; ($($arg:tt)*) => {{}};
} }
#[cfg(debug_assertions)] #[cfg(feature = "debug_prints")]
#[macro_use] #[macro_use]
#[allow(clippy::module_inception)] #[allow(clippy::module_inception)]
mod debug { mod debug {

View file

@ -345,7 +345,7 @@ impl<'a> YamlEmitter<'a> {
/// Strings starting with any of the following characters must be quoted. /// Strings starting with any of the following characters must be quoted.
/// :, &, *, ?, |, -, <, >, =, !, %, @ /// :, &, *, ?, |, -, <, >, =, !, %, @
/// Strings containing 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: /// 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 /// \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

View file

@ -29,6 +29,19 @@
//! emitter.dump(doc).unwrap(); // dump the YAML object to a String //! 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)] #![warn(missing_docs, clippy::pedantic)]

View file

@ -6,6 +6,7 @@ use std::borrow::Cow;
use std::ops::ControlFlow; use std::ops::ControlFlow;
use std::{collections::BTreeMap, convert::TryFrom, mem, ops::Index, ops::IndexMut}; use std::{collections::BTreeMap, convert::TryFrom, mem, ops::Index, ops::IndexMut};
#[cfg(feature = "encoding")]
use encoding_rs::{Decoder, DecoderResult, Encoding}; use encoding_rs::{Decoder, DecoderResult, Encoding};
use hashlink::LinkedHashMap; use hashlink::LinkedHashMap;
@ -279,6 +280,7 @@ impl YamlLoader {
/// # Returns /// # Returns
/// The function must return [`ControlFlow::Continue`] if decoding may continue or /// 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. /// [`ControlFlow::Break`] if decoding must be aborted. An optional error string may be supplied.
#[cfg(feature = "encoding")]
pub type YAMLDecodingTrapFn = fn( pub type YAMLDecodingTrapFn = fn(
malformation_length: u8, malformation_length: u8,
bytes_read_after_malformation: u8, bytes_read_after_malformation: u8,
@ -287,6 +289,7 @@ pub type YAMLDecodingTrapFn = fn(
) -> ControlFlow<Cow<'static, str>>; ) -> ControlFlow<Cow<'static, str>>;
/// The behavior [`YamlDecoder`] must have when an decoding error occurs. /// The behavior [`YamlDecoder`] must have when an decoding error occurs.
#[cfg(feature = "encoding")]
#[derive(Copy, Clone, PartialEq, Eq)] #[derive(Copy, Clone, PartialEq, Eq)]
pub enum YAMLDecodingTrap { pub enum YAMLDecodingTrap {
/// Ignore the offending bytes, remove them from the output. /// Ignore the offending bytes, remove them from the output.
@ -315,11 +318,13 @@ pub enum YAMLDecodingTrap {
/// .decode() /// .decode()
/// .unwrap(); /// .unwrap();
/// ``` /// ```
#[cfg(feature = "encoding")]
pub struct YamlDecoder<T: std::io::Read> { pub struct YamlDecoder<T: std::io::Read> {
source: T, source: T,
trap: YAMLDecodingTrap, trap: YAMLDecodingTrap,
} }
#[cfg(feature = "encoding")]
impl<T: std::io::Read> YamlDecoder<T> { impl<T: std::io::Read> YamlDecoder<T> {
/// Create a `YamlDecoder` decoding the given source. /// Create a `YamlDecoder` decoding the given source.
pub fn read(source: T) -> YamlDecoder<T> { pub fn read(source: T) -> YamlDecoder<T> {
@ -358,6 +363,7 @@ impl<T: std::io::Read> YamlDecoder<T> {
} }
/// Perform a loop of [`Decoder::decode_to_string`], reallocating `output` if needed. /// Perform a loop of [`Decoder::decode_to_string`], reallocating `output` if needed.
#[cfg(feature = "encoding")]
fn decode_loop( fn decode_loop(
input: &[u8], input: &[u8],
output: &mut String, output: &mut String,
@ -433,6 +439,7 @@ fn decode_loop(
/// This allows the encoding to be deduced by the pattern of null (#x00) characters. /// This allows the encoding to be deduced by the pattern of null (#x00) characters.
// //
/// See spec at <https://yaml.org/spec/1.2/spec.html#id2771184> /// See spec at <https://yaml.org/spec/1.2/spec.html#id2771184>
#[cfg(feature = "encoding")]
fn detect_utf16_endianness(b: &[u8]) -> &'static Encoding { fn detect_utf16_endianness(b: &[u8]) -> &'static Encoding {
if b.len() > 1 && (b[0] != b[1]) { if b.len() > 1 && (b[0] != b[1]) {
if b[0] == 0 { if b[0] == 0 {