#![no_std]
#![forbid(unsafe_code)]
#![warn(clippy::pedantic, clippy::perf, missing_docs, clippy::panic, clippy::cargo)]
#![allow(clippy::type_complexity)]
#![cfg_attr(docsrs, feature(doc_cfg))]
/*!
# descape
Provides utilities for easily parsing escape sequences in a string via [`UnescapeExt`], using [`alloc::borrow::Cow`] to only borrow when needed.
This library supports many escape sequences:
- `\\a` -> `\x07`
- `\\b` -> `\x08`
- `\\t` -> `\x09`
- `\\n` -> `\x0A`
- `\\v` -> `\x0B`
- `\\f` -> `\x0C`
- `\\r` -> `\x0D`
- `\\e` -> `\x1B`
- `\\'` -> `'`
- `\\"` -> `"`
- \\` -> `
- `\\\\` -> `\\`
- `\\xNN` -> `\xNN`
- `\\o` -> `\o`, for all octal digits `o`
- `\\oo` -> `\oo`, for all octal digits `o`
- `\\ooo` -> `\ooo`, for all octal digits `o`
- `\\uXXXX` -> `\u{XXXX}`
- `\\u{HEX}` -> `\u{HEX}`
Along with this, you can define your own custom escape handlers! See [`UnescapeExt::to_unescaped_with`] for more information on that.
This crate supports `no-std`.
Optionally, this crate has the `std` and `core_error` features,
to allow the error type of an invalid escape to implement the `Error` trait.
`std` uses `std::error::Error`, and `core_error` depends on `core::error::Error`, which is stable on Rust 1.82.0 or greater.
*/
#[cfg(any(feature = "std", docsrs))]
extern crate std;
#[cfg(any(feature = "std", docsrs))]
use std::error::Error as ErrorTrait;
#[cfg(all(feature = "core_error", not(feature = "std")))]
use core::error::Error as ErrorTrait;
extern crate alloc;
use alloc::{
borrow::Cow,
string::String,
str::CharIndices
};
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Default, Hash)]
/// An error representing an invalid escape sequence in a string.
pub struct InvalidEscape {
/// The index of the invalid escape sequence.
pub index: usize,
}
impl InvalidEscape {
/// Constructs an invalid escape error from an index.
#[must_use]
pub const fn new(index: usize) -> Self {
Self { index }
}
}
impl core::fmt::Display for InvalidEscape {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "invalid escape sequence at index {}", self.index)?;
Ok(())
}
}
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "core_error"))))]
#[cfg(any(feature = "std", feature = "core_error", docsrs))]
impl ErrorTrait for InvalidEscape {}
/// An enum defining all possible non-error returns for a macro.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum EscapeValue<'s> {
/// Remove the escape sequence entirely.
Remove,
/// Leave the escape sequence as is.
Skip,
/// Replace the escape sequence with a single character.
Character(char),
/// Replace the escape sequence with a string of characters.
String(Cow<'s, str>)
}
impl From for EscapeValue<'static> {
fn from(value: char) -> Self {
Self::Character(value)
}
}
impl<'string> From<&'string str> for EscapeValue<'string> {
fn from(value: &'string str) -> Self {
Self::String(Cow::Borrowed(value))
}
}
impl<'string> From> for EscapeValue<'string> {
fn from(value: Cow<'string, str>) -> Self {
Self::String(value)
}
}
impl<'string> From for EscapeValue<'string> {
fn from(value: String) -> Self {
Self::String(Cow::Owned(value))
}
}
/// A trait distinguishing an object as a handler for custom escape sequences.
///
/// For convenience, this trait is **automatically implemented** for all implementors of `FnMut` with the correct signature.
///
pub trait EscapeHandler {
/// Definition of a custom escape handler.
///
/// Custom escape handlers are called before parsing any escape sequences,
/// and are given 3 arguments:
/// - `idx`: The index of the current character (e.g. `Hello\nthere` gets `5`)
/// - `chr`: The current character in the string (e.g. `\\n` gets `'n'`)
/// - `iter`: A mutable reference to the underlying character iterator -
/// use this to get the rest of the string via `CharIndices::as_str`,
/// or get the next characters
///
/// Handlers return a `Result