Skip to content

Commit

Permalink
use std::convert::TryFrom instead of a custom function
Browse files Browse the repository at this point in the history
  • Loading branch information
mirosval committed Feb 28, 2023
1 parent d74c968 commit d8df964
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 23 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ fn is_positive(i: i32) -> Result<i32, String> {
}
}

// 3. Derive (1) the `unvalidated` type and a `from_unvalidated` function
// 3. Derive (1) the `unvalidated` type and a `std::convert::TryFrom` trait
#[derive(Validated)]
// 2. And a struct
struct A {
a: i32,
}
let i: i32 = 1;
// 4. Construct the instance of the original type from the unvalidated version
let a = A::from_unvalidated(UnvalidatedA { a: i }).expect("valid instance");
let a = A::try_from(UnvalidatedA { a: i }).expect("valid instance");
assert_eq!(a.a, i);
```

Expand Down
10 changes: 5 additions & 5 deletions tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ fn test_no_validated_fields() {
a: i32,
}
let i: i32 = 1;
let a = A::from_unvalidated(UnvalidatedA { a: i }).expect("valid instance");
let a = A::try_from(UnvalidatedA { a: i }).expect("valid instance");
assert_eq!(a.a, i);
}

Expand All @@ -47,7 +47,7 @@ fn test_one_validated_field() {
a: i32,
}
let a: i32 = 1;
let instance = A::from_unvalidated(UnvalidatedA { a }).expect("valid instance");
let instance = A::try_from(UnvalidatedA { a }).expect("valid instance");
assert_eq!(instance.a, a);
}

Expand All @@ -62,7 +62,7 @@ fn test_one_validated_one_not_validated_field() {
}
let a: i32 = 1;
let b: u8 = 2;
let instance = A::from_unvalidated(UnvalidatedA { a, _b: b }).expect("valid instance");
let instance = A::try_from(UnvalidatedA { a, _b: b }).expect("valid instance");
assert_eq!(instance.a, a);
assert_eq!(instance._b, b);
}
Expand All @@ -75,7 +75,7 @@ fn test_lifetime() {
a: &'a str,
}
let a: &str = "aaa";
let instance = A::from_unvalidated(UnvalidatedA { a }).expect("valid instance");
let instance = A::try_from(UnvalidatedA { a }).expect("valid instance");
assert_eq!(instance.a, a);
}

Expand All @@ -89,7 +89,7 @@ fn test_generics() {
}
let a: i32 = 1;
let b: u8 = 2;
let instance = A::from_unvalidated(UnvalidatedA { a, _b: b }).expect("valid instance");
let instance = A::try_from(UnvalidatedA { a, _b: b }).expect("valid instance");
assert_eq!(instance.a, a);
assert_eq!(instance._b, b);
}
2 changes: 1 addition & 1 deletion tests/ui/wrong_validator_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ struct A {
fn main() {
let a: i32 = 1;
// This should fail, because validator error type does not match the defined custom error type
let instance = A::from_unvalidated(UnvalidatedA { a }).expect("valid instance");
let instance = A::try_from(UnvalidatedA { a }).expect("valid instance");
assert_eq!(instance.a, a);
}
21 changes: 9 additions & 12 deletions valibuk_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,9 @@ mod tests {
struct UnvalidatedA {
pub a: i32,
}
impl A {
pub fn from_unvalidated(
unvalidated: UnvalidatedA,
) -> ::core::result::Result<A, Vec<::std::string::String>> {
impl ::std::convert::TryFrom<UnvalidatedA> for A {
type Error = ::std::vec::Vec<::std::string::String>;
fn try_from(unvalidated: UnvalidatedA) -> ::core::result::Result<Self, Self::Error> {
Ok(A { a: unvalidated.a })
}
}
Expand All @@ -99,10 +98,9 @@ mod tests {
struct UnvalidatedA {
pub a: i32,
}
impl A {
pub fn from_unvalidated(
unvalidated: UnvalidatedA,
) -> ::core::result::Result<A, Vec<E>> {
impl ::std::convert::TryFrom<UnvalidatedA> for A {
type Error = ::std::vec::Vec<E>;
fn try_from(unvalidated: UnvalidatedA) -> ::core::result::Result<Self, Self::Error> {
Ok(A { a: unvalidated.a })
}
}
Expand All @@ -122,10 +120,9 @@ mod tests {
struct UnvalidatedA<'a> {
pub a: &'a str
}
impl<'a> A<'a> {
pub fn from_unvalidated(
unvalidated: UnvalidatedA<'a>,
) -> ::core::result::Result<A<'a>, Vec<::std::string::String>> {
impl<'a> ::std::convert::TryFrom<UnvalidatedA<'a>> for A<'a> {
type Error = ::std::vec::Vec<::std::string::String>;
fn try_from(unvalidated: UnvalidatedA<'a>) -> ::core::result::Result<Self, Self::Error> {
Ok(A { a: unvalidated.a })
}
}
Expand Down
8 changes: 5 additions & 3 deletions valibuk_core/src/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,12 @@ impl<'a> ValidatedDeriv<'a> {
}
};
Ok(quote! {
impl #impl_generics #name #ty_generics {
pub fn from_unvalidated(
impl #impl_generics ::std::convert::TryFrom<#unvalidated_name #ty_generics> for #name #ty_generics {
type Error = ::std::vec::Vec<#ety>;

fn try_from(
unvalidated: #unvalidated_name #ty_generics
) -> ::core::result::Result<#name #ty_generics, Vec<#ety>> {
) -> ::core::result::Result<Self, Self::Error> {
#body
}
}
Expand Down

0 comments on commit d8df964

Please sign in to comment.