Skip to content

Commit

Permalink
Merge pull request #13 from tailcallhq/chore/add-readme
Browse files Browse the repository at this point in the history
  • Loading branch information
ssddOnTop authored Oct 17, 2024
2 parents 2c4d3e4 + 6138967 commit 9ee5f9a
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# tailcall-valid

This crate helps to collect all possible errors instead of returning the first error encountered. This is useful when you want to know all the errors in a single go.

`Valid` could be initiated with an `Option`, `Result`, success, or a failure.

## Examples

### From success

```rust
use tailcall_valid::*;

fn main() {
let result: Valid<i32, &str> = Valid::succeed(1);
assert_eq!(result, Valid::succeed(1));
}
```

### From failure

```rust
use tailcall_valid::*;

fn main() {
let err = "Expected a value";
let result: Valid<i32, &str> = Valid::fail(err);
assert_eq!(result, Valid::from_vec_cause(vec![Cause::new(err)]));
}
```

### From `Option`

```rust
use tailcall_valid::*;

fn main() {
// Case when Option is None
let err = "Expected a value";
let option: Option<i32> = None;
let result = Valid::from_option(option, err);
assert_eq!(result, Valid::from_vec_cause(vec![Cause::new(err)]));

// Case when Option is Some
let option: Option<i32> = Some(1);
let result = Valid::from_option(option, err);
assert_eq!(result, Valid::succeed(1));
}
```

### From `Result`

```rust
use tailcall_valid::*;

fn main() {
// Case when Result is Err
let err = "Expected a value";
let result: Result<i32, &str> = Err(err);
let result = result.map_err(ValidationError::new);
let result = Valid::from(result);
assert_eq!(result, Valid::from_vec_cause(vec![Cause::new(err)]));

// Case when Result is Ok
let result: Result<i32, &str> = Ok(1);
let result = result.map_err(ValidationError::new);
let result = Valid::from(result);
assert_eq!(result, Valid::succeed(1));
}
```

0 comments on commit 9ee5f9a

Please sign in to comment.