Skip to content

Commit

Permalink
update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshMcguigan committed Feb 9, 2019
1 parent e697125 commit 216e9b0
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
49 changes: 48 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,56 @@
# exit

This crate exposes a type, `Exit`, which allows using `?` in `main` while also specifying custom exit status codes. See the examples for details.
This crate exposes a type, `Exit`, which allows using `?` in `main` while also specifying custom exit status codes.

The goal of this crate was to provide a proof of concept and sample implementation for the ideas discussed in [this blog post](https://www.joshmcguigan.com/blog/custom-exit-status-codes-rust/).

## Example

```rust
#![feature(try_trait)]
use exit::Exit;

use std::env;
use std::option;

#[derive(Debug)]
enum MyErr {
MissingArg,
ParseErrorUserNum,
ParseErrorGroupNum,
}

impl From<MyErr> for i32 {
fn from(err: MyErr) -> Self {
match err {
MyErr::MissingArg => 2,
MyErr::ParseErrorUserNum => 3,
MyErr::ParseErrorGroupNum => 4,
}
}
}

impl From<option::NoneError> for MyErr {
fn from(_: option::NoneError) -> Self {
MyErr::MissingArg
}
}

fn main() -> Exit<MyErr> {
let user_num_string : String = env::args().skip(1).next()?;
let group_num_string : String = env::args().skip(2).next()?;

let user_num : u32 = user_num_string.parse()
.map_err(|_| MyErr::ParseErrorUserNum)?;
let group_num : u32 = group_num_string.parse()
.map_err(|_| MyErr::ParseErrorGroupNum)?;

println!("Hello, user #{} from group #{}!", user_num, group_num);

Exit::Ok
}
```

## License

Licensed under either of
Expand Down
2 changes: 1 addition & 1 deletion examples/map_err.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use exit::Exit;

use std::env;
use core::option;
use std::option;

#[derive(Debug)]
enum MyErr {
Expand Down

0 comments on commit 216e9b0

Please sign in to comment.