]> git.lizzy.rs Git - rust.git/blob - src/config.rs
Move BackendConfig to config.rs
[rust.git] / src / config.rs
1 use std::str::FromStr;
2
3 #[derive(Copy, Clone, Debug)]
4 pub enum CodegenMode {
5     Aot,
6     Jit,
7     JitLazy,
8 }
9
10 impl Default for CodegenMode {
11     fn default() -> Self {
12         CodegenMode::Aot
13     }
14 }
15
16 impl FromStr for CodegenMode {
17     type Err = String;
18
19     fn from_str(s: &str) -> Result<Self, Self::Err> {
20         match s {
21             "aot" => Ok(CodegenMode::Aot),
22             "jit" => Ok(CodegenMode::Jit),
23             "jit-lazy" => Ok(CodegenMode::JitLazy),
24             _ => Err(format!("Unknown codegen mode `{}`", s)),
25         }
26     }
27 }
28
29 #[derive(Copy, Clone, Debug, Default)]
30 pub struct BackendConfig {
31     pub codegen_mode: CodegenMode,
32 }
33
34 impl BackendConfig {
35     pub fn from_opts(opts: &[String]) -> Result<Self, String> {
36         let mut config = BackendConfig::default();
37         for opt in opts {
38             if let Some((name, value)) = opt.split_once('=') {
39                 match name {
40                     "mode" => config.codegen_mode = value.parse()?,
41                     _ => return Err(format!("Unknown option `{}`", name)),
42                 }
43             } else {
44                 return Err(format!("Invalid option `{}`", opt));
45             }
46         }
47         Ok(config)
48     }
49 }