diff --git a/frame/composable-support/src/validation.rs b/frame/composable-support/src/validation.rs index 6f8340dc800..b8a9c54a72b 100644 --- a/frame/composable-support/src/validation.rs +++ b/frame/composable-support/src/validation.rs @@ -65,23 +65,54 @@ //! } //! ``` -use core::marker::PhantomData; +use core::{fmt, marker::PhantomData}; use scale_info::TypeInfo; use sp_runtime::DispatchError; /// Black box that embbed the validated value. -#[derive(Default, Copy, Clone, PartialEq, Eq, Debug, TypeInfo)] +#[derive(Default, Copy, Clone)] pub struct Validated { value: T, _marker: PhantomData, } +impl TypeInfo for Validated +where + T: TypeInfo, +{ + type Identity = ::Identity; + + fn type_info() -> scale_info::Type { + T::type_info() + } +} + +impl PartialEq for Validated +where + T: PartialEq, +{ + fn eq(&self, other: &Self) -> bool { + self.value == other.value + } +} + +impl Eq for Validated where T: PartialEq + Eq {} + +impl fmt::Debug for Validated +where + T: core::fmt::Debug, +{ + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.value.fmt(f) + } +} + impl Validated where Validated: Validate, U: Validate, { - pub fn new(value: T, _validator_tag: U) -> Result { + pub fn new(value: T) -> Result { match >::validate(value) { Ok(value) => Ok(Self { value, _marker: PhantomData }), Err(e) => Err(e), @@ -309,9 +340,9 @@ mod test { #[test] fn value() { - let value = Validated::new(42, Valid); + let value = Validated::<_, Valid>::new(42); assert_ok!(value); - let value = Validated::new(42, Invalid); + let value = Validated::<_, Invalid>::new(42); assert!(value.is_err()); }