1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
|
use snafu::prelude::*;
mod enabling {
use super::*;
#[test]
fn no_argument_treated_as_source() {
#[derive(Debug, Snafu)]
struct Error {
#[snafu(source)]
cause: InnerError,
}
let _ = inner().context(Snafu);
}
#[test]
fn true_argument_treated_as_source() {
#[derive(Debug, Snafu)]
struct Error {
#[snafu(source(true))]
cause: InnerError,
}
let _ = inner().context(Snafu);
}
#[test]
fn from_argument_treated_as_source() {
#[derive(Debug, Snafu)]
struct Error {
#[snafu(source(from(InnerError, Box::new)))]
cause: Box<InnerError>,
}
let _ = inner().context(Snafu);
}
#[test]
fn false_argument_not_treated_as_source() {
#[derive(Debug, Snafu)]
struct Error {
#[snafu(source(false))]
source: i32,
}
let _ = Snafu { source: 42 }.build();
}
}
#[cfg(feature="std")]
mod transformation {
use super::*;
use std::{error::Error as StdError, io};
#[test]
fn transformation_via_closure() {
#[derive(Debug, Snafu)]
struct Error {
#[snafu(source(from(InnerError, |e| io::Error::new(io::ErrorKind::InvalidData, e))))]
source: io::Error,
}
let _ = inner().context(Snafu);
}
#[test]
fn transformation_via_function() {
fn into_io(e: InnerError) -> io::Error {
io::Error::new(io::ErrorKind::InvalidData, e)
}
#[derive(Debug, Snafu)]
struct Error {
#[snafu(source(from(InnerError, into_io)))]
source: io::Error,
}
let _ = inner().context(Snafu);
}
#[test]
fn transformation_to_trait_object() {
#[derive(Debug, Snafu)]
struct Error {
#[snafu(source(from(InnerError, Box::new)))]
source: Box<dyn StdError>,
}
let _ = inner().context(Snafu);
}
}
#[derive(Debug, Snafu)]
struct InnerError;
fn inner() -> Result<(), InnerError> {
Ok(())
}
|