use std::collections::{BTreeMap, HashMap, HashSet, btree_map::Entry};
use strict_num_extended::NonNegativeF64;
use crate::bms::parse::{
Result,
prompt::{ChannelDuplication, Prompter},
};
use crate::bms::{
command::{StringValue, channel::Channel},
prelude::*,
};
#[derive(Debug, Default, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct StopObjects {
pub stop_defs: HashMap<ObjId, StringValue<NonNegativeF64>>,
pub stops: BTreeMap<ObjTime, StopObj>,
pub stop_ids_used: HashSet<ObjId>,
pub stp_events: BTreeMap<ObjTime, StpEvent>,
}
impl StopObjects {
#[must_use]
pub fn last_obj_time(&self) -> Option<ObjTime> {
self.stops.last_key_value().map(|(&time, _)| time)
}
}
impl StopObjects {
pub fn push_stop(&mut self, stop: StopObj, prompt_handler: &impl Prompter) -> Result<()> {
match self.stops.entry(stop.time) {
Entry::Vacant(entry) => {
entry.insert(stop);
Ok(())
}
Entry::Occupied(mut entry) => {
let existing = entry.get();
prompt_handler
.handle_channel_duplication(ChannelDuplication::StopEvent {
time: stop.time,
older: existing,
newer: &stop,
})
.apply_channel(entry.get_mut(), stop.clone(), stop.time, Channel::Stop)
}
}
}
pub fn push_stop_ignore_duplicate(&mut self, stop: StopObj) {
self.stops.insert(stop.time, stop);
}
}