#[cfg(doc)]
use super::Questionnaire;
use gpui::SharedString;
#[derive(Clone)]
pub(super) enum QuestionKind {
Single(Vec<SharedString>),
Multiple(Vec<SharedString>),
Freeform,
}
#[derive(Clone)]
pub struct Question {
pub(super) id: SharedString,
pub(super) title: SharedString,
pub(super) description: Option<SharedString>,
pub(super) kind: QuestionKind,
pub(super) skippable: bool,
pub(super) placeholder: SharedString,
}
impl Question {
pub(super) fn new(
id: impl Into<SharedString>,
title: impl Into<SharedString>,
kind: QuestionKind,
) -> Self {
Self {
id: id.into(),
title: title.into(),
description: None,
kind,
skippable: false,
placeholder: "Type your answer…".into(),
}
}
pub fn single(
id: impl Into<SharedString>,
title: impl Into<SharedString>,
options: impl IntoIterator<Item = impl Into<SharedString>>,
) -> Self {
Self::new(
id,
title,
QuestionKind::Single(options.into_iter().map(Into::into).collect()),
)
}
pub fn multiple(
id: impl Into<SharedString>,
title: impl Into<SharedString>,
options: impl IntoIterator<Item = impl Into<SharedString>>,
) -> Self {
Self::new(
id,
title,
QuestionKind::Multiple(options.into_iter().map(Into::into).collect()),
)
}
pub fn freeform(id: impl Into<SharedString>, title: impl Into<SharedString>) -> Self {
Self::new(id, title, QuestionKind::Freeform)
}
#[must_use]
pub fn description(mut self, description: impl Into<SharedString>) -> Self {
self.description = Some(description.into());
self
}
#[must_use]
pub fn skippable(mut self) -> Self {
self.skippable = true;
self
}
#[must_use]
pub fn placeholder(mut self, placeholder: impl Into<SharedString>) -> Self {
self.placeholder = placeholder.into();
self
}
}
#[derive(Clone, Debug, PartialEq)]
pub enum Answer {
Choice(SharedString),
Choices(Vec<SharedString>),
Text(SharedString),
Skipped,
}
impl Answer {
#[must_use]
pub fn summary(&self) -> SharedString {
match self {
Answer::Choice(choice) => choice.clone(),
Answer::Choices(choices) => choices
.iter()
.map(std::convert::AsRef::as_ref)
.collect::<Vec<_>>()
.join(", ")
.into(),
Answer::Text(text) => text.clone(),
Answer::Skipped => "Skipped".into(),
}
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct QuestionnaireAnswer {
pub question_id: SharedString,
pub answer: Answer,
}
#[cfg(test)]
mod tests {
use super::Answer;
#[test]
fn answers_summarize() {
assert_eq!(
Answer::Choice("Engineer".into()).summary().as_ref(),
"Engineer"
);
assert_eq!(
Answer::Choices(vec!["Figma".into(), "Zed".into()])
.summary()
.as_ref(),
"Figma, Zed"
);
assert_eq!(Answer::Skipped.summary().as_ref(), "Skipped");
}
}