1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
use std::fmt;
use try_from::TryFrom;
use crate::error::Error;
#[derive(Default, Debug, Clone, Copy, PartialEq)]
pub struct Profile {
pub(crate) opt_level: Option<u8>,
pub(crate) debug: Option<bool>,
pub(crate) rpath: Option<bool>,
pub(crate) lto: Option<bool>,
pub(crate) debug_assertions: Option<bool>,
pub(crate) codegen_units: Option<u64>,
pub(crate) panic: Option<PanicStrategy>,
}
impl Profile {
pub fn new() -> Profile {
Profile::default()
}
pub fn opt_level(&mut self, opt_level: u8) -> Result<&mut Self, Error> {
if opt_level > 3 {
return Err("invalid opt level".into());
}
self.opt_level = Some(opt_level);
Ok(self)
}
pub fn debug(&mut self, debug: bool) -> &mut Self {
self.debug = Some(debug);
self
}
pub fn rpath(&mut self, rpath: bool) -> &mut Self {
self.rpath = Some(rpath);
self
}
pub fn lto(&mut self, lto: bool) -> &mut Self {
self.lto = Some(lto);
self
}
pub fn debug_assertions(&mut self, debug_assertions: bool) -> &mut Self {
self.debug_assertions = Some(debug_assertions);
self
}
pub fn codegen_units(&mut self, codegen_units: u64) -> &mut Self {
self.codegen_units = Some(codegen_units);
self
}
pub fn panic(&mut self, panic: PanicStrategy) -> &mut Self {
self.panic = Some(panic);
self
}
pub fn build(&self) -> Profile {
self.clone()
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum PanicStrategy {
Unwind,
Abort,
}
impl<'a> TryFrom<&'a str> for PanicStrategy {
type Err = Error;
fn try_from(s: &'a str) -> Result<PanicStrategy, Error> {
Ok(match s.to_lowercase().as_str() {
"unwind" => PanicStrategy::Unwind,
"abort" => PanicStrategy::Abort,
s => return Err(format!("unknown panic strategy {}", s).into()),
})
}
}
impl TryFrom<String> for PanicStrategy {
type Err = Error;
fn try_from(s: String) -> Result<PanicStrategy, Error> {
TryFrom::try_from(s.as_str())
}
}
impl fmt::Display for PanicStrategy {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", match *self {
PanicStrategy::Unwind => "unwind",
PanicStrategy::Abort => "abort",
})
}
}