Skip to content

Commit

Permalink
book: Dokument :cbor hint
Browse files Browse the repository at this point in the history
  • Loading branch information
chrysn committed Jan 13, 2025
1 parent 5ebfdf9 commit b403c6d
Showing 1 changed file with 30 additions and 13 deletions.
43 changes: 30 additions & 13 deletions book/src/hints.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,20 @@ The hint follows the syntax `:Display` and must come after the type within the b

The following display hints are currently supported:

| hint | name |
| :----- | :------------------------------------------------------- |
| `:x` | lowercase hexadecimal |
| `:X` | uppercase hexadecimal |
| `:?` | `core::fmt::Debug`-like |
| `:b` | binary |
| `:o` | octal |
| `:a` | ASCII |
| `:ms` | timestamp in seconds (input in milliseconds) |
| `:us` | timestamp in seconds (input in microseconds) |
| `:ts` | timestamp in human-readable time (input in seconds) |
| `:tms` | timestamp in human-readable time (input in milliseconds) |
| `:tus` | timestamp in human-readable time (input in microseconds) |
| hint | name |
| :------ | :------------------------------------------------------- |
| `:x` | lowercase hexadecimal |
| `:X` | uppercase hexadecimal |
| `:?` | `core::fmt::Debug`-like |
| `:b` | binary |
| `:o` | octal |
| `:a` | ASCII |
| `:ms` | timestamp in seconds (input in milliseconds) |
| `:us` | timestamp in seconds (input in microseconds) |
| `:ts` | timestamp in human-readable time (input in seconds) |
| `:tms` | timestamp in human-readable time (input in milliseconds) |
| `:tus` | timestamp in human-readable time (input in microseconds) |
| `:cbor` | CBOR encoded items rendered in Diagnostic Notation (EDN) |

The first 4 display hints resemble what's supported in `core::fmt`, for example:

Expand All @@ -44,6 +45,22 @@ let bytes = [104, 101, 255, 108, 108, 111];
defmt::info!("{=[u8]:a}", bytes); // -> INFO b"he\xffllo"
```

The CBOR display hint is useful when CBOR data is in memory, especially before further processing:

``` rust
# extern crate defmt;
# fn parse(slice: &[u8]) -> Result<(), ()> {
# Ok(())
# }
# fn main() -> Result<(), ()> {
let id_cred = &[0xa1, 0x04, 0x44, 0x6b, 0x69, 0x64, 0x31];

defmt::info!("Peer ID: {=[u8]:cbor}", id_cred); // -> INFO Peer ID: {4: 'kid1'}
let parsed = parse(id_cred)?;
# Ok(())
# }
```

## Alternate printing

Adding `#` in front of a binary, octal, and hexadecimal display hints, precedes these numbers with a base indicator.
Expand Down

0 comments on commit b403c6d

Please sign in to comment.