calyx_backend/
backend_opt.rs

1use itertools::Itertools;
2use std::str::FromStr;
3
4/// Enumeration of valid backends
5#[derive(Default, Debug, Clone, PartialEq, Eq)]
6pub enum BackendOpt {
7    #[default]
8    Calyx,
9    Verilog,
10    Xilinx,
11    XilinxXml,
12    Mlir,
13    Resources,
14    Sexp,
15    Yxi,
16    Firrtl,
17    PrimitiveUses,
18    None,
19}
20
21/// Return a vector that maps strings to Backends.
22#[inline(always)]
23fn backends() -> Vec<(&'static str, BackendOpt)> {
24    vec![
25        ("verilog", BackendOpt::Verilog),
26        ("xilinx", BackendOpt::Xilinx),
27        ("xilinx-xml", BackendOpt::XilinxXml),
28        ("calyx", BackendOpt::Calyx),
29        ("mlir", BackendOpt::Mlir),
30        ("resources", BackendOpt::Resources),
31        ("sexp", BackendOpt::Sexp),
32        ("yxi", BackendOpt::Yxi),
33        ("firrtl", BackendOpt::Firrtl),
34        ("primitive-uses", BackendOpt::PrimitiveUses),
35        ("none", BackendOpt::None),
36    ]
37}
38
39/// Command line parsing for the Backend enum
40impl FromStr for BackendOpt {
41    type Err = String;
42    fn from_str(input: &str) -> Result<Self, Self::Err> {
43        // allocate a vector for the list of backends
44        let backends = backends();
45        // see if there is a backend for the string that we receive
46        let found_backend = backends
47            .iter()
48            .find(|(backend_name, _)| &input == backend_name);
49        if let Some((_, opt)) = found_backend {
50            // return the BackendOpt if we found one
51            Ok(opt.clone())
52        } else {
53            // build list of backends for error message
54            let backend_str = backends
55                .iter()
56                .map(|(name, _)| (*name).to_string())
57                .join(", ");
58            Err(format!(
59                "`{}` is not a valid backend.\nValid backends: {}",
60                input, backend_str
61            ))
62        }
63    }
64}
65
66/// Convert `BackendOpt` to a string
67impl ToString for BackendOpt {
68    fn to_string(&self) -> String {
69        match self {
70            Self::Mlir => "mlir",
71            Self::Resources => "resources",
72            Self::Sexp => "sexp",
73            Self::Verilog => "verilog",
74            Self::Xilinx => "xilinx",
75            Self::XilinxXml => "xilinx-xml",
76            Self::Yxi => "yxi",
77            Self::Calyx => "calyx",
78            Self::Firrtl => "firrtl",
79            Self::PrimitiveUses => "primitive-uses",
80            Self::None => "none",
81        }
82        .to_string()
83    }
84}