Skip to main content

formars_signals/
submit.rs

1//! The submit boundary: point-in-time snapshots, sync validation gating,
2//! and the composed handler future.
3
4use formars_core::error::{FieldPath, FormaError};
5use formars_core::value::Value;
6
7/// Point-in-time copy of every field value, taken at the submit gate in
8/// registry declaration order. Plain owned data — later edits cannot mutate
9/// it.
10#[derive(Clone, Debug, Default, PartialEq)]
11pub struct FormSnapshot {
12    pub(crate) entries: Vec<(FieldPath, Value)>,
13}
14
15impl FormSnapshot {
16    /// The captured value for `path`, if the field was registered.
17    #[must_use]
18    pub fn get(&self, path: &FieldPath) -> Option<&Value> {
19        self.entries.iter().find(|(p, _)| p == path).map(|(_, v)| v)
20    }
21
22    /// Iterates `(path, value)` pairs in declaration order.
23    pub fn iter(&self) -> impl Iterator<Item = (&FieldPath, &Value)> {
24        self.entries.iter().map(|(p, v)| (p, v))
25    }
26
27    /// Number of captured fields.
28    #[must_use]
29    pub fn len(&self) -> usize {
30        self.entries.len()
31    }
32
33    /// True when no fields were registered at capture time.
34    #[must_use]
35    pub fn is_empty(&self) -> bool {
36        self.entries.is_empty()
37    }
38}
39
40/// Why a composed submit future failed.
41///
42/// `Validation` means the synchronous pre-handler gate rejected the form and
43/// the user handler was never constructed; `Handler` carries the handler's
44/// own error after it ran to completion.
45#[derive(Debug)]
46pub enum SubmitError<E> {
47    /// Whole-form validation failed before the handler was invoked.
48    Validation(FormaError),
49    /// The handler ran and resolved with an error.
50    Handler(E),
51}
52
53impl<E: std::fmt::Debug> std::fmt::Display for SubmitError<E> {
54    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
55        match self {
56            Self::Validation(err) => write!(f, "validation failed: {err}"),
57            Self::Handler(e) => write!(f, "submit handler failed: {e:?}"),
58        }
59    }
60}
61
62impl<E: std::fmt::Debug> std::error::Error for SubmitError<E> {}