1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
use crate::Error;
/// A runtime model value, distinct from a component's display configuration.
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub enum Value {
/// A finite numerical value.
Number(f64),
/// A text value, such as a category or class label.
Text(String),
}
impl Value {
/// Read a numerical value or return a callback-friendly error message.
pub fn as_number(&self) -> Result<f64, String> {
match self {
Self::Number(value) => Ok(*value),
Self::Text(_) => Err("Expected a numerical value.".into()),
}
}
/// Read a text value or return a callback-friendly error message.
pub fn as_text(&self) -> Result<&str, String> {
match self {
Self::Text(value) => Ok(value),
Self::Number(_) => Err("Expected a text value.".into()),
}
}
}
/// A dropdown label and the value passed to the model when selected.
#[derive(Clone, Debug, PartialEq)]
pub struct Choice {
/// Plain-text label displayed in the dropdown.
pub label: String,
/// Model value; dropdown messages carry indices, never arbitrary values.
pub value: Value,
}
impl Choice {
/// Define a labeled numerical choice.
pub fn number(label: impl Into<String>, value: f64) -> Self {
Self {
label: label.into(),
value: Value::Number(value),
}
}
/// Define a labeled text choice.
pub fn text(label: impl Into<String>, value: impl Into<String>) -> Self {
Self {
label: label.into(),
value: Value::Text(value.into()),
}
}
}
/// An input component definition. All fields are required in submitted forms.
#[derive(Clone, Debug)]
#[non_exhaustive]
pub enum Input {
/// A numerical textbox with optional inclusive bounds.
Number {
/// Plain-text label, or a generated one-based label.
label: Option<String>,
/// Initial finite value.
initial_value: f64,
/// Inclusive minimum, when present.
min: Option<f64>,
/// Inclusive maximum, when present.
max: Option<f64>,
},
/// A bounded numerical slider.
Slider {
/// Plain-text label, or a generated one-based label.
label: Option<String>,
/// Inclusive minimum and step origin.
min: f64,
/// Inclusive maximum.
max: f64,
/// Positive finite increment from the minimum.
step: f64,
/// Initial finite value, on a step within the range.
initial_value: f64,
},
/// A dropdown whose options may carry numerical or text values.
Dropdown {
/// Plain-text label, or a generated one-based label.
label: Option<String>,
/// Non-empty list of labeled choices.
options: Vec<Choice>,
/// Zero-based index of the initially selected option.
initial_value: usize,
},
}
impl Default for Input {
fn default() -> Self {
Self::Number {
label: None,
initial_value: 0.0,
min: None,
max: None,
}
}
}
impl Input {
/// Create an unbounded numerical textbox with a label.
pub fn number(label: impl Into<String>, initial_value: f64) -> Self {
Self::Number {
label: Some(label.into()),
initial_value,
min: None,
max: None,
}
}
pub(crate) fn label(&self, index: usize) -> String {
let (Self::Number { label, .. }
| Self::Slider { label, .. }
| Self::Dropdown { label, .. }) = self;
label
.clone()
.unwrap_or_else(|| format!("Input {}", index + 1))
}
pub(crate) fn is_numeric(&self) -> bool {
match self {
Self::Dropdown { options, .. } => options
.iter()
.all(|choice| matches!(choice.value, Value::Number(_))),
_ => true,
}
}
pub(crate) fn error(&self, index: usize, message: &str) -> Error {
Error::Input {
field: Some(index),
message: format!("{}: {message}", self.label(index)),
}
}
pub(crate) fn validate(&self, index: usize) -> Result<(), Error> {
let invalid =
|message: &str| Error::Configuration(format!("{}: {message}", self.label(index)));
let initial = match self {
Self::Number {
initial_value,
min,
max,
..
} => {
if min.iter().chain(max).any(|bound| !bound.is_finite())
|| matches!((min, max), (Some(a), Some(b)) if a > b)
{
return Err(invalid("Bounds must be finite and ordered."));
}
Value::Number(*initial_value)
}
Self::Slider {
min,
max,
step,
initial_value,
..
} => {
if !min.is_finite()
|| !max.is_finite()
|| min >= max
|| !step.is_finite()
|| *step <= 0.0
|| !((max - min) / step).is_finite()
{
return Err(invalid(
"Slider bounds must be finite and increasing, with a positive finite step.",
));
}
Value::Number(*initial_value)
}
Self::Dropdown {
options,
initial_value,
..
} => {
if options.is_empty() || *initial_value >= options.len() {
return Err(invalid(
"Choose an initial index within a non-empty list of options.",
));
}
if options
.iter()
.any(|choice| matches!(choice.value, Value::Number(n) if !n.is_finite()))
{
return Err(invalid("Option values must be finite."));
}
options[*initial_value].value.clone()
}
};
self.validate_value(index, &initial)
.map_err(|error| invalid(&error.to_string()))
}
pub(crate) fn validate_value(&self, index: usize, value: &Value) -> Result<(), Error> {
let invalid = |message: &str| self.error(index, message);
if matches!(value, Value::Number(n) if !n.is_finite()) {
return Err(invalid("Enter a finite number."));
}
match self {
Self::Number { min, max, .. } => {
let n = value.as_number().map_err(|_| invalid("Enter a number."))?;
if min.is_some_and(|min| n < min) || max.is_some_and(|max| n > max) {
return Err(invalid("Value is outside the allowed range."));
}
}
Self::Slider { min, max, step, .. } => {
let n = value.as_number().map_err(|_| invalid("Enter a number."))?;
if n < *min || n > *max {
return Err(invalid("Value is outside the slider range."));
}
let steps = (n - min) / step;
let tolerance = (8.0 * f64::EPSILON * steps.abs().max(1.0)).min(1e-6);
if !steps.is_finite() || (steps - steps.round()).abs() > tolerance {
return Err(invalid("Value must follow the slider step."));
}
}
Self::Dropdown { options, .. } => {
if !options.iter().any(|choice| choice.value == *value) {
return Err(invalid("Choose one of the available options."));
}
}
}
Ok(())
}
}
/// A result display definition; actual results are returned as [`Value`]s.
#[derive(Clone, Debug)]
#[non_exhaustive]
pub enum Output {
/// A finite numerical result.
Number {
/// Plain-text label, or a generated one-based label.
label: Option<String>,
/// Decimal places, from zero through fifteen.
precision: usize,
},
/// A plain-text result such as a class label.
Text {
/// Plain-text label, or a generated one-based label.
label: Option<String>,
},
}
impl Default for Output {
fn default() -> Self {
Self::Number {
label: Some("Result".into()),
precision: 2,
}
}
}
impl Output {
/// Define a numerical result and its decimal precision.
pub fn number(label: impl Into<String>, precision: usize) -> Self {
Self::Number {
label: Some(label.into()),
precision,
}
}
/// Define a plain-text result.
pub fn text(label: impl Into<String>) -> Self {
Self::Text {
label: Some(label.into()),
}
}
pub(crate) fn label(&self, index: usize) -> String {
let (Self::Number { label, .. } | Self::Text { label }) = self;
label
.clone()
.unwrap_or_else(|| format!("Result {}", index + 1))
}
}