| 1 |
#![cfg(feature = "std")] |
| 2 |
|
| 3 |
use core::fmt::Display; |
| 4 |
use ref_cast::RefCast; |
| 5 |
use std::path::{Path, PathBuf}; |
| 6 |
use thiserror::Error; |
| 7 |
|
| 8 |
#[derive(Error, Debug)] |
| 9 |
#[error("failed to read '{file}'")] |
| 10 |
struct StructPathBuf { |
| 11 |
file: PathBuf, |
| 12 |
} |
| 13 |
|
| 14 |
#[derive(Error, Debug, RefCast)] |
| 15 |
#[repr(C)] |
| 16 |
#[error("failed to read '{file}'")] |
| 17 |
struct StructPath { |
| 18 |
file: Path, |
| 19 |
} |
| 20 |
|
| 21 |
#[derive(Error, Debug)] |
| 22 |
enum EnumPathBuf { |
| 23 |
#[error("failed to read '{0}'")] |
| 24 |
Read(PathBuf), |
| 25 |
} |
| 26 |
|
| 27 |
#[derive(Error, Debug)] |
| 28 |
#[error("{tail}")] |
| 29 |
pub struct UnsizedError { |
| 30 |
pub head: i32, |
| 31 |
pub tail: str, |
| 32 |
} |
| 33 |
|
| 34 |
#[derive(Error, Debug)] |
| 35 |
pub enum BothError { |
| 36 |
#[error("display:{0} debug:{0:?}")] |
| 37 |
DisplayDebug(PathBuf), |
| 38 |
#[error("debug:{0:?} display:{0}")] |
| 39 |
DebugDisplay(PathBuf), |
| 40 |
} |
| 41 |
|
| 42 |
fn assert<T: Display>(expected: &str, value: T) { |
| 43 |
assert_eq!(expected, value.to_string()); |
| 44 |
} |
| 45 |
|
| 46 |
#[test] |
| 47 |
fn test_display() { |
| 48 |
let path = Path::new("/thiserror"); |
| 49 |
let file = path.to_owned(); |
| 50 |
assert("failed to read '/thiserror'", StructPathBuf { file }); |
| 51 |
let file = path.to_owned(); |
| 52 |
assert("failed to read '/thiserror'", EnumPathBuf::Read(file)); |
| 53 |
assert("failed to read '/thiserror'", StructPath::ref_cast(path)); |
| 54 |
} |