diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml new file mode 100644 index 0000000..e25c924 --- /dev/null +++ b/.github/workflows/ci.yaml @@ -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 + diff --git a/README.md b/README.md index 7fccebb..b45623d 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,7 @@ fn is_positive(i: i32) -> Result { #[derive(Validated)] // 2. And a struct struct A { + #[validator(is_positive)] // Apply the function from (1) as validator a: i32, } let i: i32 = 1; @@ -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 diff --git a/examples/minimal.rs b/examples/minimal.rs new file mode 100644 index 0000000..5a6285f --- /dev/null +++ b/examples/minimal.rs @@ -0,0 +1,25 @@ +use valibuk::Validated; + +// 1. Having a T -> Result validator +fn is_positive(i: i32) -> Result { + 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); +}