This commit is contained in:
2020-01-31 01:10:23 +08:00
parent 8f311fc137
commit 925f224357
2 changed files with 0 additions and 0 deletions

10
err_derive/Cargo.toml Normal file
View File

@@ -0,0 +1,10 @@
[package]
name = "err-derive-test"
version = "0.1.0"
authors = ["Hatter Jiang <jht5945@gmail.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
err-derive = "0.2.2"

37
err_derive/src/main.rs Normal file
View File

@@ -0,0 +1,37 @@
use err_derive::Error;
#[derive(Debug, Error)]
pub enum TestError {
#[error(display = "invalid attribute (expected: {:?}, got: {:?})", expected, found)]
InvalidAttribute {
expected: String,
found: String,
},
#[error(display = "missing attribute: {:?}", _0)]
MissingAttribute(String),
}
fn main() {
println!("-----Error 0-----");
if let Err(e) = test_err_0() {
println!("{:?}", e);
println!("{}", e);
}
println!("-----Error 1-----");
if let Err(e) = test_err_1() {
println!("{:?}", e);
println!("{}", e);
}
}
fn test_err_0() -> Result<(), TestError> {
Err(TestError::MissingAttribute("test".into()))
}
fn test_err_1() -> Result<(), TestError> {
Err(TestError::InvalidAttribute {
expected: "aaa".into(),
found: "bbbb".into(),
})
}