Skip to content

Commit

Permalink
Refactored Validated Traits (#619)
Browse files Browse the repository at this point in the history
* refactored Validated traits

* removed U from new
  • Loading branch information
PoisonPhang authored Feb 14, 2022
1 parent 5fc27d3 commit bb51e1c
Showing 1 changed file with 36 additions and 5 deletions.
41 changes: 36 additions & 5 deletions frame/composable-support/src/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T, U> {
value: T,
_marker: PhantomData<U>,
}

impl<T, U> TypeInfo for Validated<T, U>
where
T: TypeInfo,
{
type Identity = <T as TypeInfo>::Identity;

fn type_info() -> scale_info::Type {
T::type_info()
}
}

impl<T, U> PartialEq for Validated<T, U>
where
T: PartialEq,
{
fn eq(&self, other: &Self) -> bool {
self.value == other.value
}
}

impl<T, U> Eq for Validated<T, U> where T: PartialEq + Eq {}

impl<T, U> fmt::Debug for Validated<T, U>
where
T: core::fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.value.fmt(f)
}
}

impl<T, U> Validated<T, U>
where
Validated<T, U>: Validate<T, U>,
U: Validate<T, U>,
{
pub fn new(value: T, _validator_tag: U) -> Result<Self, &'static str> {
pub fn new(value: T) -> Result<Self, &'static str> {
match <U as Validate<T, U>>::validate(value) {
Ok(value) => Ok(Self { value, _marker: PhantomData }),
Err(e) => Err(e),
Expand Down Expand Up @@ -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());
}

Expand Down

0 comments on commit bb51e1c

Please sign in to comment.