use indexmap::IndexMap;
use crate::toml::TomlValue;
use crate::workspace::manifest::BuiltinProfile;
#[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 {
self.profile.insert(id, value);
self
}
pub fn build(self) -> Template {
let TemplateBuilder { inherits, profile } = self;
Template {
inherits,
items: profile,
}
}
}
pub fn dev_profile() -> TemplateBuilder {
TemplateBuilder::new(BuiltinProfile::Dev)
.item(TemplateItemId::OptimizationLevel, TomlValue::Int(0))
.item(TemplateItemId::DebugInfo, TomlValue::Bool(true))
.item(TemplateItemId::Strip, TomlValue::String("none".to_string()))
.item(TemplateItemId::Lto, TomlValue::Bool(false))
.item(TemplateItemId::CodegenUnits, TomlValue::Int(256))
.item(TemplateItemId::Incremental, TomlValue::Bool(true))
}
pub fn release_profile() -> TemplateBuilder {
TemplateBuilder::new(BuiltinProfile::Release)
.item(TemplateItemId::OptimizationLevel, TomlValue::Int(3))
.item(TemplateItemId::DebugInfo, TomlValue::Bool(false))
.item(TemplateItemId::Strip, TomlValue::String("none".to_string()))
.item(TemplateItemId::Lto, TomlValue::Bool(false))
.item(TemplateItemId::CodegenUnits, TomlValue::Int(16))
.item(TemplateItemId::Incremental, TomlValue::Bool(false))
}
#[derive(Debug, Hash, Eq, PartialEq, Clone, Copy)]
pub enum TemplateItemId {
DebugInfo,
SplitDebugInfo,
Strip,
Lto,
CodegenUnits,
Panic,
OptimizationLevel,
Incremental,
CodegenBackend,
FrontendThreads,
TargetCpuInstructionSet,
Linker,
}
#[derive(Debug, Default)]
pub struct KitOptions {
nightly_items: bool,
}
impl KitOptions {
pub fn nightly_items_enabled(&self) -> bool {
self.nightly_items
}
pub fn with_nightly_items(mut self) -> Self {
self.nightly_items = true;
self
}
}