use crate::{
RawAnswers, Template,
jinja::Typed,
template::{Question, QuestionKind},
};
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq, Eq)]
pub struct Selection {
pub with: Vec<String>,
pub without: Vec<String>,
pub base: Option<Vec<String>>,
}
impl Selection {
pub fn is_present(&self) -> bool {
!self.with.is_empty() || !self.without.is_empty()
}
pub fn admit(&self, template: &Template, supplied: &RawAnswers) -> Result<(), String> {
if !self.is_present() {
return Ok(());
}
let question = template
.part_question()
.ok_or("--with and --without need a template with a parts question")?;
if supplied.contains_key(&question.id) {
return Err(
"an answers document cannot answer the parts question with --with or --without"
.into(),
);
}
let options = options(question);
for part in self.with.iter().chain(&self.without) {
if !options.contains(part) {
return Err(format!("unknown part {part:?}"));
}
if self.with.contains(part) && self.without.contains(part) {
return Err(format!(
"part {part:?} is named by both --with and --without"
));
}
}
Ok(())
}
pub(crate) fn is_default(&self) -> bool {
!self.is_present() && self.base.is_none()
}
pub(crate) fn settle(&self, mut selected: Vec<String>) -> Vec<String> {
for part in &self.with {
if !selected.contains(part) {
selected.push(part.clone());
}
}
selected.retain(|part| !self.without.contains(part));
selected
}
}
pub(crate) fn options(question: &Question) -> &[String] {
match &question.kind {
QuestionKind::MultiSelect {
options: Typed::Literal(options),
..
} => options,
_ => &[],
}
}