use crate::EguiValidationReport;
use std::borrow::Cow;
use std::collections::BTreeMap;
pub use crate::_garde_field_path as field_path;
use crate::validation_report::IntoFieldPath;
pub use garde;
use garde::Path;
#[macro_export]
macro_rules! _garde_field_path {
(
$($field:expr $(,)?)+
) => {
$crate::garde::garde::Path::empty()
$(
.join($field)
)+
};
}
pub struct GardeReport(BTreeMap<garde::Path, garde::Error>);
impl GardeReport {
pub fn new(result: Result<(), garde::Report>) -> Self {
if let Err(errors) = result {
GardeReport(errors.iter().cloned().collect())
} else {
GardeReport(BTreeMap::new())
}
}
}
impl EguiValidationReport for GardeReport {
type FieldPath<'a> = Path;
type Errors = BTreeMap<Path, garde::Error>;
fn get_field_error(&self, field: Self::FieldPath<'_>) -> Option<Cow<'static, str>> {
self.0.get(&field).map(|e| e.to_string().into())
}
fn has_errors(&self) -> bool {
!self.0.is_empty()
}
fn error_count(&self) -> usize {
self.0.len()
}
fn get_errors(&self) -> Option<&Self::Errors> {
if self.has_errors() {
Some(&self.0)
} else {
None
}
}
}
impl IntoFieldPath<Path> for Path {
fn into_field_path(self) -> Path {
self
}
}
impl IntoFieldPath<Path> for &str {
fn into_field_path(self) -> Path {
Path::new(self)
}
}
impl IntoFieldPath<Path> for String {
fn into_field_path(self) -> Path {
Path::new(self)
}
}
impl IntoFieldPath<Path> for usize {
fn into_field_path(self) -> Path {
Path::new(self)
}
}