From 59d558f83087a385704af595f15fc6263d19ac3d Mon Sep 17 00:00:00 2001 From: Maarten de Vries Date: Fri, 12 Sep 2025 13:20:25 +0200 Subject: [PATCH 1/2] Implement `defmt::Format` for EscapedStr and related types. --- CHANGELOG.md | 4 ++++ src/str.rs | 11 +++++++---- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 208cc839..af45cdf0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] +### Added + +- Implement `defmt::Format` for `EscapedStr`, `EscapedStringFragment` and `StringUnescapeError`. + ## [v0.6.0] - 2024-08-07 ### Breaking diff --git a/src/str.rs b/src/str.rs index ab122485..80eccfcb 100644 --- a/src/str.rs +++ b/src/str.rs @@ -2,8 +2,9 @@ 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), @@ -11,8 +12,9 @@ pub enum EscapedStringFragment<'a> { 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, @@ -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" ), } } @@ -88,7 +90,7 @@ fn unescape_next_fragment( /// #[serde(borrow)] /// description: serde_json_core::str::EscapedStr<'a>, /// } -/// +/// /// serde_json_core::de::from_str_escaped::>( /// r#"{ "name": "Party\u0021", "description": "I'm throwing a party! Hopefully the \u2600 shines!" }"#, /// &mut [0; 8], @@ -97,6 +99,7 @@ fn unescape_next_fragment( /// ``` #[derive(Debug, Clone, Copy, 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> { From 6b2f594c333d1b6fead3fa71db5d73eaad1b5e9e Mon Sep 17 00:00:00 2001 From: Maarten de Vries Date: Fri, 12 Sep 2025 13:21:21 +0200 Subject: [PATCH 2/2] Implement `Default` for EscapedStr. --- CHANGELOG.md | 1 + src/str.rs | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index af45cdf0..6fa57126 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Added - Implement `defmt::Format` for `EscapedStr`, `EscapedStringFragment` and `StringUnescapeError`. +- Implement `Default` for `EscapedStr` (returning an empty string). ## [v0.6.0] - 2024-08-07 diff --git a/src/str.rs b/src/str.rs index 80eccfcb..2a1b8e2f 100644 --- a/src/str.rs +++ b/src/str.rs @@ -97,7 +97,7 @@ fn unescape_next_fragment( /// ) /// .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);