From 61b82d304516981e5e8a2c5633d2cc59381c1b48 Mon Sep 17 00:00:00 2001 From: Serhii Potapov Date: Sat, 18 Nov 2023 18:38:54 +0100 Subject: [PATCH] Add example integer_bounded --- Cargo.lock | 7 +++++++ examples/float_sortable/Cargo.toml | 1 + examples/integer_bounded/Cargo.toml | 11 +++++++++++ examples/integer_bounded/src/main.rs | 23 +++++++++++++++++++++++ examples/string_regex_email/Cargo.toml | 2 ++ 5 files changed, 44 insertions(+) create mode 100644 examples/integer_bounded/Cargo.toml create mode 100644 examples/integer_bounded/src/main.rs diff --git a/Cargo.lock b/Cargo.lock index 45e35ca..37451b8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -116,6 +116,13 @@ version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b" +[[package]] +name = "integer_bounded" +version = "0.1.0" +dependencies = [ + "nutype", +] + [[package]] name = "itoa" version = "1.0.6" diff --git a/examples/float_sortable/Cargo.toml b/examples/float_sortable/Cargo.toml index 8b4b441..584b390 100644 --- a/examples/float_sortable/Cargo.toml +++ b/examples/float_sortable/Cargo.toml @@ -3,6 +3,7 @@ name = "float_sortable" version = "0.1.0" edition = "2021" publish = false +authors = ["Serhii Potapov "] [dependencies] nutype = { path = "../../nutype" } diff --git a/examples/integer_bounded/Cargo.toml b/examples/integer_bounded/Cargo.toml new file mode 100644 index 0000000..a1ea7ac --- /dev/null +++ b/examples/integer_bounded/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "integer_bounded" +version = "0.1.0" +edition = "2021" +publish = false +authors = ["Serhii Potapov "] + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +nutype = { path = "../../nutype" } diff --git a/examples/integer_bounded/src/main.rs b/examples/integer_bounded/src/main.rs new file mode 100644 index 0000000..2b123f3 --- /dev/null +++ b/examples/integer_bounded/src/main.rs @@ -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); +} diff --git a/examples/string_regex_email/Cargo.toml b/examples/string_regex_email/Cargo.toml index 1aae745..bf8f9aa 100644 --- a/examples/string_regex_email/Cargo.toml +++ b/examples/string_regex_email/Cargo.toml @@ -2,6 +2,8 @@ name = "string_regex_email" version = "0.1.0" edition = "2021" +publish = false +authors = ["Serhii Potapov "] # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html