Use cargo features.

This commit is contained in:
Ethiraric 2024-03-30 19:24:54 +01:00
parent 0f1dedc584
commit f166970a3e
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
`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**:

View file

@ -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]

View file

@ -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 {

View file

@ -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

View file

@ -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)]

View file

@ -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<Cow<'static, str>>;
/// 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<T: std::io::Read> {
source: T,
trap: YAMLDecodingTrap,
}
#[cfg(feature = "encoding")]
impl<T: std::io::Read> YamlDecoder<T> {
/// Create a `YamlDecoder` decoding the given source.
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.
#[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 <https://yaml.org/spec/1.2/spec.html#id2771184>
#[cfg(feature = "encoding")]
fn detect_utf16_endianness(b: &[u8]) -> &'static Encoding {
if b.len() > 1 && (b[0] != b[1]) {
if b[0] == 0 {