lichtung_plot/spec/
category_labels.rs1use crate::{Error, Result};
2
3#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
5pub enum LabelEndpoints {
6 #[default]
8 None,
9 First,
11 Last,
13 Both,
15}
16
17#[derive(Debug, Clone, PartialEq, Eq)]
26pub struct CategoryLabelSelection {
27 stride: usize,
28 offset: usize,
29 endpoints: LabelEndpoints,
30}
31
32impl Default for CategoryLabelSelection {
33 fn default() -> Self {
34 Self::all()
35 }
36}
37
38impl CategoryLabelSelection {
39 pub const fn all() -> Self {
41 Self {
42 stride: 1,
43 offset: 0,
44 endpoints: LabelEndpoints::None,
45 }
46 }
47
48 pub fn every(stride: usize) -> Result<Self> {
50 if stride == 0 {
51 return Err(Error::Data("category label stride must be positive".into()));
52 }
53 Ok(Self {
54 stride,
55 ..Self::all()
56 })
57 }
58
59 pub fn offset(mut self, offset: usize) -> Result<Self> {
61 if offset >= self.stride {
62 return Err(Error::Data(
63 "category label offset must be smaller than its stride".into(),
64 ));
65 }
66 self.offset = offset;
67 Ok(self)
68 }
69
70 #[must_use]
72 pub const fn endpoints(mut self, value: LabelEndpoints) -> Self {
73 if self.stride != 1 {
74 self.endpoints = value;
75 }
76 self
77 }
78
79 pub(crate) const fn is_all(&self) -> bool {
80 self.stride == 1
81 }
82
83 pub(crate) fn includes(&self, index: usize, count: usize) -> bool {
84 index < count
85 && (index % self.stride == self.offset
86 || (index == 0
87 && matches!(self.endpoints, LabelEndpoints::First | LabelEndpoints::Both))
88 || (index == count - 1
89 && matches!(self.endpoints, LabelEndpoints::Last | LabelEndpoints::Both)))
90 }
91
92 pub(crate) fn indices(
93 &self,
94 count: usize,
95 ) -> impl ExactSizeIterator<Item = usize> + Clone + '_ {
96 let base = if self.offset < count {
97 (count - 1 - self.offset) / self.stride + 1
98 } else {
99 0
100 };
101 let first = count > 0
102 && self.offset != 0
103 && matches!(self.endpoints, LabelEndpoints::First | LabelEndpoints::Both);
104 let last = count > 0
105 && (count - 1) % self.stride != self.offset
106 && matches!(self.endpoints, LabelEndpoints::Last | LabelEndpoints::Both)
107 && (count != 1 || !first);
108 let leading = usize::from(first);
109 (0..(base + leading + usize::from(last))).map(move |ordinal| {
110 if first && ordinal == 0 {
111 0
112 } else if ordinal - leading < base {
113 self.offset + (ordinal - leading) * self.stride
114 } else {
115 count - 1
116 }
117 })
118 }
119
120 pub(crate) fn observe(&self, round: &mut crate::layout::kernel::FeedbackRound) -> Result<()> {
121 round.observe_count(self.stride)?;
122 round.observe_count(self.offset)?;
123 round.observe_count(self.endpoints as usize)
124 }
125}
126
127const LABEL_SELECTION_FIELDS: &[crate::recipe::schema::Field] = &[
128 crate::recipe::schema::mandatory::<usize>("stride"),
129 crate::recipe::schema::optional::<usize>("offset"),
130 crate::recipe::schema::optional::<LabelEndpoints>("endpoints"),
131];
132
133impl crate::recipe::codec::Codec for CategoryLabelSelection {
134 fn schema(ctx: &mut crate::recipe::schema::Schema) -> serde_json::Value {
135 use crate::recipe::schema as grammar;
136 grammar::one_of([
137 grammar::literal("all"),
138 grammar::record(ctx, LABEL_SELECTION_FIELDS),
139 ])
140 }
141 fn encode(&self) -> toml_edit::Value {
142 let mut fields = toml_edit::InlineTable::new();
143 fields.insert("stride", self.stride.encode());
144 fields.insert("offset", self.offset.encode());
145 fields.insert("endpoints", self.endpoints.encode());
146 fields.into()
147 }
148 fn decode(item: &toml_edit::Item, _: Option<Self>) -> Result<Self> {
149 use crate::recipe::codec::field;
150 if item.as_str() == Some("all") {
151 return Ok(Self::all());
152 }
153 let fields = crate::recipe::schema::record_fields(item, LABEL_SELECTION_FIELDS)?;
154 Ok(Self::every(field(fields, "stride", None)?)?
155 .offset(field(fields, "offset", Some(0))?)?
156 .endpoints(field(fields, "endpoints", Some(LabelEndpoints::None))?))
157 }
158}