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