use crate::common::locale::LocaleMessage;
use blake3::Hash;
use std::fmt::Debug;
use std::sync::Arc;
#[derive(Default)]
pub struct ValidateErrorStore(pub Arc<[(String, Box<dyn LocaleMessage>)]>);
impl Debug for ValidateErrorStore {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
for (i, error) in self.0.iter().enumerate() {
if i > 0 {
write!(f, ", ")?;
}
write!(f, "{:?}", error.0)?;
}
Ok(())
}
}
impl PartialEq for ValidateErrorStore {
fn eq(&self, other: &Self) -> bool {
self.hash() == other.hash()
}
}
impl Clone for ValidateErrorStore {
fn clone(&self) -> Self {
Self(Arc::clone(&self.0))
}
}
impl ValidateErrorStore {
pub fn as_original_message(&self) -> Arc<[String]> {
self.as_original_message_vec().into()
}
pub fn as_original_message_vec(&self) -> Vec<String> {
self.0.iter().map(|e| e.0.clone()).collect()
}
pub fn as_validate_error_collector(&self) -> ValidateErrorCollector {
self.clone().into()
}
fn hash(&self) -> Hash {
let mut hasher = blake3::Hasher::new();
for error in self.0.iter() {
hasher.update(error.0.as_bytes());
}
hasher.finalize()
}
}
impl Into<ValidateErrorCollector> for ValidateErrorStore {
fn into(self) -> ValidateErrorCollector {
let mut errors: Vec<(String, Box<dyn LocaleMessage>)> = vec![];
for error in self.0.iter() {
errors.push((error.0.clone(), Box::new(error.1.get_locale_data())));
}
ValidateErrorCollector(errors)
}
}
#[derive(Default)]
pub struct ValidateErrorCollector(pub Vec<(String, Box<dyn LocaleMessage>)>);
impl Into<ValidateErrorStore> for ValidateErrorCollector {
fn into(self) -> ValidateErrorStore {
ValidateErrorStore(self.0.into())
}
}
impl ValidateErrorCollector {
pub fn new() -> Self {
Self(vec![])
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
pub fn push(&mut self, error: (String, Box<dyn LocaleMessage>)) {
self.0.push(error);
}
pub fn len(&self) -> usize {
self.0.len()
}
}
pub trait AsValidateErrorStore {
fn as_validate_store(&self) -> ValidateErrorStore;
fn as_validate_error_collector(&self) -> ValidateErrorCollector {
self.as_validate_store().as_validate_error_collector()
}
fn as_original_message_vec(&self) -> Vec<String> {
self.as_validate_store().as_original_message_vec()
}
fn as_original_message(&self) -> Arc<[String]> {
self.as_validate_store().as_original_message()
}
}
impl<T, E> AsValidateErrorStore for Result<T, E>
where
for<'a> &'a E: Into<ValidateErrorStore>,
{
fn as_validate_store(&self) -> ValidateErrorStore {
self.as_ref().err().map(Into::into).unwrap_or_default()
}
}