Skip to content

Commit

Permalink
add minimal example and ci
Browse files Browse the repository at this point in the history
  • Loading branch information
mirosval committed Feb 28, 2023
1 parent d8df964 commit c78d757
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/ci.yaml
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

3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ fn is_positive(i: i32) -> Result<i32, String> {
#[derive(Validated)]
// 2. And a struct
struct A {
#[validator(is_positive)] // Apply the function from (1) as validator
a: i32,
}
let i: i32 = 1;
Expand All @@ -28,6 +29,8 @@ let a = A::try_from(UnvalidatedA { a: i }).expect("valid instance");
assert_eq!(a.a, i);
```

See more examples in `tests` and `examples`

## TODO

- [x] Move validator registrations into macro annotations
Expand Down
25 changes: 25 additions & 0 deletions examples/minimal.rs
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);
}

0 comments on commit c78d757

Please sign in to comment.