Debian Codegraph @

third_party/rust/thiserror/tests/test_path.rs

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