-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
53 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
name: Cargo Build & Test | ||
|
||
on: | ||
push: | ||
pull_request: | ||
|
||
env: | ||
CARGO_TERM_COLOR: always | ||
|
||
jobs: | ||
build_and_test: | ||
name: valibuk - latest | ||
runs-on: ubuntu-latest | ||
strategy: | ||
matrix: | ||
toolchain: | ||
- stable | ||
- beta | ||
- nightly | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- run: rustup update ${{ matrix.toolchain }} && rustup default ${{ matrix.toolchain }} | ||
- run: cargo build --verbose | ||
- run: cargo test --verbose | ||
|
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,25 @@ | ||
use valibuk::Validated; | ||
|
||
// 1. Having a T -> Result<T, E> validator | ||
fn is_positive(i: i32) -> Result<i32, String> { | ||
if i > 0 { | ||
Ok(i) | ||
} else { | ||
Err("wrong".to_string()) | ||
} | ||
} | ||
|
||
// 3. Derive (1) the `unvalidated` type and a `std::convert::TryFrom` trait | ||
#[derive(Validated)] | ||
// 2. And a struct | ||
struct A { | ||
#[validator(is_positive)] // Apply the function from (1) as validator | ||
a: i32, | ||
} | ||
|
||
fn main() { | ||
let i: i32 = 1; | ||
// 4. Construct the instance of the original type from the unvalidated version | ||
let a = A::try_from(UnvalidatedA { a: i }).expect("valid instance"); | ||
assert_eq!(a.a, i); | ||
} |