Skip to content

Commit

Permalink
defmt-decoder: impl TryFrom<Option<String>> for Encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
Urhengulas committed Sep 8, 2021
1 parent 046b16d commit 8299476
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
9 changes: 1 addition & 8 deletions decoder/src/elf2table/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,7 @@ pub fn parse_impl(elf: &[u8], check_version: bool) -> Result<Option<Table>, anyh
self::check_version(&version).map_err(anyhow::Error::msg)?;
}

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

// second pass to demangle symbols
let mut map = BTreeMap::new();
Expand Down
16 changes: 16 additions & 0 deletions decoder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ mod stream;

use std::{
collections::{BTreeMap, HashMap},
convert::TryFrom,
error::Error,
fmt, io,
};
Expand Down Expand Up @@ -121,6 +122,21 @@ enum Encoding {
Rzcobs,
}

impl TryFrom<Option<String>> for Encoding {
type Error = anyhow::Error;

fn try_from(value: Option<String>) -> Result<Self, Self::Error> {
match value {
Some(e) => match &*e {
"raw" => Ok(Encoding::Raw),
"rzcobs" => Ok(Encoding::Rzcobs),
_ => anyhow::bail!("Unknown defmt encoding '{}' specified. This is a bug.", e),
},
None => anyhow::bail!("No defmt encoding specified. This is a bug."),
}
}
}

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

0 comments on commit 8299476

Please sign in to comment.