ridstack-form 0.1.0

End-to-End Type-safe form handling for Dioxus applications
use dioxus::prelude::*;
use std::collections::BTreeSet;

pub(crate) mod impls;

use crate::{
    field::{FieldApi, FieldApiState, FieldErrors, RawFieldErrors},
    traits::{FieldError, FieldStateProvider, PrimitiveFieldValue},
};

impl<TFieldError> FieldErrors<TFieldError>
where
    TFieldError: FieldError,
{
    /// Gets the error message with the highest priority. Lowest value in ascending order will have the highest priority.
    pub fn get_most_important_error_msg(&self) -> Option<String> {
        let first_err_ref = self.errors.read();

        let first_err = first_err_ref.first();
        let msg = first_err.map(|e| e.error_value());
        return msg;
    }
}
impl RawFieldErrors {
    /// Gets the error message with the highest priority. Lowest value in ascending order will have the highest priority.
    pub fn get_most_important_error_msg<'a>(&'a self) -> Option<String> {
        let first_err = self.errors.read().first().cloned();

        return first_err;
    }
}

pub fn use_raw_field_errors() -> RawFieldErrors {
    try_use_context::<RawFieldErrors>()
        .expect("You are trying to access field context outside the Field component.")
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, thiserror::Error)]
#[error("{0}")]
pub struct PrimitiveFieldError(pub String);
impl FieldError for PrimitiveFieldError {
    fn custom_message(&self) -> Option<String> {
        Some(self.to_string())
    }
}

mod sealed {
    use crate::traits::{FieldValue, PrimitiveFieldValue};
    use std::ops::{Add, Div, Mul, Sub};

    pub trait SealedNum: PrimitiveFieldValue + FieldValue + Add + Sub + Mul + Div {}
}

pub use sealed::SealedNum;