lichtung_plot/data/
status.rs1use std::sync::Arc;
4
5use super::{DataSourceInfo, EstimateStatus};
6use crate::{BooleanValues, CategoryValues, Error, Result};
7
8#[derive(Debug, Clone)]
10pub struct EstimateStatusBinding {
11 statuses: Arc<[EstimateStatus]>,
12 source: StatusSource,
13}
14
15#[derive(Debug, Clone)]
16enum StatusSource {
17 Direct(super::binding::VectorSource),
18 Boolean(BooleanValues),
19 Category {
20 values: CategoryValues,
21 mapping: Arc<[(String, EstimateStatus)]>,
22 },
23}
24
25impl EstimateStatusBinding {
26 pub fn new(statuses: impl IntoIterator<Item = EstimateStatus>) -> Self {
28 let statuses: Arc<[_]> = statuses.into_iter().collect();
29 let source = StatusSource::Direct(super::binding::VectorSource::direct(statuses.len()));
30 Self { statuses, source }
31 }
32
33 pub fn boolean(values: BooleanValues) -> Result<Self> {
35 let statuses = super::source::collect_source(values.iter().enumerate().map(
36 |(slot, value)| match value {
37 Some(true) => Ok(EstimateStatus::Estimated),
38 Some(false) => Ok(EstimateStatus::NotEstimable),
39 None => Err(Error::Data(
40 format!("missing Boolean at slot {slot}").into(),
41 )),
42 },
43 ))?;
44 Ok(Self {
45 statuses: statuses.into(),
46 source: StatusSource::Boolean(values),
47 })
48 }
49
50 #[expect(
52 clippy::needless_pass_by_value,
53 reason = "the owning category input retains its exact original projection"
54 )]
55 pub fn categorical<S: AsRef<str>>(
56 values: CategoryValues,
57 mapping: impl IntoIterator<Item = (S, EstimateStatus)>,
58 ) -> Result<Self> {
59 let values = match values.key_values() {
60 super::KeyValues::Strings(values) => values,
61 super::KeyValues::Raw(_) => {
62 return Err(Error::Data(
63 "estimate status mapping requires string category keys".into(),
64 ))
65 }
66 };
67 let mapping =
68 super::source::collect_source(mapping.into_iter().map(|(key, status)| {
69 Ok((super::source::copy_source_string(key.as_ref())?, status))
70 }))?;
71 let statuses =
72 super::source::collect_source(values.iter().enumerate().map(|(slot, value)| {
73 let key = value.ok_or_else(|| {
74 Error::Data(format!("missing category at slot {slot}").into())
75 })?;
76 let mut matches = mapping.iter().filter(|(raw, _)| raw == key);
77 let status = matches
78 .next()
79 .ok_or_else(|| {
80 Error::Data(format!("unmapped category {key:?} at slot {slot}").into())
81 })?
82 .1;
83 if matches.next().is_some() {
84 return Err(Error::Data(format!("duplicate mapping for {key:?}").into()));
85 }
86 Ok(status)
87 }))?;
88 Ok(Self {
89 statuses: statuses.into(),
90 source: StatusSource::Category {
91 values,
92 mapping: mapping.into(),
93 },
94 })
95 }
96
97 pub fn statuses(&self) -> &[EstimateStatus] {
99 &self.statuses
100 }
101
102 pub const fn get_boolean(&self) -> Option<&BooleanValues> {
104 match &self.source {
105 StatusSource::Boolean(values) => Some(values),
106 _ => None,
107 }
108 }
109
110 pub fn get_categorical(&self) -> Option<(&CategoryValues, &[(String, EstimateStatus)])> {
112 match &self.source {
113 StatusSource::Category { values, mapping } => Some((values, mapping)),
114 _ => None,
115 }
116 }
117
118 pub(crate) fn shared_statuses(&self) -> Arc<[EstimateStatus]> {
119 self.statuses.clone()
120 }
121
122 pub(crate) fn vector_source(&self) -> &super::binding::VectorSource {
123 match &self.source {
124 StatusSource::Direct(source) => source,
125 StatusSource::Boolean(values) => values.vector_source(),
126 StatusSource::Category { values, .. } => values.vector_source(),
127 }
128 }
129
130 pub fn get_source(&self) -> &DataSourceInfo {
132 match &self.source {
133 StatusSource::Direct(source) => source.info(),
134 StatusSource::Boolean(values) => values.get_source(),
135 StatusSource::Category { values, .. } => values.get_source(),
136 }
137 }
138}