From cf0ab479e10fd45ca92aff2283cc75ee45b4a4fb Mon Sep 17 00:00:00 2001 From: Priit Laes Date: Thu, 8 Feb 2024 10:35:45 +0200 Subject: [PATCH 1/3] book: Add some examples for byte slice/array hints as well Initially struggled to figure this out as examples for these were missing. --- book/src/hints.md | 14 ++++++++++++++ parser/src/tests.rs | 3 +++ 2 files changed, 17 insertions(+) diff --git a/book/src/hints.md b/book/src/hints.md index 97a67781..3e130c75 100644 --- a/book/src/hints.md +++ b/book/src/hints.md @@ -74,6 +74,20 @@ defmt::info!("{=u8:#08X}", 42); // -> INFO 0x00002A When the alternate form is used for hex and binary, the `0x`/`0b` length is subtracted from the leading zeros. This matches [`core::fmt` behavior](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=b11809759f975e266251f7968e542756). +## Display hints for byte slice and byte array elements + +Besides ASCII hints, byte slice and array elements can be formatted using hexadecimal or binary hints: + +```rust +# extern crate defmt; +let bytes = [4, 101, 5, 108, 6, 111]; + +defmt::info!("{=[u8]}", bytes); // -> INFO [4, 101, 5, 108, 6, 111] +defmt::info!("{=[u8]:x}", bytes); // -> INFO [4, 65, 5, 6c, 6, 6f] +defmt::info!("{=[u8]:#04x}", bytes); // -> INFO [0x04, 0x65, 0x05, 0x6c, 0x06, 0x6f] +defmt::info!("{=[u8]:#010b}", bytes); // -> INFO [0b00000100, 0b01100101, 0b00000101, 0b01101100, 0b00000110, 0b01101111] +``` + ## Propagation Display hints "propagate downwards" and apply to formatting parameters that specify no display hint. diff --git a/parser/src/tests.rs b/parser/src/tests.rs index 4fc7735c..a57845ed 100644 --- a/parser/src/tests.rs +++ b/parser/src/tests.rs @@ -10,6 +10,7 @@ use super::*; #[case::two_param_type_hint("=u8:x", None, Type::U8, Some(DisplayHint::Hexadecimal {alternate: false, uppercase: false, zero_pad: 0}))] #[case::two_param_index_type("0=u8", Some(0), Type::U8, None)] #[case::two_param_index_hint("0:a", Some(0), Type::Format, Some(DisplayHint::Ascii))] +#[case::two_param_type_hint("=[u8]:#04x", None, Type::U8Slice, Some(DisplayHint::Hexadecimal {alternate: true, uppercase: false, zero_pad: 4}))] #[case::all_param("1=u8:b", Some(1), Type::U8, Some(DisplayHint::Binary { alternate: false, zero_pad: 0}))] fn all_parse_param_cases( #[case] input: &str, @@ -28,7 +29,9 @@ fn all_parse_param_cases( #[case(":b", DisplayHint::Binary { alternate: false, zero_pad: 0 })] #[case(":#b", DisplayHint::Binary { alternate: true, zero_pad: 0 })] #[case(":x", DisplayHint::Hexadecimal { alternate: false, uppercase: false, zero_pad: 0 })] +#[case(":02x", DisplayHint::Hexadecimal { alternate: false, uppercase: false, zero_pad: 2 })] #[case(":#x", DisplayHint::Hexadecimal { alternate: true, uppercase: false, zero_pad: 0 })] +#[case(":#04x", DisplayHint::Hexadecimal { alternate: true, uppercase: false, zero_pad: 4 })] #[case(":X", DisplayHint::Hexadecimal { alternate: false, uppercase: true, zero_pad: 0 })] #[case(":#X", DisplayHint::Hexadecimal { alternate: true, uppercase: true, zero_pad: 0 })] #[case(":ms", DisplayHint::Seconds(TimePrecision::Millis))] From c14771396702cdc3110e011c6aaa67552cfe417c Mon Sep 17 00:00:00 2001 From: Johann Hemmann Date: Wed, 6 Mar 2024 12:31:56 +0100 Subject: [PATCH 2/3] Update CHANGELOG.md --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7cf5eb91..ef2c6110 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] +- [#811]: `book`: Add some examples for byte slice/array hints as well + +[#811]: https://github.com/knurling-rs/defmt/pull/811 + ## [v0.3.6] - 2024-02-05 - [#804]: `CI`: Remove mdbook strategy From 98a351c30c2db287839a5d279e75e953da14ead6 Mon Sep 17 00:00:00 2001 From: Johann Hemmann Date: Wed, 6 Mar 2024 12:33:48 +0100 Subject: [PATCH 3/3] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 37680828..335a6b28 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] -- [#813] doc: add note for the alloc feature flag +- [#813]: doc: add note for the alloc feature flag - [#811]: `book`: Add some examples for byte slice/array hints as well - [#800]: `defmt-macros`: Fix generic trait bounds in Format derive macro