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;

/// Represents a `[profile.*]` table
#[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 {
    /// Constructs a new, empty profile
    pub fn new() -> Profile {
        Profile::default()
    }

    /// Sets the optimization level for this profile
    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)
    }

    /// Sets the debug flag for this profile
    pub fn debug(&mut self, debug: bool) -> &mut Self {
        self.debug = Some(debug);
        self
    }

    /// Sets the rpath flag for this profile
    pub fn rpath(&mut self, rpath: bool) -> &mut Self {
        self.rpath = Some(rpath);
        self
    }

    /// Sets the lto flag for this profile
    pub fn lto(&mut self, lto: bool) -> &mut Self {
        self.lto = Some(lto);
        self
    }

    /// Sets the debug-assertions flag for this profile
    pub fn debug_assertions(&mut self, debug_assertions: bool) -> &mut Self {
        self.debug_assertions = Some(debug_assertions);
        self
    }

    /// Sets the number of codegen units for this profile
    pub fn codegen_units(&mut self, codegen_units: u64) -> &mut Self {
        self.codegen_units = Some(codegen_units);
        self
    }

    /// Sets the panic strategy for this profile
    pub fn panic(&mut self, panic: PanicStrategy) -> &mut Self {
        self.panic = Some(panic);
        self
    }

    /// Takes ownership of this builder
    pub fn build(&self) -> Profile {
        self.clone()
    }
}

/// Represents the possible values for the `panic` setting
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum PanicStrategy {
    /// `panic = "unwind"`
    Unwind,
    /// `panic = "abort"`
    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",
        })
    }
}