use serde::{Deserialize, Serialize};
use crate::edit::{Confirmed, Session};
use crate::module::{ActionKind, Needs};
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct ChoiceView {
pub id: String,
pub reason: String,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct FieldView {
pub key: String,
pub ty: &'static str,
pub description: String,
pub prompt: String,
pub example: Option<String>,
pub hint: Option<String>,
pub section: String,
pub advanced: bool,
pub secret: bool,
pub source: Option<String>,
pub restart: bool,
pub editable: bool,
pub choices: Vec<ChoiceView>,
pub provider: Option<String>,
pub current: Option<String>,
pub staged: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct ChangeView {
pub key: String,
pub from: Option<String>,
pub to: String,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct ActionView {
pub description: String,
pub kind: &'static str,
pub from: Option<String>,
pub to: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct ReviewView {
pub changes: Vec<ChangeView>,
pub restart: Vec<String>,
pub actions: Vec<ActionView>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct SectionView {
pub title: String,
pub keys: Vec<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct FileOnlyView {
pub key: String,
pub kind: &'static str,
pub description: String,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct View {
pub fields: Vec<FieldView>,
pub sections: Vec<SectionView>,
pub file_only: Vec<FileOnlyView>,
pub review: ReviewView,
pub wizard: Option<WizardView>,
}
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(tag = "event", rename_all = "snake_case", deny_unknown_fields)]
pub enum Event {
Set {
key: String,
text: String,
},
Unset {
key: String,
},
Confirm,
Next,
Back,
Goto {
step: String,
},
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct Refusal {
pub key: String,
pub error: String,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct Reply {
pub view: View,
pub refused: Option<Refusal>,
pub confirmed: bool,
}
pub struct Driver<'a> {
session: Session<'a>,
needs: Needs,
confirmed: Option<Confirmed>,
}
impl<'a> Driver<'a> {
pub fn new(session: Session<'a>, needs: Needs) -> Self {
Driver {
session,
needs,
confirmed: None,
}
}
pub fn plain(&self, key: &str) -> Option<String> {
self.session.plain(key)
}
pub fn schema(&self) -> &crate::schema::Schema {
self.session.schema()
}
pub fn view(&self) -> View {
let fields: Vec<FieldView> = self
.session
.fields()
.into_iter()
.map(|f| FieldView {
choices: self
.session
.choices(&f.key, &self.needs)
.into_iter()
.map(|c| ChoiceView {
id: c.id,
reason: c.reason,
})
.collect(),
ty: f.ty.name(),
key: f.key,
description: f.description,
prompt: f.prompt,
example: f.example,
hint: f.hint,
section: f.section,
advanced: f.advanced,
secret: f.secret,
source: f.source,
restart: f.restart,
editable: f.editable,
provider: f.provider,
current: f.current,
staged: f.staged,
})
.collect();
let mut sections: Vec<SectionView> = Vec::new();
for f in &fields {
match sections.iter_mut().find(|s| s.title == f.section) {
Some(s) => s.keys.push(f.key.clone()),
None => sections.push(SectionView {
title: f.section.clone(),
keys: vec![f.key.clone()],
}),
}
}
let file_only = self
.session
.file_only()
.into_iter()
.map(|t| FileOnlyView {
key: t.key,
kind: t.kind,
description: t.description,
})
.collect();
let review = self.session.review();
let actions = review
.plan()
.actions
.iter()
.map(|a| {
let (kind, from, to) = match &a.kind {
ActionKind::Note => ("note", None, None),
ActionKind::DataTransfer { from, to } => {
("data-transfer", Some(from.clone()), Some(to.clone()))
}
ActionKind::Step(_) => ("step", None, None),
};
ActionView {
description: a.description.clone(),
kind,
from,
to,
}
})
.collect();
View {
wizard: None,
fields,
sections,
file_only,
review: ReviewView {
changes: review
.changes()
.iter()
.map(|c| ChangeView {
key: c.key.clone(),
from: c.from.clone(),
to: c.to.clone(),
})
.collect(),
restart: review.restart_needed().to_vec(),
actions,
},
}
}
#[must_use = "a refused event is reported only in the reply"]
pub fn apply(&mut self, event: Event) -> Reply {
let mut refused = None;
let mut confirmed = false;
match event {
Event::Set { key, text } => {
self.confirmed = None;
if let Err(e) = self.session.stage(&key, &text) {
refused = Some(Refusal {
key,
error: e.to_string(),
});
}
}
Event::Unset { key } => {
self.confirmed = None;
self.session.unstage(&key);
}
Event::Next | Event::Back | Event::Goto { .. } => {
refused = Some(Refusal {
key: String::new(),
error: "this session has no steps: wrap the driver in a Wizard to use next, back and goto"
.into(),
});
}
Event::Confirm => {
let review = self.session.review();
if review.is_empty() {
refused = Some(Refusal {
key: String::new(),
error: "there is nothing to confirm: no value has been changed".into(),
});
} else {
self.confirmed = Some(review.confirm());
confirmed = true;
}
}
}
Reply {
view: self.view(),
refused,
confirmed,
}
}
pub fn take_confirmed(&mut self) -> Option<Confirmed> {
self.confirmed.take()
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct StepView {
pub id: String,
pub title: String,
pub state: &'static str,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct WizardView {
pub steps: Vec<StepView>,
pub current: Option<String>,
pub at_review: bool,
pub title: String,
pub intro: String,
pub keys: Vec<String>,
pub can_back: bool,
pub can_next: bool,
pub note: Option<String>,
}