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

defmt-decoder: impl FromStr for Encoding #572

Merged
merged 1 commit into from
Sep 8, 2021
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
8 changes: 2 additions & 6 deletions decoder/src/elf2table/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use std::{
path::{Path, PathBuf},
};

use crate::{BitflagsKey, Encoding, StringEntry, Table, TableEntry, Tag, DEFMT_VERSION};
use crate::{BitflagsKey, StringEntry, Table, TableEntry, Tag, DEFMT_VERSION};
use anyhow::{anyhow, bail, ensure};
use object::{Object, ObjectSection, ObjectSymbol};

Expand Down Expand Up @@ -99,11 +99,7 @@ pub fn parse_impl(elf: &[u8], check_version: bool) -> Result<Option<Table>, anyh
}

let encoding = match encoding {
Some(e) => match &e[..] {
"raw" => Encoding::Raw,
"rzcobs" => Encoding::Rzcobs,
_ => bail!("Unknown defmt encoding '{}' specified. This is a bug.", e),
},
Some(e) => e.parse()?,
None => bail!("No defmt encoding specified. This is a bug."),
};

Expand Down
13 changes: 13 additions & 0 deletions decoder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use std::{
collections::{BTreeMap, HashMap},
error::Error,
fmt, io,
str::FromStr,
};

use byteorder::{ReadBytesExt, LE};
Expand Down Expand Up @@ -121,6 +122,18 @@ enum Encoding {
Rzcobs,
}

impl FromStr for Encoding {
type Err = anyhow::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"raw" => Ok(Encoding::Raw),
"rzcobs" => Ok(Encoding::Rzcobs),
_ => anyhow::bail!("Unknown defmt encoding '{}' specified. This is a bug.", s),
}
}
}

/// Internal table that holds log levels and maps format strings to indices
#[derive(Debug, PartialEq)]
pub struct Table {
Expand Down