-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy patherror.rs
More file actions
executable file
·104 lines (91 loc) · 2.61 KB
/
error.rs
File metadata and controls
executable file
·104 lines (91 loc) · 2.61 KB
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
use serde::{Serialize, Serializer};
use std::io;
#[cfg(target_os = "android")]
use tauri::plugin::mobile::PluginInvokeError;
/// An error type for serial port operations
#[derive(Debug)]
pub enum Error {
/// IO Error (stored as string to allow cloning)
Io(String),
/// String error message
String(String),
/// Serial port error
SerialPort(String),
}
impl Clone for Error {
fn clone(&self) -> Self {
match self {
Error::Io(s) => Error::Io(s.clone()),
Error::String(s) => Error::String(s.clone()),
Error::SerialPort(s) => Error::SerialPort(s.clone()),
}
}
}
impl Error {
pub fn new(msg: impl Into<String>) -> Self {
Error::String(msg.into())
}
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Error::Io(err) => write!(f, "IO error: {}", err),
Error::String(s) => write!(f, "{}", s),
Error::SerialPort(err) => write!(f, "Serial port error: {}", err),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::Io(_) => None,
Error::SerialPort(_) => None,
Error::String(_) => None,
}
}
}
impl From<io::Error> for Error {
fn from(err: io::Error) -> Self {
Error::Io(err.to_string())
}
}
impl From<serialport::Error> for Error {
fn from(err: serialport::Error) -> Self {
Error::SerialPort(err.to_string())
}
}
impl From<&str> for Error {
fn from(err: &str) -> Self {
Error::String(err.to_string())
}
}
impl From<String> for Error {
fn from(err: String) -> Self {
Error::String(err)
}
}
impl From<Error> for io::Error {
fn from(error: Error) -> io::Error {
match error {
Error::Io(e) => io::Error::new(io::ErrorKind::Other, e),
Error::String(s) => io::Error::new(io::ErrorKind::Other, s),
Error::SerialPort(e) => io::Error::new(io::ErrorKind::Other, e),
}
}
}
impl Serialize for Error {
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&self.to_string())
}
}
#[cfg(target_os = "android")]
impl From<PluginInvokeError> for Error {
fn from(error: PluginInvokeError) -> Self {
Error::String(error.to_string())
}
}
// Error is automatically Send and Sync because all its fields are Send and Sync
// String is Send and Sync, so Error is safe to use across threads