Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

### Added

- Implement `defmt::Format` for `EscapedStr`, `EscapedStringFragment` and `StringUnescapeError`.
- Implement `Default` for `EscapedStr` (returning an empty string).

## [v0.6.0] - 2024-08-07

### Breaking
Expand Down
13 changes: 8 additions & 5 deletions src/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@

use core::fmt;

#[derive(Debug)]
/// A fragment of an escaped string
#[derive(Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum EscapedStringFragment<'a> {
/// A series of characters which weren't escaped in the input.
NotEscaped(&'a str),
/// A character which was escaped in the input.
Escaped(char),
}

#[derive(Debug)]
/// Errors occuring while unescaping strings.
#[derive(Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum StringUnescapeError {
/// Failed to unescape a character due to an invalid escape sequence.
InvalidEscapeSequence,
Expand All @@ -23,7 +25,7 @@ impl fmt::Display for StringUnescapeError {
match self {
StringUnescapeError::InvalidEscapeSequence => write!(
f,
"Failed to unescape a character due to an invalid escape sequence."
"Failed to unescape a character due to an invalid escape sequence"
),
}
}
Expand Down Expand Up @@ -88,15 +90,16 @@ fn unescape_next_fragment(
/// #[serde(borrow)]
/// description: serde_json_core::str::EscapedStr<'a>,
/// }
///
///
/// serde_json_core::de::from_str_escaped::<Event<'_>>(
/// r#"{ "name": "Party\u0021", "description": "I'm throwing a party! Hopefully the \u2600 shines!" }"#,
/// &mut [0; 8],
/// )
/// .unwrap();
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(rename = "__serde_json_core_escaped_string__")]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct EscapedStr<'a>(pub &'a str);

impl<'a> EscapedStr<'a> {
Expand Down
Loading