Skip to main content

tree_table/types/
header_cell.rs

1use super::cell_props::CellProps;
2use super::val::Val;
3use crate::{Change, EventData};
4use alloc::borrow::Cow;
5use alloc::rc::Rc;
6use alloc::string::String;
7
8#[cfg_attr(feature = "tsify", derive(tsify::Tsify))]
9#[cfg_attr(feature = "tsify", tsify(from_wasm_abi))]
10#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
11#[derive(Debug, Clone, PartialEq)]
12pub struct HeaderCell {
13    pub iid: Rc<str>,
14    pub val: Val,
15    pub dim: u64,
16    pub vis: bool,
17    pub props: Option<CellProps>,
18}
19
20impl HeaderCell {
21    pub fn new(val: Val, iid: Rc<str>, dim: u64, vis: bool) -> Self {
22        HeaderCell {
23            iid,
24            val,
25            dim,
26            vis,
27            props: None,
28        }
29    }
30
31    #[inline(always)]
32    pub fn event_data_apply_own_hformat(
33        &mut self,
34        col: usize,
35        event_data: &mut EventData,
36        normalize: bool,
37        ignore_fmt_errs: bool,
38    ) -> Result<bool, String> {
39        let Some(fmt) = self
40            .props
41            .as_ref()
42            .and_then(|props| props.data_format.as_ref())
43        else {
44            return Ok(false);
45        };
46
47        match fmt.try_format_val(&self.val, normalize) {
48            Ok(Cow::Owned(new_val)) => {
49                if new_val != self.val {
50                    let change = self.record_set_val(new_val);
51                    event_data.changes.push(Change::HeaderCell { col, change });
52                    Ok(true)
53                } else {
54                    Ok(false)
55                }
56            }
57            Ok(Cow::Borrowed(_)) => Ok(false), // same-type + !normalize = no-op
58            Err(e) => {
59                if ignore_fmt_errs {
60                    Ok(false)
61                } else {
62                    Err(e)
63                }
64            }
65        }
66    }
67}