Skip to content

chore: add README.md #13

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

Merged
merged 3 commits into from
Oct 17, 2024
Merged
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
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));
}
```