Use strum to automate keeping targets list in sync

This commit is contained in:
Richard Feldman 2022-11-09 10:47:09 -05:00
parent e589dc88a3
commit 5a2dde73e0
No known key found for this signature in database
GPG key ID: F1F21AA5B1D9E43B
3 changed files with 21 additions and 28 deletions

View file

@ -18,6 +18,7 @@ use std::mem::ManuallyDrop;
use std::os::raw::{c_char, c_int};
use std::path::{Path, PathBuf};
use std::process;
use strum::{EnumIter, IntoEnumIterator, IntoStaticStr};
use target_lexicon::BinaryFormat;
use target_lexicon::{
Architecture, Environment, OperatingSystem, Triple, Vendor, X86_32Architecture,
@ -151,8 +152,10 @@ pub fn build_app<'a>() -> Command<'a> {
Arg::new(FLAG_TARGET)
.long(FLAG_TARGET)
.help("Choose a different target")
.default_value(Target::default().as_str())
.possible_values(Target::OPTIONS)
.default_value(Target::default().into())
.possible_values(Target::iter().map(|target| {
Into::<&'static str>::into(target)
}))
.required(false),
)
.arg(
@ -289,8 +292,10 @@ pub fn build_app<'a>() -> Command<'a> {
Arg::new(FLAG_TARGET)
.long(FLAG_TARGET)
.help("Choose a different target")
.default_value(Target::default().as_str())
.possible_values(Target::OPTIONS)
.default_value(Target::default().into())
.possible_values(Target::iter().map(|target| {
Into::<&'static str>::into(target)
}))
.required(false),
)
)
@ -1149,12 +1154,17 @@ fn run_with_wasmer<I: Iterator<Item = S>, S: AsRef<[u8]>>(_wasm_path: &std::path
println!("Running wasm files is not supported on this target.");
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[derive(Debug, Copy, Clone, EnumIter, IntoStaticStr, PartialEq, Eq)]
pub enum Target {
#[strum(serialize = "system")]
System,
#[strum(serialize = "linux32")]
Linux32,
#[strum(serialize = "linux64")]
Linux64,
#[strum(serialize = "windows64")]
Windows64,
#[strum(serialize = "wasm32")]
Wasm32,
}
@ -1165,27 +1175,6 @@ impl Default for Target {
}
impl Target {
const fn as_str(&self) -> &'static str {
use Target::*;
match self {
System => "system",
Linux32 => "linux32",
Linux64 => "linux64",
Windows64 => "windows64",
Wasm32 => "wasm32",
}
}
/// NOTE keep up to date!
const OPTIONS: &'static [&'static str] = &[
Target::System.as_str(),
Target::Linux32.as_str(),
Target::Linux64.as_str(),
Target::Windows64.as_str(),
Target::Wasm32.as_str(),
];
pub fn to_triple(self) -> Triple {
use Target::*;
@ -1231,7 +1220,7 @@ impl From<&Target> for Triple {
impl std::fmt::Display for Target {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{}", self.as_str())
write!(f, "{}", Into::<&'static str>::into(self))
}
}