bms_rs/bms/model/
text.rs

1//! This module introduces struct [`TextObjects`], which manages definitions and events of caption texts.
2
3use std::collections::{BTreeMap, HashMap, btree_map::Entry};
4
5use crate::{
6    bms::{error::Result, prelude::*},
7    parse::prompt::ChannelDuplication,
8};
9
10#[derive(Debug, Default, Clone, PartialEq, Eq)]
11#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
12/// This aggregate manages definitions and events of caption texts.
13pub struct TextObjects {
14    /// Storage for `#TEXT` definitions
15    /// The texts corresponding to the id of the text object.
16    pub texts: HashMap<ObjId, String>,
17    /// Text events, indexed by time. `#xxx99:`
18    pub text_events: BTreeMap<ObjTime, TextObj>,
19}
20
21impl TextObjects {
22    /// Adds a new text object to the notes.
23    pub fn push_text_event(
24        &mut self,
25        text_obj: TextObj,
26        prompt_handler: &impl Prompter,
27    ) -> Result<()> {
28        match self.text_events.entry(text_obj.time) {
29            Entry::Vacant(entry) => {
30                entry.insert(text_obj);
31                Ok(())
32            }
33            Entry::Occupied(mut entry) => {
34                let existing = entry.get();
35
36                prompt_handler
37                    .handle_channel_duplication(ChannelDuplication::TextEvent {
38                        time: text_obj.time,
39                        older: existing,
40                        newer: &text_obj,
41                    })
42                    .apply_channel(
43                        entry.get_mut(),
44                        text_obj.clone(),
45                        text_obj.time,
46                        Channel::Text,
47                    )
48            }
49        }
50    }
51}