Skip to content
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

implement enum decoder #98

Merged
merged 1 commit into from
Aug 11, 2020
Merged
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
66 changes: 61 additions & 5 deletions decoder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,12 +355,29 @@ fn parse_args<'t>(
}
Type::Format => {
let format = get_format(format_list, bytes, table)?;
let inner_args = parse_args(bytes, format, table, format_list)?;

args.push(Arg::Format {
format,
args: inner_args,
});
if format.contains('|') {
// enum
let enum_string = format;
let discriminant = bytes.read_u8().map_err(drop)?;
// NOTE nesting of enums, like "A|B(C|D)" is not possible; indirection is
// required: "A|B({:?})" where "{:?}" -> "C|D"
let variant = enum_string
.split('|')
.nth(usize::from(discriminant))
.ok_or(())?;
let inner_args = parse_args(bytes, variant, table, format_list)?;
args.push(Arg::Format {
format: variant,
args: inner_args,
});
} else {
let inner_args = parse_args(bytes, format, table, format_list)?;
args.push(Arg::Format {
format,
args: inner_args,
});
}
}
Type::I16 => {
let data = bytes.read_i16::<LE>().map_err(drop)?;
Expand Down Expand Up @@ -937,4 +954,43 @@ mod tests {
"0.000002 INFO Hello World 125",
);
}

#[test]
fn option() {
let mut entries = BTreeMap::new();
entries.insert(4, "x={:?}".to_owned());
entries.insert(3, "None|Some({:?})".to_owned());
entries.insert(2, "{:u8}".to_owned());

let table = Table {
entries,
debug: 0..0,
error: 0..0,
info: 4..5,
trace: 0..0,
warn: 0..0,
};

let bytes = [
4, // string index (INFO)
0, // timestamp
3, // string index (enum)
1, // Some discriminant
2, // string index (u8)
42, // Some.0
];

let frame = super::decode(&bytes, &table).unwrap().0;
assert_eq!(frame.display(false).to_string(), "0.000000 INFO x=Some(42)");

let bytes = [
4, // string index (INFO)
1, // timestamp
3, // string index (enum)
0, // None discriminant
];

let frame = super::decode(&bytes, &table).unwrap().0;
assert_eq!(frame.display(false).to_string(), "0.000001 INFO x=None");
}
}
17 changes: 17 additions & 0 deletions firmware/qemu/src/bin/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,23 @@ fn main() -> ! {
let slices: &[&[u16]] = &[&[256, 257, 258], &[259, 260]];
binfmt::info!("{:[?]}", slices);

#[derive(Format)]
enum E {
A,
B,
}

binfmt::info!("e1={:?}", E::A);
binfmt::info!("e2={:?}", E::B);

binfmt::info!("e3={:?}", Some(42u8));
binfmt::info!("e4={:?}", None::<u8>);

binfmt::info!("e5={:?}", Ok::<u8, u16>(42u8));
binfmt::info!("e6={:?}", Err::<u8, u16>(256u16));

binfmt::info!("e7={:?}", Some(X { y: Y { z: 42 } }));

loop {
debug::exit(debug::EXIT_SUCCESS)
}
Expand Down
2 changes: 1 addition & 1 deletion src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ where
{
fn format(&self, f: &mut Formatter) {
if f.needs_tag() {
let t = internp!("None|Some({})");
let t = internp!("None|Some({:?})");
f.u8(&t);
}
match self {
Expand Down