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
#[macro_use]
pub mod pub_macros {
/// Macro to create const for partition types.
macro_rules! partition_types {
(
$(
$(#[$docs:meta])*
($upcase:ident, $guid:expr, $os:expr)$(,)*
)+
) => {
const fn str_to_uuid_or_panic(s: &str) -> Uuid {
let res_u = Uuid::try_parse(s);
match res_u {
Ok(u) => return u,
Err(_) => {
panic!("string was not an uuid");
},
}
}
$(
$(#[$docs])*
pub const $upcase: Type = Type {
guid: str_to_uuid_or_panic($guid),
os: $os,
};
)+
impl FromStr for Type {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_ascii_uppercase().as_str() {
$(
$guid |
stringify!($upcase) => Ok($upcase),
)+
_ => {
match ::uuid::Uuid::from_str(s) {
Ok(u) => Ok(Type {
guid: u,
os: OperatingSystem::None,
}),
Err(_) => Err("Invalid Partition Type GUID.".to_string()),
}
}
}
}
}
impl From<Uuid> for Type {
fn from(guid: Uuid) -> Self {
$(
if guid == $upcase.guid {
return $upcase;
}
)+
Type {
guid,
os: OperatingSystem::None,
}
}
}
}
}
}
pub(crate) trait ResultInsert<T> {
fn insert_ok(&mut self, value: T) -> &mut T;
}
impl<T, E> ResultInsert<T> for Result<T, E> {
fn insert_ok(&mut self, value: T) -> &mut T {
*self = Ok(value);
// SAFETY: the code above just filled the option
unsafe { self.as_mut().unwrap_unchecked() }
}
}