-
Notifications
You must be signed in to change notification settings - Fork 30
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 #79 from jprochazk/newtype
Newtype
- Loading branch information
Showing
21 changed files
with
289 additions
and
32 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
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,4 +1,5 @@ | ||
use super::util; | ||
|
||
#[derive(Debug, garde::Validate)] | ||
struct Test<'a> { | ||
#[garde(ascii)] | ||
|
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,4 +1,5 @@ | ||
use super::util; | ||
|
||
#[derive(Debug, garde::Validate)] | ||
struct Test<'a> { | ||
#[garde(credit_card)] | ||
|
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,4 +1,5 @@ | ||
use super::util; | ||
|
||
struct Context { | ||
needle: String, | ||
} | ||
|
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,4 +1,5 @@ | ||
use super::util; | ||
|
||
#[derive(Debug, garde::Validate)] | ||
struct Test<'a> { | ||
#[garde(email)] | ||
|
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,4 +1,5 @@ | ||
use super::util; | ||
|
||
#[derive(Debug, garde::Validate)] | ||
struct TestIpAny<'a> { | ||
#[garde(ip)] | ||
|
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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#![allow(non_camel_case_types)] | ||
|
||
use super::util; | ||
|
||
#[derive(Debug, garde::Validate)] | ||
#[garde(transparent)] | ||
struct NonEmptyStr_Struct<'a> { | ||
#[garde(length(min = 1))] | ||
v: &'a str, | ||
} | ||
|
||
#[derive(Debug, garde::Validate)] | ||
#[garde(transparent)] | ||
struct NonEmptyStr_Tuple<'a>(#[garde(length(min = 1))] &'a str); | ||
|
||
#[derive(Debug, garde::Validate)] | ||
|
||
struct Test<'a> { | ||
#[garde(dive)] | ||
a: NonEmptyStr_Struct<'a>, | ||
#[garde(dive)] | ||
b: NonEmptyStr_Tuple<'a>, | ||
} | ||
|
||
#[test] | ||
fn newtype_valid() { | ||
util::check_ok( | ||
&[Test { | ||
a: NonEmptyStr_Struct { v: "test" }, | ||
b: NonEmptyStr_Tuple("test"), | ||
}], | ||
&(), | ||
); | ||
} | ||
|
||
#[test] | ||
fn newtype_invalid() { | ||
util::check_fail!( | ||
&[Test { | ||
a: NonEmptyStr_Struct { v: "" }, | ||
b: NonEmptyStr_Tuple(""), | ||
}], | ||
&() | ||
); | ||
} |
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,4 +1,5 @@ | ||
use super::util; | ||
|
||
#[derive(Debug, garde::Validate)] | ||
struct Test<'a> { | ||
#[garde(phone_number)] | ||
|
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,4 +1,5 @@ | ||
use super::util; | ||
|
||
#[derive(Debug, garde::Validate)] | ||
struct Test<'a> { | ||
#[garde(range(min = 10, max = 100))] | ||
|
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,4 +1,5 @@ | ||
use super::util; | ||
|
||
#[allow(dead_code)] | ||
#[derive(Debug, garde::Validate)] | ||
struct Struct { | ||
|
16 changes: 16 additions & 0 deletions
16
garde/tests/rules/snapshots/rules__rules__newtype__newtype_invalid.snap
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,16 @@ | ||
--- | ||
source: garde/tests/./rules/newtype.rs | ||
expression: snapshot | ||
--- | ||
Test { | ||
a: NonEmptyStr_Struct { | ||
v: "", | ||
}, | ||
b: NonEmptyStr_Tuple( | ||
"", | ||
), | ||
} | ||
a: length is lower than 1 | ||
b: length is lower than 1 | ||
|
||
|
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,24 @@ | ||
#![allow(dead_code)] | ||
|
||
#[derive(Debug, garde::Validate)] | ||
#[garde(transparent)] | ||
struct EmptyTuple(); | ||
|
||
#[derive(Debug, garde::Validate)] | ||
#[garde(transparent)] | ||
struct EmptyStruct {} | ||
|
||
#[derive(Debug, garde::Validate)] | ||
#[garde(transparent)] | ||
struct NonUnaryTuple<'a>(#[garde(ascii)] &'a str, #[garde(ascii)] &'a str); | ||
|
||
#[derive(Debug, garde::Validate)] | ||
#[garde(transparent)] | ||
struct NonUnaryStruct<'a> { | ||
#[garde(ascii)] | ||
a: &'a str, | ||
#[garde(ascii)] | ||
b: &'a str, | ||
} | ||
|
||
fn main() {} |
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,23 @@ | ||
error: transparent structs must have exactly one field | ||
--> tests/ui/compile-fail/non_unary_newtype.rs | ||
| | ||
| #[garde(transparent)] | ||
| ^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: transparent structs must have exactly one field | ||
--> tests/ui/compile-fail/non_unary_newtype.rs | ||
| | ||
| #[garde(transparent)] | ||
| ^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: transparent structs must have exactly one field | ||
--> tests/ui/compile-fail/non_unary_newtype.rs | ||
| | ||
| #[garde(transparent)] | ||
| ^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: transparent structs must have exactly one field | ||
--> tests/ui/compile-fail/non_unary_newtype.rs | ||
| | ||
| #[garde(transparent)] | ||
| ^^^^^^^^^^^^^^^^^^^^^ |
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,27 @@ | ||
#![allow(dead_code)] | ||
|
||
#[derive(Debug, garde::Validate)] | ||
#[garde(transparent)] | ||
struct UnaryTuple<'a>(#[garde(ascii)] &'a str); | ||
|
||
#[derive(Debug, garde::Validate)] | ||
#[garde(transparent)] | ||
struct UnaryStruct<'a> { | ||
#[garde(ascii)] | ||
a: &'a str, | ||
} | ||
|
||
#[derive(Debug, garde::Validate)] | ||
#[garde(transparent)] | ||
struct TupleWithSkippedField<'a>(#[garde(ascii)] &'a str, #[garde(skip)] &'a str); | ||
|
||
#[derive(Debug, garde::Validate)] | ||
#[garde(transparent)] | ||
struct StructWithSkippedField<'a> { | ||
#[garde(ascii)] | ||
a: &'a str, | ||
#[garde(skip)] | ||
b: &'a str, | ||
} | ||
|
||
fn main() {} |
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
Oops, something went wrong.