Skip to content

Commit

Permalink
Merge pull request #99 from greyblake/example-integer
Browse files Browse the repository at this point in the history
Add example integer_bounded
  • Loading branch information
greyblake authored Nov 19, 2023
2 parents d827c15 + 61b82d3 commit cf12bf6
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions examples/float_sortable/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ name = "float_sortable"
version = "0.1.0"
edition = "2021"
publish = false
authors = ["Serhii Potapov <[email protected]>"]

[dependencies]
nutype = { path = "../../nutype" }
11 changes: 11 additions & 0 deletions examples/integer_bounded/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "integer_bounded"
version = "0.1.0"
edition = "2021"
publish = false
authors = ["Serhii Potapov <[email protected]>"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
nutype = { path = "../../nutype" }
23 changes: 23 additions & 0 deletions examples/integer_bounded/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use nutype::nutype;

#[nutype(
validate(
greater_or_equal = -100,
less_or_equal = 100,
),
derive(Debug, PartialEq)
)]
struct Volume(i8);

fn main() {
// Too small
assert_eq!(Volume::new(-101), Err(VolumeError::GreaterOrEqualViolated),);

// Too big
assert_eq!(Volume::new(101), Err(VolumeError::LessOrEqualViolated),);

// Valid values
assert_eq!(Volume::new(-100).unwrap().into_inner(), -100);
assert_eq!(Volume::new(0).unwrap().into_inner(), 0);
assert_eq!(Volume::new(100).unwrap().into_inner(), 100);
}
2 changes: 2 additions & 0 deletions examples/string_regex_email/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
name = "string_regex_email"
version = "0.1.0"
edition = "2021"
publish = false
authors = ["Serhii Potapov <[email protected]>"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand Down

0 comments on commit cf12bf6

Please sign in to comment.