Skip to content

Commit

Permalink
refactor: ♻️ Use strum-macros to avoid reimplementing from_str
Browse files Browse the repository at this point in the history
  • Loading branch information
SergioGasquez committed Oct 10, 2022
1 parent 6a13fec commit 2da07d9
Showing 1 changed file with 12 additions and 16 deletions.
28 changes: 12 additions & 16 deletions src/targets.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
//! ESP32 chip variants support.

use crate::emoji;
use anyhow::Context;
use log::debug;
use std::{collections::HashSet, str::FromStr};
use strum::Display;
use strum_macros::EnumString;

#[derive(Clone, Copy, Hash, PartialEq, Eq, Debug, Display)]
#[derive(Clone, Copy, EnumString, PartialEq, Hash, Eq, Debug, Display)]
pub enum Target {
/// Xtensa LX7 based dual core
#[strum(serialize = "esp32")]
Expand All @@ -21,20 +23,6 @@ pub enum Target {
ESP32C3,
}

impl FromStr for Target {
type Err = String;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"esp32" => Ok(Target::ESP32),
"esp32s2" => Ok(Target::ESP32S2),
"esp32s3" => Ok(Target::ESP32S3),
"esp32c3" => Ok(Target::ESP32C3),
_ => Err(format!("{} Target '{}' is not supported", emoji::ERROR, s)),
}
}
}

/// Returns a vector of Chips from a comma or space separated string.
pub fn parse_targets(targets_str: &str) -> Result<HashSet<Target>, String> {
debug!("{} Parsing targets: {}", emoji::DEBUG, targets_str);
Expand All @@ -53,7 +41,15 @@ pub fn parse_targets(targets_str: &str) -> Result<HashSet<Target>, String> {
};

for target in targets_str {
targets.insert(FromStr::from_str(target).unwrap());
targets.insert(
Target::from_str(target)
.context(format!(
"{} Target '{}' is not supported",
emoji::ERROR,
target
))
.unwrap(),
);
}
debug!("{} Parsed targets: {:?}", emoji::DEBUG, targets);
Ok(targets)
Expand Down

0 comments on commit 2da07d9

Please sign in to comment.