use std::collections::HashSet;
use crate::js::chars::{drop_indents, trim, trim_start};
use crate::js::number::number;
#[derive(Clone, Debug, Default, PartialEq)]
pub(crate) struct JourneyTask {
pub(crate) task: String,
pub(crate) score: f64,
pub(crate) people: Vec<String>,
pub(crate) section: String,
}
pub(crate) fn journey_actors(tasks: &[JourneyTask]) -> Vec<String> {
let mut seen = HashSet::new();
let mut names: Vec<String> = Vec::new();
for task in tasks {
for name in &task.people {
if seen.insert(name.as_str()) {
names.push(name.clone());
}
}
}
names.sort_by(|a, b| a.encode_utf16().cmp(b.encode_utf16()));
names
}
#[derive(Clone, Debug, Default)]
pub(crate) struct JourneyDb {
pub(crate) title: Option<String>,
pub(crate) acc_title: Option<String>,
pub(crate) acc_descr: Option<String>,
pub(crate) sections: Vec<String>,
pub(crate) tasks: Vec<JourneyTask>,
current_section: String,
}
impl JourneyDb {
pub(crate) fn new(title: Option<&str>) -> JourneyDb {
JourneyDb {
title: title.map(str::to_string),
..JourneyDb::default()
}
}
pub(crate) fn set_diagram_title(&mut self, text: &str) {
self.title = Some(text.to_string());
}
pub(crate) fn set_acc_title(&mut self, text: &str) {
self.acc_title = Some(trim_start(text).to_string());
}
pub(crate) fn set_acc_description(&mut self, text: &str) {
self.acc_descr = Some(drop_indents(text));
}
pub(crate) fn add_section(&mut self, text: &str) {
self.current_section = text.to_string();
self.sections.push(text.to_string());
}
pub(crate) fn add_task(&mut self, task: &str, data: &str) {
let pieces: Vec<&str> = data[1..].split(':').collect();
let people = match pieces.get(1) {
Some(names) => names.split(',').map(|s| trim(s).to_string()).collect(),
None => Vec::new(),
};
self.tasks.push(JourneyTask {
task: task.to_string(),
score: number(pieces[0]),
people,
section: self.current_section.clone(),
});
}
}