Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setup very first example with CI #97

Merged
merged 4 commits into from
Nov 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,22 @@ jobs:
with:
command: clippy
args: -- -D warnings

examples:
name: Examples
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2

- name: Install Rust
uses: actions-rs/toolchain@v1
with:
toolchain: stable
components: clippy

- name: Run examples
run: |
for example in $(ls ./examples/); do
cargo run --bin $example
done
40 changes: 34 additions & 6 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ members = [
"nutype_macros",
"test_suite",
"dummy",
"examples/*",
]
8 changes: 7 additions & 1 deletion Justfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
all: test-all clippy
all: fmt test-all clippy examples

test-all:
cargo test --features nutype_test
Expand All @@ -14,6 +14,9 @@ test:
test-ui:
cargo test --features nutype_test,ui

fmt:
cargo fmt

watch:
cargo watch -x test

Expand All @@ -22,3 +25,6 @@ watch-dummy:

clippy:
cargo clippy -- -D warnings

examples:
for example in `ls examples`; do cargo run --bin $example; done
8 changes: 8 additions & 0 deletions examples/float_sortable/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "float_sortable"
version = "0.1.0"
edition = "2021"
publish = false

[dependencies]
nutype = { path = "../../nutype" }
36 changes: 36 additions & 0 deletions examples/float_sortable/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// This example shows how an f64 newtype can derive `Ord` and be sortable
// if it defines `finite` validation.

use nutype::nutype;

#[nutype(derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord), validate(finite))]
pub struct Width(f64);

fn main() {
let raw_widths = vec![1.5, 1.4, 2.1, 1.8];

// NOTE: sorting raw_widths is not possible, because f64 does not implement Ord.
// raw_widths.sort();

// So instead we can wrap f64 into Width, which implements Ord.
// Ord is possible to safely derived, because there is `finite` validation in place,
// which excluded NaN values.
let mut widths: Vec<Width> = raw_widths
.into_iter()
.map(|w| Width::new(w).unwrap())
.collect();

// Now we can sort
widths.sort();

// Verify
assert_eq!(
widths,
vec![
Width::new(1.4).unwrap(),
Width::new(1.5).unwrap(),
Width::new(1.8).unwrap(),
Width::new(2.1).unwrap(),
],
)
}
11 changes: 11 additions & 0 deletions examples/string_regex_email/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "string_regex_email"
version = "0.1.0"
edition = "2021"

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

[dependencies]
nutype = { path = "../../nutype", features = ["regex"] }
regex = "1.10.2"
lazy_static = "1.4.0"
42 changes: 42 additions & 0 deletions examples/string_regex_email/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use lazy_static::lazy_static;
use nutype::nutype;
use regex::Regex;

lazy_static! {
// Note: this regex is very simplified.
// In reality you'd like to use a more sophisticated regex for email validation.
static ref EMAIL_REGEX: Regex = Regex::new("^\\w+@\\w+\\.\\w+$").unwrap();
}

// Note: technically the local part of email address is case-sensitive, but in practice all the
// popular email services (e.g. gmail) make it case-insensitive, so applying `lowercase` is OK.
#[nutype(
sanitize(trim, lowercase),
validate(
char_len_min = 5,
char_len_max = 20,
regex = EMAIL_REGEX,
),
derive(Debug, PartialEq, AsRef),
)]
struct Email(String);

fn main() {
// Too short
assert_eq!(Email::new("a@b"), Err(EmailError::CharLenMinViolated));

// Too long
assert_eq!(
Email::new("[email protected]"),
Err(EmailError::CharLenMaxViolated)
);

// Does not match the regex
assert_eq!(Email::new("foo@barcom"), Err(EmailError::RegexViolated));

// A valid email
let email: Email = Email::new("\t [email protected] \n").unwrap();

// The underlying string that represents the email address is sanitized
assert_eq!(email.as_ref(), "[email protected]");
}
1 change: 1 addition & 0 deletions nutype_macros/src/any/gen/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ fn gen_definition(error_type_name: &ErrorTypeName, validators: &[AnyValidator])
.collect();

quote! {
#[allow(clippy::enum_variant_names)]
pub enum #error_type_name {
#error_variants
}
Expand Down
1 change: 1 addition & 0 deletions nutype_macros/src/float/gen/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ fn gen_definition<T>(
.collect();

quote! {
#[allow(clippy::enum_variant_names)]
pub enum #error_type_name {
#error_variants
}
Expand Down
1 change: 1 addition & 0 deletions nutype_macros/src/integer/gen/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ fn gen_definition<T>(
.collect();

quote! {
#[allow(clippy::enum_variant_names)]
pub enum #error_type_name {
#error_variants
}
Expand Down
1 change: 1 addition & 0 deletions nutype_macros/src/string/gen/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ fn gen_definition(error_type_name: &ErrorTypeName, validators: &[StringValidator
.collect();

quote! {
#[allow(clippy::enum_variant_names)]
pub enum #error_type_name {
#error_variants
}
Expand Down
Loading