Skip to main content

cargo_toml_builder/types/
profile.rs

1use std::fmt;
2use std::convert::TryFrom;
3
4use crate::error::Error;
5
6/// Represents a `[profile.*]` table
7#[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    /// Constructs a new, empty profile
20    pub fn new() -> Profile {
21        Profile::default()
22    }
23
24    /// Sets the optimization level for this profile
25    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    /// Sets the debug flag for this profile
34    pub fn debug(&mut self, debug: bool) -> &mut Self {
35        self.debug = Some(debug);
36        self
37    }
38
39    /// Sets the rpath flag for this profile
40    pub fn rpath(&mut self, rpath: bool) -> &mut Self {
41        self.rpath = Some(rpath);
42        self
43    }
44
45    /// Sets the lto flag for this profile
46    pub fn lto(&mut self, lto: bool) -> &mut Self {
47        self.lto = Some(lto);
48        self
49    }
50
51    /// Sets the debug-assertions flag for this profile
52    pub fn debug_assertions(&mut self, debug_assertions: bool) -> &mut Self {
53        self.debug_assertions = Some(debug_assertions);
54        self
55    }
56
57    /// Sets the number of codegen units for this profile
58    pub fn codegen_units(&mut self, codegen_units: u64) -> &mut Self {
59        self.codegen_units = Some(codegen_units);
60        self
61    }
62
63    /// Sets the panic strategy for this profile
64    pub fn panic(&mut self, panic: PanicStrategy) -> &mut Self {
65        self.panic = Some(panic);
66        self
67    }
68
69    /// Takes ownership of this builder
70    pub fn build(&self) -> Profile {
71        self.clone()
72    }
73}
74
75/// Represents the possible values for the `panic` setting
76#[derive(Debug, Clone, Copy, PartialEq)]
77pub enum PanicStrategy {
78    /// `panic = "unwind"`
79    Unwind,
80    /// `panic = "abort"`
81    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