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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
use std::backtrace::Backtrace;
use thiserror::Error;
use miette::Diagnostic;
use crate::usb::mode::Mode;


#[derive(Error, Debug, Diagnostic)]
pub enum Error {
	#[error(transparent)]
	#[diagnostic(code(my_lib::io_error))]
	Io {
		#[backtrace]
		#[from]
		source: std::io::Error,
	},

	#[error(transparent)]
	#[diagnostic()]
	Process {
		#[backtrace]
		#[from]
		source: std::process::ExitStatusError,
	},

	#[error(transparent)]
	#[diagnostic()]
	Transfer {
		#[backtrace]
		#[from]
		source: nusb::transfer::TransferError,
	},

	#[error(transparent)]
	#[diagnostic()]
	Borrow {
		#[backtrace]
		#[from]
		source: std::cell::BorrowMutError,
	},

	#[error("Awaiting device timeout `{device}`.")]
	#[diagnostic()]
	DeviceTimeout {
		#[backtrace]
		backtrace: Backtrace,
		device: crate::device::Device,
	},

	#[error("Awaiting {what} timeout.")]
	Timeout {
		#[backtrace]
		backtrace: Backtrace,
		what: String,
	},

	#[error(transparent)]
	#[diagnostic()]
	Utf {
		#[backtrace]
		#[from]
		source: std::str::Utf8Error,
	},

	#[error(transparent)]
	Json {
		#[backtrace]
		#[from]
		source: serde_json::Error,
	},

	#[cfg(target_os = "macos")]
	#[error(transparent)]
	Plist {
		#[backtrace]
		#[from]
		source: plist::Error,
	},

	#[cfg(target_os = "linux")]
	#[error(transparent)]
	Lfs {
		#[backtrace]
		#[from]
		source: lfs_core::Error,
	},

	#[cfg(target_os = "windows")]
	#[error(transparent)]
	WinApi {
		#[backtrace]
		#[from]
		source: windows::core::Error,
	},

	#[error("Chain of errors: {source}\n\t{others:#?}")]
	#[diagnostic()]
	Chain {
		#[backtrace]
		#[diagnostic(transparent)]
		source: Box<Error>,
		#[related]
		#[diagnostic(transparent)]
		others: Vec<Error>,
	},

	#[error(transparent)]
	#[diagnostic(transparent)]
	DeviceSerial {
		#[backtrace]
		#[from]
		source: crate::device::serial::error::SerialNumberFormatError,
	},

	#[error(transparent)]
	#[diagnostic()]
	SerialPort {
		#[backtrace]
		#[from]
		source: serialport::Error,
	},

	#[diagnostic()]
	#[error("Device not found.")]
	/// Device discovery error.
	NotFound(#[backtrace] Backtrace),

	#[diagnostic()]
	#[error("Interface not ready.")]
	/// Interface error.
	NotReady(#[backtrace] Backtrace),

	#[error("Device in the wrong state `{0:?}`.")]
	WrongState(Mode),

	#[error("Mount point not found for {0}.")]
	MountNotFound(String),
	// #[error("data store disconnected")]
	// Disconnect(#[from] io::Error),
	// #[error("the data for key `{0}` is not available")]
	// Redaction(String),
	// #[error("invalid header (expected {expected:?}, found {found:?})")]
	// InvalidHeader { expected: String, found: String },
	// #[error("unknown error")]
	// Unknown,
}


impl Error {
	#[track_caller]
	pub fn usb_timeout(device: crate::device::Device) -> Self {
		Self::DeviceTimeout { device,
		                      backtrace: Backtrace::capture() }
	}

	#[track_caller]
	pub fn timeout<S: ToString>(what: S) -> Self {
		Self::Timeout { what: what.to_string(),
		                backtrace: Backtrace::capture() }
	}

	#[track_caller]
	pub fn not_found() -> Self { Self::NotFound(Backtrace::capture()) }
	#[track_caller]
	pub fn not_ready() -> Self { Self::NotReady(Backtrace::capture()) }

	#[track_caller]
	pub fn chain<I, A, B>(err: A, others: I) -> Self
		where I: IntoIterator<Item = B>,
		      A: Into<Error>,
		      B: Into<Error> {
		Self::Chain { source: Box::new(err.into()),
		              others: others.into_iter().map(Into::into).collect() }
	}
}


unsafe impl Sync for Error {}