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
//! Stable names for `std::io::ErrorKind`.
use std::io::ErrorKind;
use crate::closed_enum::closed_enum;
closed_enum! {
/// The kind of an I/O failure, spelled the same in every front end.
///
/// `std::io::ErrorKind`'s `Display` text is not a stable contract and the
/// enum itself is `#[non_exhaustive]`; this is the closed subset exarch
/// reports, so Python, Node and the CLI emit identical tokens. A kind
/// without an entry, and `ErrorKind::Other`, is [`Other`](Self::Other).
///
/// # Examples
///
/// ```
/// use exarch_core::IoKind;
/// use std::io::ErrorKind;
///
/// assert_eq!(IoKind::from(ErrorKind::PermissionDenied).as_str(), "PermissionDenied");
/// assert_eq!(IoKind::from(ErrorKind::HostUnreachable), IoKind::Other);
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum IoKind {
/// `ErrorKind::NotFound`.
NotFound => "NotFound",
/// `ErrorKind::PermissionDenied`.
PermissionDenied => "PermissionDenied",
/// `ErrorKind::AlreadyExists`.
AlreadyExists => "AlreadyExists",
/// `ErrorKind::NotADirectory`.
NotADirectory => "NotADirectory",
/// `ErrorKind::IsADirectory`.
IsADirectory => "IsADirectory",
/// `ErrorKind::DirectoryNotEmpty`.
DirectoryNotEmpty => "DirectoryNotEmpty",
/// `ErrorKind::ReadOnlyFilesystem`.
ReadOnlyFilesystem => "ReadOnlyFilesystem",
/// `ErrorKind::StorageFull`.
StorageFull => "StorageFull",
/// `ErrorKind::QuotaExceeded`.
QuotaExceeded => "QuotaExceeded",
/// `ErrorKind::FileTooLarge`.
FileTooLarge => "FileTooLarge",
/// `ErrorKind::InvalidInput`.
InvalidInput => "InvalidInput",
/// `ErrorKind::InvalidData`.
InvalidData => "InvalidData",
/// `ErrorKind::InvalidFilename`.
InvalidFilename => "InvalidFilename",
/// `ErrorKind::TimedOut`.
TimedOut => "TimedOut",
/// `ErrorKind::Interrupted`.
Interrupted => "Interrupted",
/// `ErrorKind::UnexpectedEof`.
UnexpectedEof => "UnexpectedEof",
/// `ErrorKind::WriteZero`.
WriteZero => "WriteZero",
/// `ErrorKind::Unsupported`.
Unsupported => "Unsupported",
/// `ErrorKind::CrossesDevices`.
CrossesDevices => "CrossesDevices",
/// `ErrorKind::TooManyLinks`.
TooManyLinks => "TooManyLinks",
/// `ErrorKind::ResourceBusy`.
ResourceBusy => "ResourceBusy",
/// `ErrorKind::BrokenPipe`.
BrokenPipe => "BrokenPipe",
/// `ErrorKind::OutOfMemory`.
OutOfMemory => "OutOfMemory",
/// `ErrorKind::WouldBlock`.
WouldBlock => "WouldBlock",
/// `ErrorKind::Other`, and any kind without an entry here.
Other => "Other",
}
}
impl From<ErrorKind> for IoKind {
fn from(kind: ErrorKind) -> Self {
use ErrorKind as K;
match kind {
K::NotFound => Self::NotFound,
K::PermissionDenied => Self::PermissionDenied,
K::AlreadyExists => Self::AlreadyExists,
K::NotADirectory => Self::NotADirectory,
K::IsADirectory => Self::IsADirectory,
K::DirectoryNotEmpty => Self::DirectoryNotEmpty,
K::ReadOnlyFilesystem => Self::ReadOnlyFilesystem,
K::StorageFull => Self::StorageFull,
K::QuotaExceeded => Self::QuotaExceeded,
K::FileTooLarge => Self::FileTooLarge,
K::InvalidInput => Self::InvalidInput,
K::InvalidData => Self::InvalidData,
K::InvalidFilename => Self::InvalidFilename,
K::TimedOut => Self::TimedOut,
K::Interrupted => Self::Interrupted,
K::UnexpectedEof => Self::UnexpectedEof,
K::WriteZero => Self::WriteZero,
K::Unsupported => Self::Unsupported,
K::CrossesDevices => Self::CrossesDevices,
K::TooManyLinks => Self::TooManyLinks,
K::ResourceBusy => Self::ResourceBusy,
K::BrokenPipe => Self::BrokenPipe,
K::OutOfMemory => Self::OutOfMemory,
K::WouldBlock => Self::WouldBlock,
_ => Self::Other,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn every_named_kind_round_trips_through_its_spelling() {
for kind in IoKind::ALL {
assert_eq!(kind.as_str(), kind.to_string());
}
assert_eq!(IoKind::ALL.len(), 25);
}
#[test]
fn an_unlisted_kind_is_other() {
assert_eq!(IoKind::from(ErrorKind::AddrInUse), IoKind::Other);
assert_eq!(IoKind::from(ErrorKind::Other), IoKind::Other);
}
}