use super::{
CuttingStep,
progress::{Progress, StepState, next_position, previous_position},
};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ChipFace {
pub text: String,
pub ticked: bool,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StepPage {
pub heading: String,
pub tier_name: String,
pub side: String,
pub angle: String,
pub chips: Vec<ChipFace>,
pub meet: String,
pub depth: String,
pub cheater: String,
pub notes: String,
pub tool: String,
pub state: StepState,
pub state_text: String,
pub has_previous: bool,
pub has_next: bool,
}
impl StepPage {
#[must_use]
pub fn new(steps: &[CuttingStep], position: usize, progress: &Progress) -> Option<Self> {
let step = steps.get(position)?;
let state = progress.state(step);
Some(Self {
heading: format!("Step {} of {}", step.number, steps.len()),
tier_name: step.code.clone(),
side: step.side.label().to_owned(),
angle: format!("{:.2}°", step.angle_deg),
chips: step
.indices
.iter()
.enumerate()
.map(|(chip, index)| ChipFace {
text: index.text.clone(),
ticked: progress.chip_ticked(step, chip),
})
.collect(),
meet: step.meet.clone(),
depth: depth_text(step),
cheater: step
.cheater_offset_deg
.map_or_else(String::new, |deg| format!("Cheater offset {deg:+.2}°")),
notes: step.notes.clone(),
tool: step.tool_line.clone().unwrap_or_default(),
state,
state_text: state_text(state).to_owned(),
has_previous: previous_position(position).is_some(),
has_next: next_position(position, steps.len()).is_some(),
})
}
}
fn depth_text(step: &CuttingStep) -> String {
match (step.mast, step.depth_mm) {
(Some(mast), Some(mm)) => format!("Depth {mm:.2} mm · mast {mast:.4}"),
(Some(mast), None) => format!("Mast {mast:.4}"),
(None, _) => String::new(),
}
}
#[must_use]
pub const fn state_text(state: StepState) -> &'static str {
match state {
StepState::NotDone => "",
StepState::Done => "Done",
StepState::Changed => "Changed since you marked it",
}
}
#[must_use]
pub fn progress_line(steps: &[CuttingStep], progress: &Progress) -> String {
let done = progress.done_count(steps);
let changed = progress.changed_count(steps);
let noun = if steps.len() == 1 { "step" } else { "steps" };
let changed_note = match changed {
0 => String::new(),
1 => " · 1 step changed since you marked it".to_owned(),
many => format!(" · {many} steps changed since you marked them"),
};
format!("{done} of {} {noun} done{changed_note}", steps.len())
}
#[must_use]
pub fn progress_fraction(steps: &[CuttingStep], progress: &Progress) -> f32 {
if steps.is_empty() {
return 0.0;
}
progress.done_count(steps) as f32 / steps.len() as f32
}
#[must_use]
pub fn stone_caption(step: &CuttingStep, step_count: usize) -> String {
format!(
"The stone after step {} of {step_count}, {} highlighted",
step.number, step.code
)
}
#[must_use]
pub fn wheel_caption(gear_teeth: u32, step: &CuttingStep) -> String {
let count = step.indices.len();
let noun = if count == 1 { "index" } else { "indices" };
format!("{gear_teeth}-tooth index wheel, {count} {noun} marked")
}