add err derive test
This commit is contained in:
10
err-derive-test/Cargo.toml
Normal file
10
err-derive-test/Cargo.toml
Normal 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-test/src/main.rs
Normal file
37
err-derive-test/src/main.rs
Normal 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(),
|
||||||
|
})
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user