Skip to content

Commit

Permalink
unify code list validation trait with xwasser validation
Browse files Browse the repository at this point in the history
  • Loading branch information
kilork committed Nov 15, 2024
1 parent 06234b3 commit b6a9980
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 21 deletions.
9 changes: 9 additions & 0 deletions crates/codelists/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ impl CodeListsProvider for CodeLists {
}
}

impl<T> CodeListsProvider for Arc<T>
where
T: CodeListsProvider,
{
fn contains(&self, codelist: &str, value: &str) -> bool {
T::contains(self, codelist, value)
}
}

pub trait CodeListValue {
const CODELIST: &str;

Expand Down
2 changes: 1 addition & 1 deletion crates/derive/src/xwasser_validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ fn trait_xwasser_validate(ast: &DeriveInput, content: TokenStream) -> TokenStrea
#[automatically_derived]
#[cfg(feature = "validate")]
impl <#(, #type_params)*> crate::XWasserValidate for #name #ty_generics #where_clause {
fn xwasser_validate(&self, codelists: &crate::CodeLists) -> Result<(), crate::XWasserValidateError> {
fn xwasser_validate(&self, codelists: &impl crate::CodeListsProvider) -> Result<(), crate::XWasserValidateError> {
use crate::XWasserValidate as _;
#content
}
Expand Down
52 changes: 32 additions & 20 deletions src/validate.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use xoev_xwasser_codelists::{CodeListValue, CodeLists};
use xoev_xwasser_codelists::{CodeListValue, CodeListsProvider};

pub trait XWasserValidate {
fn xwasser_validate(&self, codelists: &CodeLists) -> Result<(), XWasserValidateError>;
fn xwasser_validate(
&self,
codelists: &impl CodeListsProvider,
) -> Result<(), XWasserValidateError>;
}

pub enum XWasserValidateError {
Expand All @@ -14,7 +17,10 @@ impl<T> XWasserValidate for T
where
T: CodeListValue + XWasserValidateMarker,
{
fn xwasser_validate(&self, codelists: &CodeLists) -> Result<(), XWasserValidateError> {
fn xwasser_validate(
&self,
codelists: &impl CodeListsProvider,
) -> Result<(), XWasserValidateError> {
if self.validate(codelists) {
Ok(())
} else {
Expand All @@ -30,7 +36,10 @@ impl<T> XWasserValidate for Option<T>
where
T: XWasserValidate,
{
fn xwasser_validate(&self, codelists: &CodeLists) -> Result<(), XWasserValidateError> {
fn xwasser_validate(
&self,
codelists: &impl CodeListsProvider,
) -> Result<(), XWasserValidateError> {
self.as_ref()
.map(|t| t.xwasser_validate(codelists))
.unwrap_or(Ok(()))
Expand All @@ -41,7 +50,10 @@ impl<T> XWasserValidate for Vec<T>
where
T: XWasserValidate,
{
fn xwasser_validate(&self, codelists: &CodeLists) -> Result<(), XWasserValidateError> {
fn xwasser_validate(
&self,
codelists: &impl CodeListsProvider,
) -> Result<(), XWasserValidateError> {
self.iter()
.find_map(|t| t.xwasser_validate(codelists).err())
.map(Err)
Expand All @@ -50,91 +62,91 @@ where
}

impl XWasserValidate for raxb::value::ConstStr {
fn xwasser_validate(&self, _: &CodeLists) -> Result<(), XWasserValidateError> {
fn xwasser_validate(&self, _: &impl CodeListsProvider) -> Result<(), XWasserValidateError> {
Ok(())
}
}

impl XWasserValidate for String {
fn xwasser_validate(&self, _: &CodeLists) -> Result<(), XWasserValidateError> {
fn xwasser_validate(&self, _: &impl CodeListsProvider) -> Result<(), XWasserValidateError> {
Ok(())
}
}

impl XWasserValidate for bool {
fn xwasser_validate(&self, _: &CodeLists) -> Result<(), XWasserValidateError> {
fn xwasser_validate(&self, _: &impl CodeListsProvider) -> Result<(), XWasserValidateError> {
Ok(())
}
}

impl XWasserValidate for isize {
fn xwasser_validate(&self, _: &CodeLists) -> Result<(), XWasserValidateError> {
fn xwasser_validate(&self, _: &impl CodeListsProvider) -> Result<(), XWasserValidateError> {
Ok(())
}
}

impl XWasserValidate for u64 {
fn xwasser_validate(&self, _: &CodeLists) -> Result<(), XWasserValidateError> {
fn xwasser_validate(&self, _: &impl CodeListsProvider) -> Result<(), XWasserValidateError> {
Ok(())
}
}

impl XWasserValidate for u32 {
fn xwasser_validate(&self, _: &CodeLists) -> Result<(), XWasserValidateError> {
fn xwasser_validate(&self, _: &impl CodeListsProvider) -> Result<(), XWasserValidateError> {
Ok(())
}
}

impl XWasserValidate for u16 {
fn xwasser_validate(&self, _: &CodeLists) -> Result<(), XWasserValidateError> {
fn xwasser_validate(&self, _: &impl CodeListsProvider) -> Result<(), XWasserValidateError> {
Ok(())
}
}

impl XWasserValidate for u8 {
fn xwasser_validate(&self, _: &CodeLists) -> Result<(), XWasserValidateError> {
fn xwasser_validate(&self, _: &impl CodeListsProvider) -> Result<(), XWasserValidateError> {
Ok(())
}
}

impl XWasserValidate for i64 {
fn xwasser_validate(&self, _: &CodeLists) -> Result<(), XWasserValidateError> {
fn xwasser_validate(&self, _: &impl CodeListsProvider) -> Result<(), XWasserValidateError> {
Ok(())
}
}

impl XWasserValidate for i32 {
fn xwasser_validate(&self, _: &CodeLists) -> Result<(), XWasserValidateError> {
fn xwasser_validate(&self, _: &impl CodeListsProvider) -> Result<(), XWasserValidateError> {
Ok(())
}
}

impl XWasserValidate for i16 {
fn xwasser_validate(&self, _: &CodeLists) -> Result<(), XWasserValidateError> {
fn xwasser_validate(&self, _: &impl CodeListsProvider) -> Result<(), XWasserValidateError> {
Ok(())
}
}

impl XWasserValidate for i8 {
fn xwasser_validate(&self, _: &CodeLists) -> Result<(), XWasserValidateError> {
fn xwasser_validate(&self, _: &impl CodeListsProvider) -> Result<(), XWasserValidateError> {
Ok(())
}
}

impl XWasserValidate for usize {
fn xwasser_validate(&self, _: &CodeLists) -> Result<(), XWasserValidateError> {
fn xwasser_validate(&self, _: &impl CodeListsProvider) -> Result<(), XWasserValidateError> {
Ok(())
}
}

impl XWasserValidate for f64 {
fn xwasser_validate(&self, _: &CodeLists) -> Result<(), XWasserValidateError> {
fn xwasser_validate(&self, _: &impl CodeListsProvider) -> Result<(), XWasserValidateError> {
Ok(())
}
}

impl XWasserValidate for () {
fn xwasser_validate(&self, _: &CodeLists) -> Result<(), XWasserValidateError> {
fn xwasser_validate(&self, _: &impl CodeListsProvider) -> Result<(), XWasserValidateError> {
Ok(())
}
}

0 comments on commit b6a9980

Please sign in to comment.