cargo_wizard/
template.rs

1use indexmap::IndexMap;
2
3use crate::toml::TomlValue;
4use crate::workspace::manifest::BuiltinProfile;
5
6/// A set of Cargo profile items and .cargo/config.toml config items that can be applied to a
7/// Cargo workspace.
8#[derive(Debug)]
9pub struct Template {
10    inherits: BuiltinProfile,
11    items: IndexMap<TemplateItemId, TomlValue>,
12}
13
14impl Template {
15    pub fn inherits(&self) -> BuiltinProfile {
16        self.inherits
17    }
18
19    pub fn iter_items(&self) -> impl Iterator<Item = (TemplateItemId, &TomlValue)> {
20        self.items.iter().map(|(id, value)| (*id, value))
21    }
22
23    pub fn get_item(&self, id: TemplateItemId) -> Option<&TomlValue> {
24        self.items.get(&id)
25    }
26
27    pub fn insert_item(&mut self, id: TemplateItemId, value: TomlValue) {
28        self.items.insert(id, value);
29    }
30
31    pub fn remove_item(&mut self, id: TemplateItemId) {
32        self.items.shift_remove(&id);
33    }
34}
35
36#[doc(hidden)]
37pub struct TemplateBuilder {
38    inherits: BuiltinProfile,
39    profile: IndexMap<TemplateItemId, TomlValue>,
40}
41
42impl TemplateBuilder {
43    pub fn new(inherits: BuiltinProfile) -> Self {
44        Self {
45            inherits,
46            profile: Default::default(),
47        }
48    }
49
50    pub fn item(mut self, id: TemplateItemId, value: TomlValue) -> Self {
51        self.profile.insert(id, value);
52        self
53    }
54
55    pub fn build(self) -> Template {
56        let TemplateBuilder { inherits, profile } = self;
57        Template {
58            inherits,
59            items: profile,
60        }
61    }
62}
63
64/// Default properties of the dev profile.
65pub fn dev_profile() -> TemplateBuilder {
66    TemplateBuilder::new(BuiltinProfile::Dev)
67        .item(TemplateItemId::OptimizationLevel, TomlValue::Int(0))
68        .item(TemplateItemId::DebugInfo, TomlValue::Bool(true))
69        .item(TemplateItemId::Strip, TomlValue::String("none".to_string()))
70        .item(TemplateItemId::Lto, TomlValue::Bool(false))
71        .item(TemplateItemId::CodegenUnits, TomlValue::Int(256))
72        .item(TemplateItemId::Incremental, TomlValue::Bool(true))
73}
74
75/// Default properties of the release profile.
76pub fn release_profile() -> TemplateBuilder {
77    TemplateBuilder::new(BuiltinProfile::Release)
78        .item(TemplateItemId::OptimizationLevel, TomlValue::Int(3))
79        .item(TemplateItemId::DebugInfo, TomlValue::Bool(false))
80        .item(TemplateItemId::Strip, TomlValue::String("none".to_string()))
81        .item(TemplateItemId::Lto, TomlValue::Bool(false))
82        .item(TemplateItemId::CodegenUnits, TomlValue::Int(16))
83        .item(TemplateItemId::Incremental, TomlValue::Bool(false))
84}
85
86/// Identifier of a specific item of a template.
87#[derive(Debug, Hash, Eq, PartialEq, Clone, Copy)]
88pub enum TemplateItemId {
89    // Do not forget to modify CargoKnownOptions when adding new variants to this enum
90    DebugInfo,
91    SplitDebugInfo,
92    Strip,
93    Lto,
94    CodegenUnits,
95    Panic,
96    OptimizationLevel,
97    Incremental,
98    CodegenBackend,
99    FrontendThreads,
100    TargetCpuInstructionSet,
101    Linker,
102}
103
104/// Describes options for applying templates
105#[derive(Debug, Default)]
106pub struct WizardOptions {
107    /// Include template items that require a nightly compiler.
108    nightly_items: bool,
109}
110
111impl WizardOptions {
112    pub fn nightly_items_enabled(&self) -> bool {
113        self.nightly_items
114    }
115
116    pub fn with_nightly_items(mut self) -> Self {
117        self.nightly_items = true;
118        self
119    }
120}