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
use indexmap::IndexMap;

use crate::toml::TomlValue;
use crate::workspace::manifest::BuiltinProfile;

/// A set of Cargo profile items and .cargo/config.toml config items that can be applied to a
/// Cargo workspace.
#[derive(Debug)]
pub struct Template {
    inherits: BuiltinProfile,
    items: IndexMap<TemplateItemId, TomlValue>,
}

impl Template {
    pub fn inherits(&self) -> BuiltinProfile {
        self.inherits
    }

    pub fn iter_items(&self) -> impl Iterator<Item = (TemplateItemId, &TomlValue)> {
        self.items.iter().map(|(id, value)| (*id, value))
    }

    pub fn get_item(&self, id: TemplateItemId) -> Option<&TomlValue> {
        self.items.get(&id)
    }

    pub fn insert_item(&mut self, id: TemplateItemId, value: TomlValue) {
        self.items.insert(id, value);
    }

    pub fn remove_item(&mut self, id: TemplateItemId) {
        self.items.shift_remove(&id);
    }
}

#[doc(hidden)]
pub struct TemplateBuilder {
    inherits: BuiltinProfile,
    profile: IndexMap<TemplateItemId, TomlValue>,
}

impl TemplateBuilder {
    pub fn new(inherits: BuiltinProfile) -> Self {
        Self {
            inherits,
            profile: Default::default(),
        }
    }

    pub fn item(mut self, id: TemplateItemId, value: TomlValue) -> Self {
        assert!(self.profile.insert(id, value).is_none());
        self
    }

    pub fn build(self) -> Template {
        let TemplateBuilder { inherits, profile } = self;
        Template {
            inherits,
            items: profile,
        }
    }
}

/// Identifier of a specific item of a template.
#[derive(Debug, Hash, Eq, PartialEq, Clone, Copy)]
pub enum TemplateItemId {
    // Do not forget to modify CargoKnownOptions when adding new variants to this enum
    DebugInfo,
    Strip,
    Lto,
    CodegenUnits,
    Panic,
    OptimizationLevel,
    CodegenBackend,
    FrontendThreads,
    TargetCpuInstructionSet,
    Linker,
}

/// Describes options for applying templates
#[derive(Debug, Default)]
pub struct WizardOptions {
    /// Include template items that require a nightly compiler.
    nightly_items: bool,
}

impl WizardOptions {
    pub fn nightly_items_enabled(&self) -> bool {
        self.nightly_items
    }

    pub fn with_nightly_items(mut self) -> Self {
        self.nightly_items = true;
        self
    }
}