-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #168 from greyblake/custom-error
Add Validation::Custom variant
- Loading branch information
Showing
37 changed files
with
768 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,35 @@ | ||
/* | ||
use nutype::nutype; | ||
|
||
// Validation function | ||
fn validate_name(name: &str) -> Result<(), NameError> { | ||
if name.len() < 3 { | ||
Err(NameError::TooShort) | ||
} else if name.len() > 20 { | ||
} else if name.len() > 10 { | ||
Err(NameError::TooLong) | ||
} else { | ||
Ok(()) | ||
} | ||
} | ||
|
||
// Name validation error | ||
#[derive(Debug)] | ||
#[derive(Debug, PartialEq)] | ||
enum NameError { | ||
TooShort, | ||
TooLong, | ||
} | ||
|
||
// Variant 1: with and error | ||
#[nutype( | ||
sanitize(trim), | ||
validate(with = validate_name, error = NameError), | ||
derive(Debug, AsRef, PartialEq, Deref), | ||
derive(Debug, AsRef, PartialEq), | ||
)] | ||
struct Name(String); | ||
*/ | ||
|
||
fn main() {} | ||
fn main() { | ||
let name = Name::try_new("John").unwrap(); | ||
assert_eq!(name.as_ref(), "John"); | ||
|
||
assert_eq!( | ||
Name::try_new("JohnJohnJohnJohnJohn"), | ||
Err(NameError::TooLong) | ||
); | ||
|
||
assert_eq!(Name::try_new("Jo"), Err(NameError::TooShort)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[package] | ||
name = "custom_error" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
nutype = { path = "../../nutype" } | ||
thiserror = "1.0.63" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use nutype::nutype; | ||
use thiserror::Error; | ||
|
||
#[nutype( | ||
validate(with = validate_positively_odd, error = PositivelyOddError), | ||
derive(Debug, FromStr), | ||
)] | ||
struct PositivelyOdd(i32); | ||
|
||
#[derive(Error, Debug, PartialEq)] | ||
enum PositivelyOddError { | ||
#[error("The value is negative.")] | ||
Negative, | ||
|
||
#[error("The value is even.")] | ||
Even, | ||
} | ||
|
||
fn validate_positively_odd(value: &i32) -> Result<(), PositivelyOddError> { | ||
if *value < 0 { | ||
return Err(PositivelyOddError::Negative); | ||
} | ||
|
||
if *value % 2 == 0 { | ||
return Err(PositivelyOddError::Even); | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
fn main() { | ||
let err = PositivelyOdd::try_new(2).unwrap_err(); | ||
assert_eq!(err, PositivelyOddError::Even); | ||
|
||
let podd: PositivelyOdd = PositivelyOdd::try_new(3).unwrap(); | ||
assert_eq!(podd.into_inner(), 3); | ||
|
||
let err: PositivelyOddParseError = "-3".parse::<PositivelyOdd>().unwrap_err(); | ||
assert!(matches!( | ||
err, | ||
PositivelyOddParseError::Validate(PositivelyOddError::Negative) | ||
)); | ||
} |
Oops, something went wrong.