Skip to content

Commit

Permalink
Use cargo features.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ethiraric committed Mar 30, 2024
1 parent 4ca0fc4 commit 77306e1
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 5 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**:
Expand Down
8 changes: 7 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
6 changes: 3 additions & 3 deletions src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]

Expand Down
7 changes: 7 additions & 0 deletions src/yaml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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,
Expand All @@ -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.
Expand Down Expand Up @@ -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> {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 77306e1

Please sign in to comment.