Lines
96.34 %
Functions
38.1 %
Branches
100 %
//! The typed text of a select control. A browser shows an entity by its
//! label in a control named `label:<address>` and submits its id in a hidden
//! input named by the address; the text is only echoed back, never resolved.
use std::collections::BTreeMap;
use crate::descriptor::{Form, Kind, Source};
use crate::pairs::{Address, resolve};
use crate::values::{FieldError, FormValues};
pub const LABEL_PREFIX: &str = "label:";
/// Names a select control whose text a fill wrote, so a later fill may move it.
pub const FILLED_PREFIX: &str = "filled:";
/// The value of a `filled:` mark whose cell was copied from the split above.
pub const COPIED_MARK: &str = "copied";
/// The message of a select control whose text picks no entity.
pub const UNPICKED: &str = "pick an entry from the list";
/// The select address `name` stands for once `prefix` is taken off.
fn prefixed_select<'a>(form: &Form, name: &'a str, prefix: &str) -> Option<&'a str> {
let address = name.strip_prefix(prefix)?;
let field = match resolve(form, address)? {
Address::Flat(field) | Address::Cell(_, _, field) => field,
Address::Row(..) => return None,
};
matches!(field.kind, Kind::Select { .. }).then_some(address)
}
/// Whether `name` is a `label:` or `filled:` companion of a select control.
pub(crate) fn is_select_companion(form: &Form, name: &str) -> bool {
[LABEL_PREFIX, FILLED_PREFIX]
.into_iter()
.any(|prefix| prefixed_select(form, name, prefix).is_some())
/// The typed text of every select control in `pairs`, by address.
#[must_use]
pub fn select_labels(form: &Form, pairs: &[(String, String)]) -> BTreeMap<String, String> {
pairs
.iter()
.filter_map(|(name, text)| {
Some((
prefixed_select(form, name, LABEL_PREFIX)?.to_string(),
text.clone(),
))
})
.collect()
/// The select controls in `pairs` marked as written by a fill, by address;
/// `true` marks a cell copied from the split above.
pub fn select_filled(form: &Form, pairs: &[(String, String)]) -> BTreeMap<String, bool> {
.filter_map(|(name, mark)| {
let address = prefixed_select(form, name, FILLED_PREFIX)?;
Some((address.to_string(), mark == COPIED_MARK))
fn value_at<'v>(form: &Form, values: &'v FormValues, address: &str) -> Option<&'v str> {
match resolve(form, address)? {
Address::Flat(field) => values.text(field.key),
Address::Cell(field, index, cell) => values
.rows(field.key)?
.get(index)?
.get(cell.key)
.map(String::as_str),
Address::Row(..) => None,
fn source_at(form: &Form, address: &str) -> Option<Source> {
match field.kind {
Kind::Select { source, .. } => Some(source),
_ => None,
fn unpicked_at(address: &str) -> FieldError {
FieldError {
field: address.to_string(),
message: UNPICKED.to_string(),
/// A field error at every select control holding text but no entity: such
/// text is never submitted as a name.
pub fn unpicked(
form: &Form,
labels: &BTreeMap<String, String>,
values: &FormValues,
) -> Vec<FieldError> {
labels
.filter(|(address, text)| {
!text.trim().is_empty()
&& value_at(form, values, address).is_none_or(|id| id.trim().is_empty())
.map(|(address, _)| unpicked_at(address))
/// A field error at every select control whose typed text is not the
/// current label of the entity its value names: the text was retyped where
/// no pick could see it. `label_of` gives that label, and `None` for a value
/// naming no known entity, which resolution judges instead.
pub fn mismatched(
label_of: impl Fn(Source, &str) -> Option<String>,
let text = text.trim();
let current = source_at(form, address)
.zip(value_at(form, values, address))
.and_then(|(source, value)| label_of(source, value.trim()));
!text.is_empty() && current.is_some_and(|label| label != text)