Skip to content

Commit

Permalink
chore: prepare to publish
Browse files Browse the repository at this point in the history
  • Loading branch information
loichyan committed Jun 18, 2021
1 parent 9c6f050 commit 081cde5
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 33 deletions.
10 changes: 9 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,19 @@ authors = ["loichyan <loichyan@foxmail.com>"]
edition = "2018"
license = "MIT"

description = "Easily create error with contexts"
readme = "README.md"
repository = "https://github.com/loichyan/thisctx"
documentation = "https://docs.rs/thisctx"

keywords = ["error"]
categories = ["rust-patterns"]

[workspace]
members = ["impl"]

[dependencies]
thisctx_impl = { path = "impl" }
thisctx_impl = { version="=0.1.0", path="impl" }

[dev-dependencies]
thiserror = "1.0"
40 changes: 10 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,54 +1,34 @@
# THISCTX

A simple crate work with [thiserror](https://crates.io/crates/thiserror) to create errors with contexts, inspired by [snafu](https://crates.io/crates/snafu);
A simple crate work with [thiserror](https://crates.io/crates/thiserror) to create errors with contexts, inspired by [snafu](https://crates.io/crates/snafu).

## Examples

```rust
use thisctx::thisctx;
use std::fs;
use std::path::{Path, PathBuf};
use thisctx::{thisctx, ResultExt};
use thiserror::Error;

thisctx! {
#[derive(Debug, Error)]
pub enum Error {
#[error("I/O failed '{}': {src}", .ctx.path.display())]
#[error("I/O failed '{}': {src}", .ctx.0.display())]
IoFaild {
#[source]
@source
src: std::io::Error,
@context
ctx:
#[derive(Debug)]
struct {
path: std::path::PathBuf,
},
struct (PathBuf),
},
#[error("invalid file '{}': {}", .ctx.path.display(), .ctx.disc)]
InvalidFile {
@context
ctx:
#[derive(Debug)]
struct {
disc: String,
path: std::path::PathBuf,
},
},
#[error("not UTF-8: {0}")]
NotUtf8 (
#[source]
@source
std::str::Utf8Error
),
#[error("invalid argument: '{}'", 0.0)]
InvalidArg (
@context
#[derive(Debug)]
struct (String)
),
#[error("I have no idea about this error")]
JustFailed,
}
}

fn load_config(path: &Path) -> Result<String, Error> {
fs::read_to_string(path).context(IoFaild(path))
}
```

## License
Expand Down
31 changes: 31 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,34 @@
//! A simple crate work with [thiserror](https://crates.io/crates/thiserror) to create errors with contexts, inspired by [snafu](https://crates.io/crates/snafu).
//!
//! # Examples
//!
//! ```rust
//! use std::fs;
//! use std::path::{Path, PathBuf};
//! use thisctx::{thisctx, ResultExt};
//! use thiserror::Error;
//!
//! thisctx! {
//! #[derive(Debug, Error)]
//! pub enum Error {
//! #[error("I/O failed '{}': {src}", .ctx.0.display())]
//! IoFaild {
//! #[source]
//! @source
//! src: std::io::Error,
//! @context
//! ctx:
//! #[derive(Debug)]
//! struct (PathBuf),
//! },
//! }
//! }
//!
//! fn load_config(path: &Path) -> Result<String, Error> {
//! fs::read_to_string(path).context(IoFaild(path))
//! }
//!```
mod ext;

pub use ext::{IntoError, NoneError, OptionExt, ResultExt};
Expand Down
4 changes: 2 additions & 2 deletions tests/use_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ thisctx! {
struct (String)
),
#[error("I have no idea about this error")]
JustFailed { },
JustFailed {},
#[error("it just failed either")]
FailedEither ( ),
FailedEither (),
}
}

0 comments on commit 081cde5

Please sign in to comment.