use super::stored::LightingValues;
use std::collections::HashSet;
#[derive(Debug, Clone, PartialEq)]
pub(super) enum Lookup {
NoRow,
Row(LightingValues),
UnknownRig(String),
Unreadable(String),
}
impl Lookup {
pub(super) const fn has_row(&self) -> bool {
!matches!(self, Self::NoRow)
}
pub(super) fn summary_text(&self) -> String {
match self {
Self::NoRow => String::new(),
Self::Row(values) => values.summary(),
Self::UnknownRig(rig) => format!(
"The saved lighting uses '{rig}', which this version does not have, \
so your normal lighting is used."
),
Self::Unreadable(reason) => {
format!("The saved lighting could not be read ({reason}).")
}
}
}
}
pub(super) fn lookup_from_row(settings_json: Option<&str>) -> Lookup {
let Some(text) = settings_json else {
return Lookup::NoRow;
};
match LightingValues::from_json(text) {
Err(reason) => Lookup::Unreadable(reason),
Ok(values) if values.rig().is_none() => Lookup::UnknownRig(values.lighting_rig),
Ok(values) => Lookup::Row(values),
}
}
#[derive(Debug, Clone, PartialEq)]
pub(super) enum Plan {
Keep,
Apply(LightingValues),
Restore,
}
#[derive(Debug, Clone, PartialEq)]
pub(super) enum Note {
UsingSaved,
BackToNormal,
UnknownRig(String),
Unreadable(String),
}
#[derive(Debug, Clone, PartialEq)]
pub(super) struct Decision {
pub(super) plan: Plan,
pub(super) note: Option<Note>,
}
pub(super) fn decide(lookup: &Lookup, override_active: bool) -> Decision {
let back = if override_active {
Plan::Restore
} else {
Plan::Keep
};
match lookup {
Lookup::NoRow => Decision {
note: override_active.then_some(Note::BackToNormal),
plan: back,
},
Lookup::Row(values) => Decision {
plan: Plan::Apply(values.clone()),
note: Some(Note::UsingSaved),
},
Lookup::UnknownRig(rig) => Decision {
plan: back,
note: Some(Note::UnknownRig(rig.clone())),
},
Lookup::Unreadable(reason) => Decision {
plan: back,
note: Some(Note::Unreadable(reason.clone())),
},
}
}
pub(super) fn note_text(note: &Note) -> (String, &'static str) {
match note {
Note::UsingSaved => ("Using this design's saved lighting.".to_string(), "info"),
Note::BackToNormal => ("Back to your normal lighting.".to_string(), "info"),
Note::UnknownRig(rig) => (
format!(
"This design's saved lighting uses '{rig}', which this version does not \
have. Your normal lighting is used instead."
),
"warning",
),
Note::Unreadable(reason) => (
format!(
"This design's saved lighting could not be read ({reason}). Your normal \
lighting is used instead."
),
"warning",
),
}
}
pub(super) fn first_time_for(told: &mut HashSet<String>, design_uuid: &str) -> bool {
told.insert(design_uuid.to_string())
}
pub(super) fn may_show_over(toast_visible: bool, toast_kind: &str) -> bool {
!(toast_visible && matches!(toast_kind, "warning" | "error"))
}