-
-
Notifications
You must be signed in to change notification settings - Fork 104
decoder: add Frame::fragments() and Frame::display_fragments() #966
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
darkwater
wants to merge
1
commit into
knurling-rs:main
Choose a base branch
from
darkwater:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -91,6 +91,52 @@ impl<'t> Frame<'t> { | |
DisplayMessage { frame: self } | ||
} | ||
|
||
/// Returns an iterator over the fragments of the message contained in this log frame. | ||
/// | ||
/// Collecting this into a String will yield the same result as [`Self::display_message`], but | ||
/// this iterator will yield interpolated fragments on their own. For example, the log: | ||
/// | ||
/// ```ignore | ||
/// defmt::info!("foo = {}, bar = {}", 1, 2); | ||
/// ``` | ||
/// | ||
/// Will yield the following strings: | ||
/// | ||
/// ```ignore | ||
/// vec!["foo = ", "1", ", bar = ", "2"] | ||
/// ``` | ||
/// | ||
/// Note that nested fragments will not yield separately: | ||
/// | ||
/// ```ignore | ||
/// defmt::info!("foo = {}", Foo { bar: 1 }); | ||
/// ``` | ||
/// | ||
/// Will yield: | ||
/// | ||
/// ```ignore | ||
/// vec!["foo = ", "Foo { bar: 1 }"] | ||
/// ``` | ||
/// | ||
/// This iterator yields the same fragments as [`Self::fragments`], so you can zip them | ||
/// together to get both representations. | ||
pub fn display_fragments(&'t self) -> DisplayFragments<'t> { | ||
DisplayFragments { | ||
frame: self, | ||
iter: self.fragments().into_iter(), | ||
} | ||
} | ||
|
||
/// Returns the fragments of the message contained in this log frame. | ||
/// | ||
/// Each fragment represents a part of the log message. See [`Fragment`] for more details. | ||
/// | ||
/// This iterator yields the same fragments as [`Self::display_fragments`], so you can zip them | ||
/// together to get both representations. | ||
pub fn fragments(&'t self) -> Vec<Fragment<'t>> { | ||
defmt_parser::parse(self.format, ParserMode::ForwardsCompatible).unwrap() | ||
} | ||
|
||
pub fn level(&self) -> Option<Level> { | ||
self.level | ||
} | ||
|
@@ -100,119 +146,120 @@ impl<'t> Frame<'t> { | |
} | ||
|
||
fn format_args(&self, format: &str, args: &[Arg], parent_hint: Option<&DisplayHint>) -> String { | ||
self.format_args_real(format, args, parent_hint).unwrap() // cannot fail, we only write to a `String` | ||
let params = defmt_parser::parse(format, ParserMode::ForwardsCompatible).unwrap(); | ||
let mut buf = String::new(); | ||
for param in params { | ||
self.format_fragment(param, &mut buf, args, parent_hint) | ||
.unwrap(); // cannot fail, we only write to a `String` | ||
} | ||
buf | ||
} | ||
|
||
fn format_args_real( | ||
fn format_fragment( | ||
&self, | ||
format: &str, | ||
param: Fragment<'_>, | ||
buf: &mut String, | ||
args: &[Arg], | ||
parent_hint: Option<&DisplayHint>, | ||
) -> Result<String, fmt::Error> { | ||
let params = defmt_parser::parse(format, ParserMode::ForwardsCompatible).unwrap(); | ||
let mut buf = String::new(); | ||
for param in params { | ||
match param { | ||
Fragment::Literal(lit) => { | ||
buf.push_str(&lit); | ||
} | ||
Fragment::Parameter(param) => { | ||
let hint = param.hint.as_ref().or(parent_hint); | ||
|
||
match &args[param.index] { | ||
Arg::Bool(x) => write!(buf, "{x}")?, | ||
Arg::F32(x) => write!(buf, "{}", ryu::Buffer::new().format(*x))?, | ||
Arg::F64(x) => write!(buf, "{}", ryu::Buffer::new().format(*x))?, | ||
Arg::Uxx(x) => { | ||
match param.ty { | ||
Type::BitField(range) => { | ||
let left_zeroes = | ||
mem::size_of::<u128>() * 8 - range.end as usize; | ||
let right_zeroes = left_zeroes + range.start as usize; | ||
// isolate the desired bitfields | ||
let bitfields = (*x << left_zeroes) >> right_zeroes; | ||
|
||
if let Some(DisplayHint::Ascii) = hint { | ||
let bstr = bitfields | ||
.to_be_bytes() | ||
.iter() | ||
.skip(right_zeroes / 8) | ||
.copied() | ||
.collect::<Vec<u8>>(); | ||
self.format_bytes(&bstr, hint, &mut buf)? | ||
} else { | ||
self.format_u128(bitfields, hint, &mut buf)?; | ||
} | ||
) -> Result<(), fmt::Error> { | ||
match param { | ||
Fragment::Literal(lit) => { | ||
buf.push_str(&lit); | ||
} | ||
Fragment::Parameter(param) => { | ||
let hint = param.hint.as_ref().or(parent_hint); | ||
|
||
match &args[param.index] { | ||
Arg::Bool(x) => write!(buf, "{x}")?, | ||
Arg::F32(x) => write!(buf, "{}", ryu::Buffer::new().format(*x))?, | ||
Arg::F64(x) => write!(buf, "{}", ryu::Buffer::new().format(*x))?, | ||
Arg::Uxx(x) => { | ||
match param.ty { | ||
Type::BitField(range) => { | ||
let left_zeroes = mem::size_of::<u128>() * 8 - range.end as usize; | ||
let right_zeroes = left_zeroes + range.start as usize; | ||
// isolate the desired bitfields | ||
let bitfields = (*x << left_zeroes) >> right_zeroes; | ||
|
||
if let Some(DisplayHint::Ascii) = hint { | ||
let bstr = bitfields | ||
.to_be_bytes() | ||
.iter() | ||
.skip(right_zeroes / 8) | ||
.copied() | ||
.collect::<Vec<u8>>(); | ||
self.format_bytes(&bstr, hint, buf)? | ||
} else { | ||
self.format_u128(bitfields, hint, buf)?; | ||
} | ||
_ => match hint { | ||
Some(DisplayHint::ISO8601(precision)) => { | ||
self.format_iso8601(*x as u64, precision, &mut buf)? | ||
} | ||
Some(DisplayHint::Debug) => { | ||
self.format_u128(*x, parent_hint, &mut buf)? | ||
} | ||
_ => self.format_u128(*x, hint, &mut buf)?, | ||
}, | ||
} | ||
_ => match hint { | ||
Some(DisplayHint::ISO8601(precision)) => { | ||
self.format_iso8601(*x as u64, precision, buf)? | ||
} | ||
Some(DisplayHint::Debug) => { | ||
self.format_u128(*x, parent_hint, buf)? | ||
} | ||
_ => self.format_u128(*x, hint, buf)?, | ||
}, | ||
} | ||
Arg::Ixx(x) => self.format_i128(*x, param.ty, hint, &mut buf)?, | ||
Arg::Str(x) | Arg::Preformatted(x) => self.format_str(x, hint, &mut buf)?, | ||
Arg::IStr(x) => self.format_str(x, hint, &mut buf)?, | ||
Arg::Format { format, args } => match parent_hint { | ||
Some(DisplayHint::Ascii) => { | ||
buf.push_str(&self.format_args(format, args, parent_hint)); | ||
} | ||
_ => buf.push_str(&self.format_args(format, args, hint)), | ||
}, | ||
Arg::FormatSequence { args } => { | ||
for arg in args { | ||
buf.push_str(&self.format_args("{=?}", &[arg.clone()], hint)) | ||
} | ||
} | ||
Arg::Ixx(x) => self.format_i128(*x, param.ty, hint, buf)?, | ||
Arg::Str(x) | Arg::Preformatted(x) => self.format_str(x, hint, buf)?, | ||
Arg::IStr(x) => self.format_str(x, hint, buf)?, | ||
Arg::Format { format, args } => match parent_hint { | ||
Some(DisplayHint::Ascii) => { | ||
buf.push_str(&self.format_args(format, args, parent_hint)); | ||
} | ||
Arg::FormatSlice { elements } => { | ||
match hint { | ||
// Filter Ascii Hints, which contains u8 byte slices | ||
Some(DisplayHint::Ascii) | ||
if elements.iter().filter(|e| e.format == "{=u8}").count() | ||
!= 0 => | ||
{ | ||
let vals = elements | ||
.iter() | ||
.map(|e| match e.args.as_slice() { | ||
[Arg::Uxx(v)] => u8::try_from(*v) | ||
.expect("the value must be in u8 range"), | ||
_ => panic!( | ||
"FormatSlice should only contain one argument" | ||
), | ||
}) | ||
.collect::<Vec<u8>>(); | ||
self.format_bytes(&vals, hint, &mut buf)? | ||
} | ||
_ => { | ||
buf.write_str("[")?; | ||
let mut is_first = true; | ||
for element in elements { | ||
if !is_first { | ||
buf.write_str(", ")?; | ||
_ => buf.push_str(&self.format_args(format, args, hint)), | ||
}, | ||
Arg::FormatSequence { args } => { | ||
for arg in args { | ||
buf.push_str(&self.format_args("{=?}", &[arg.clone()], hint)) | ||
} | ||
} | ||
Arg::FormatSlice { elements } => { | ||
match hint { | ||
// Filter Ascii Hints, which contains u8 byte slices | ||
Some(DisplayHint::Ascii) | ||
if elements.iter().filter(|e| e.format == "{=u8}").count() != 0 => | ||
{ | ||
let vals = elements | ||
.iter() | ||
.map(|e| match e.args.as_slice() { | ||
[Arg::Uxx(v)] => { | ||
u8::try_from(*v).expect("the value must be in u8 range") | ||
} | ||
is_first = false; | ||
buf.write_str(&self.format_args( | ||
element.format, | ||
&element.args, | ||
hint, | ||
))?; | ||
_ => panic!("FormatSlice should only contain one argument"), | ||
}) | ||
.collect::<Vec<u8>>(); | ||
self.format_bytes(&vals, hint, buf)? | ||
} | ||
_ => { | ||
buf.write_str("[")?; | ||
let mut is_first = true; | ||
for element in elements { | ||
if !is_first { | ||
buf.write_str(", ")?; | ||
} | ||
buf.write_str("]")?; | ||
is_first = false; | ||
buf.write_str(&self.format_args( | ||
element.format, | ||
&element.args, | ||
hint, | ||
))?; | ||
} | ||
buf.write_str("]")?; | ||
} | ||
} | ||
Arg::Slice(x) => self.format_bytes(x, hint, &mut buf)?, | ||
Arg::Char(c) => write!(buf, "{c}")?, | ||
} | ||
Arg::Slice(x) => self.format_bytes(x, hint, buf)?, | ||
Arg::Char(c) => write!(buf, "{c}")?, | ||
} | ||
} | ||
} | ||
Ok(buf) | ||
|
||
Ok(()) | ||
} | ||
|
||
fn format_u128( | ||
|
@@ -531,6 +578,26 @@ impl fmt::Display for DisplayMessage<'_> { | |
} | ||
} | ||
|
||
/// An iterator over the fragments of a log message, formatted as strings. | ||
/// | ||
/// See [`Frame::display_fragments`]. | ||
pub struct DisplayFragments<'t> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a new public type, so it needs documenting. (Also I should There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done (just a summary and a link to |
||
frame: &'t Frame<'t>, | ||
iter: std::vec::IntoIter<Fragment<'t>>, | ||
} | ||
|
||
impl Iterator for DisplayFragments<'_> { | ||
type Item = String; | ||
|
||
fn next(&mut self) -> Option<Self::Item> { | ||
let mut buf = String::new(); | ||
self.frame | ||
.format_fragment(self.iter.next()?, &mut buf, &self.frame.args, None) | ||
.ok()?; | ||
Some(buf) | ||
} | ||
} | ||
|
||
/// Prints a `Frame` when formatted via `fmt::Display`, including all included metadata (level, | ||
/// timestamp, ...). | ||
pub struct DisplayFrame<'t> { | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.