`defmt_parser`: `impl FromStr for Type`
@@ -148,6 +148,40 @@ pub enum Type {
U8Array(usize), // FIXME: This `usize` is not the target's `usize`; use `u64` instead?
}
+impl FromStr for Type {
+ type Err = ();
+
+ fn from_str(s: &str) -> Result<Self, Self::Err> {
+ Ok(match s {
+ "u8" => Type::U8,
+ "u16" => Type::U16,
+ "u24" => Type::U24,
+ "u32" => Type::U32,
+ "u64" => Type::U64,
+ "u128" => Type::U128,
+ "usize" => Type::Usize,
+ "i8" => Type::I8,
+ "i16" => Type::I16,
+ "i32" => Type::I32,
+ "i64" => Type::I64,
+ "i128" => Type::I128,
+ "isize" => Type::Isize,
+ "f32" => Type::F32,
+ "f64" => Type::F64,
+ "bool" => Type::Bool,
+ "str" => Type::Str,
+ "istr" => Type::IStr,
+ "__internal_Debug" => Type::Debug,
+ "__internal_Display" => Type::Display,
+ "[u8]" => Type::U8Slice,
+ "?" => Type::Format,
+ "[?]" => Type::FormatSlice,
+ "char" => Type::Char,
+ _ => return Err(()),
+ })
+ }
+}
+
// when not specified in the format string, this type is assumed
impl Default for Type {
fn default() -> Self {
@@ -254,31 +288,8 @@ fn parse_param(mut input: &str, mode: ParserMode) -> Result<Param, Cow<'static,
const U8_ARRAY_START: &str = "[u8;";
// what comes next is the type
- ty = match type_fragment {
- "u8" => Type::U8,
- "u16" => Type::U16,
- "u24" => Type::U24,
- "u32" => Type::U32,
- "u64" => Type::U64,
- "u128" => Type::U128,
- "usize" => Type::Usize,
- "i8" => Type::I8,
- "i16" => Type::I16,
- "i32" => Type::I32,
- "i64" => Type::I64,
- "i128" => Type::I128,
- "isize" => Type::Isize,
- "f32" => Type::F32,
- "f64" => Type::F64,
- "bool" => Type::Bool,
- "str" => Type::Str,
- "istr" => Type::IStr,
- "__internal_Debug" => Type::Debug,
- "__internal_Display" => Type::Display,
- "[u8]" => Type::U8Slice,
- "?" => Type::Format,
- "[?]" => Type::FormatSlice,
- "char" => Type::Char,
+ ty = match type_fragment.parse() {
+ Ok(ty) => ty,
_ if input.starts_with(U8_ARRAY_START) => {
let len = parse_array(&type_fragment[U8_ARRAY_START.len()..])?;
Type::U8Array(len)
