use super::CuttingStep;
use std::collections::BTreeMap;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StepState {
NotDone,
Done,
Changed,
}
impl StepState {
#[must_use]
pub const fn code(self) -> i32 {
match self {
Self::NotDone => 0,
Self::Done => 1,
Self::Changed => 2,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum MarkChange {
Mark {
key: String,
signature: String,
},
Unmark {
key: String,
},
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Progress {
marks: BTreeMap<String, String>,
}
impl Progress {
#[must_use]
pub fn from_marks<I>(marks: I) -> Self
where
I: IntoIterator<Item = (String, String)>,
{
Self {
marks: marks.into_iter().collect(),
}
}
#[must_use]
pub fn state(&self, step: &CuttingStep) -> StepState {
match self.marks.get(&step.key) {
None => StepState::NotDone,
Some(signature) if *signature == step.signature => StepState::Done,
Some(_) => StepState::Changed,
}
}
#[must_use]
pub fn chip_ticked(&self, step: &CuttingStep, chip: usize) -> bool {
if self.state(step) == StepState::Done {
return true;
}
step.indices
.get(chip)
.is_some_and(|index| self.marks.get(&index.key) == Some(&step.signature))
}
#[must_use]
pub fn done_count(&self, steps: &[CuttingStep]) -> usize {
steps
.iter()
.filter(|step| self.state(step) == StepState::Done)
.count()
}
#[must_use]
pub fn changed_count(&self, steps: &[CuttingStep]) -> usize {
steps
.iter()
.filter(|step| self.state(step) == StepState::Changed)
.count()
}
#[must_use]
pub fn first_open(&self, steps: &[CuttingStep]) -> Option<usize> {
steps
.iter()
.position(|step| self.state(step) != StepState::Done)
}
#[must_use]
pub fn resume_position(&self, steps: &[CuttingStep]) -> usize {
self.first_open(steps)
.unwrap_or_else(|| steps.len().saturating_sub(1))
}
#[must_use]
pub fn toggle_done(&self, step: &CuttingStep) -> Vec<MarkChange> {
if self.state(step) == StepState::Done {
let mut changes = vec![MarkChange::Unmark {
key: step.key.clone(),
}];
changes.extend(
step.indices
.iter()
.filter(|index| self.marks.contains_key(&index.key))
.map(|index| MarkChange::Unmark {
key: index.key.clone(),
}),
);
changes
} else {
vec![mark(&step.key, step)]
}
}
#[must_use]
pub fn mark_done(&self, step: &CuttingStep) -> Vec<MarkChange> {
if self.state(step) == StepState::Done {
Vec::new()
} else {
vec![mark(&step.key, step)]
}
}
#[must_use]
pub fn toggle_chip(&self, step: &CuttingStep, chip: usize) -> Vec<MarkChange> {
let Some(index) = step.indices.get(chip) else {
return Vec::new();
};
if !self.chip_ticked(step, chip) {
let mut changes = vec![mark(&index.key, step)];
let others_ticked =
(0..step.indices.len()).all(|other| other == chip || self.chip_ticked(step, other));
if others_ticked {
changes.push(mark(&step.key, step));
}
return changes;
}
if self.state(step) == StepState::Done {
let mut changes = vec![MarkChange::Unmark {
key: step.key.clone(),
}];
for (other, other_index) in step.indices.iter().enumerate() {
if other == chip {
changes.push(MarkChange::Unmark {
key: other_index.key.clone(),
});
} else if self.marks.get(&other_index.key) != Some(&step.signature) {
changes.push(mark(&other_index.key, step));
}
}
return changes;
}
vec![MarkChange::Unmark {
key: index.key.clone(),
}]
}
pub fn apply(&mut self, changes: &[MarkChange]) {
for change in changes {
match change {
MarkChange::Mark { key, signature } => {
self.marks.insert(key.clone(), signature.clone());
}
MarkChange::Unmark { key } => {
self.marks.remove(key);
}
}
}
}
pub fn clear(&mut self) {
self.marks.clear();
}
}
fn mark(key: &str, step: &CuttingStep) -> MarkChange {
MarkChange::Mark {
key: key.to_owned(),
signature: step.signature.clone(),
}
}
#[must_use]
pub const fn next_position(position: usize, count: usize) -> Option<usize> {
if position + 1 < count {
Some(position + 1)
} else {
None
}
}
#[must_use]
pub const fn previous_position(position: usize) -> Option<usize> {
position.checked_sub(1)
}