1
//! The typed text of a select control. A browser shows an entity by its
2
//! label in a control named `label:<address>` and submits its id in a hidden
3
//! input named by the address; the text is only echoed back, never resolved.
4

            
5
use std::collections::BTreeMap;
6

            
7
use crate::descriptor::{Form, Kind, Source};
8
use crate::pairs::{Address, resolve};
9
use crate::values::{FieldError, FormValues};
10

            
11
pub const LABEL_PREFIX: &str = "label:";
12

            
13
/// Names a select control whose text a fill wrote, so a later fill may move it.
14
pub const FILLED_PREFIX: &str = "filled:";
15

            
16
/// The value of a `filled:` mark whose cell was copied from the split above.
17
pub const COPIED_MARK: &str = "copied";
18

            
19
/// The message of a select control whose text picks no entity.
20
pub const UNPICKED: &str = "pick an entry from the list";
21

            
22
/// The select address `name` stands for once `prefix` is taken off.
23
2634816
fn prefixed_select<'a>(form: &Form, name: &'a str, prefix: &str) -> Option<&'a str> {
24
2634816
    let address = name.strip_prefix(prefix)?;
25
77568
    let field = match resolve(form, address)? {
26
77184
        Address::Flat(field) | Address::Cell(_, _, field) => field,
27
192
        Address::Row(..) => return None,
28
    };
29
77184
    matches!(field.kind, Kind::Select { .. }).then_some(address)
30
2634816
}
31

            
32
/// Whether `name` is a `label:` or `filled:` companion of a select control.
33
815040
pub(crate) fn is_select_companion(form: &Form, name: &str) -> bool {
34
815040
    [LABEL_PREFIX, FILLED_PREFIX]
35
815040
        .into_iter()
36
1584576
        .any(|prefix| prefixed_select(form, name, prefix).is_some())
37
815040
}
38

            
39
/// The typed text of every select control in `pairs`, by address.
40
#[must_use]
41
70848
pub fn select_labels(form: &Form, pairs: &[(String, String)]) -> BTreeMap<String, String> {
42
70848
    pairs
43
70848
        .iter()
44
562176
        .filter_map(|(name, text)| {
45
            Some((
46
562176
                prefixed_select(form, name, LABEL_PREFIX)?.to_string(),
47
30144
                text.clone(),
48
            ))
49
562176
        })
50
        .collect()
51
70848
}
52

            
53
/// The select controls in `pairs` marked as written by a fill, by address;
54
/// `true` marks a cell copied from the split above.
55
#[must_use]
56
62592
pub fn select_filled(form: &Form, pairs: &[(String, String)]) -> BTreeMap<String, bool> {
57
62592
    pairs
58
62592
        .iter()
59
488064
        .filter_map(|(name, mark)| {
60
488064
            let address = prefixed_select(form, name, FILLED_PREFIX)?;
61
384
            Some((address.to_string(), mark == COPIED_MARK))
62
488064
        })
63
        .collect()
64
62592
}
65

            
66
33984
fn value_at<'v>(form: &Form, values: &'v FormValues, address: &str) -> Option<&'v str> {
67
33984
    match resolve(form, address)? {
68
1920
        Address::Flat(field) => values.text(field.key),
69
32064
        Address::Cell(field, index, cell) => values
70
32064
            .rows(field.key)?
71
32064
            .get(index)?
72
32064
            .get(cell.key)
73
32064
            .map(String::as_str),
74
        Address::Row(..) => None,
75
    }
76
33984
}
77

            
78
18624
fn source_at(form: &Form, address: &str) -> Option<Source> {
79
18624
    let field = match resolve(form, address)? {
80
18624
        Address::Flat(field) | Address::Cell(_, _, field) => field,
81
        Address::Row(..) => return None,
82
    };
83
18624
    match field.kind {
84
18624
        Kind::Select { source, .. } => Some(source),
85
        _ => None,
86
    }
87
18624
}
88

            
89
1344
fn unpicked_at(address: &str) -> FieldError {
90
1344
    FieldError {
91
1344
        field: address.to_string(),
92
1344
        message: UNPICKED.to_string(),
93
1344
    }
94
1344
}
95

            
96
/// A field error at every select control holding text but no entity: such
97
/// text is never submitted as a name.
98
#[must_use]
99
61248
pub fn unpicked(
100
61248
    form: &Form,
101
61248
    labels: &BTreeMap<String, String>,
102
61248
    values: &FormValues,
103
61248
) -> Vec<FieldError> {
104
61248
    labels
105
61248
        .iter()
106
18816
        .filter(|(address, text)| {
107
18816
            !text.trim().is_empty()
108
15360
                && value_at(form, values, address).is_none_or(|id| id.trim().is_empty())
109
18816
        })
110
960
        .map(|(address, _)| unpicked_at(address))
111
        .collect()
112
61248
}
113

            
114
/// A field error at every select control whose typed text is not the
115
/// current label of the entity its value names: the text was retyped where
116
/// no pick could see it. `label_of` gives that label, and `None` for a value
117
/// naming no known entity, which resolution judges instead.
118
#[must_use]
119
318
pub fn mismatched(
120
318
    form: &Form,
121
318
    labels: &BTreeMap<String, String>,
122
318
    values: &FormValues,
123
318
    label_of: impl Fn(Source, &str) -> Option<String>,
124
318
) -> Vec<FieldError> {
125
318
    labels
126
318
        .iter()
127
97
        .filter(|(address, text)| {
128
97
            let text = text.trim();
129
97
            let current = source_at(form, address)
130
97
                .zip(value_at(form, values, address))
131
97
                .and_then(|(source, value)| label_of(source, value.trim()));
132
97
            !text.is_empty() && current.is_some_and(|label| label != text)
133
97
        })
134
2
        .map(|(address, _)| unpicked_at(address))
135
        .collect()
136
318
}