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
use convert_case::{Case, Casing};
use proc_macro2::TokenStream;
use quote::quote;
use syn::{parse_macro_input, ItemEnum};
#[proc_macro_derive(Privsep, attributes(unimplemented))]
pub fn derive_privsep(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
let input = parse_macro_input!(item as ItemEnum);
derive_privsep_enum(input).into()
}
fn derive_privsep_enum(item: ItemEnum) -> TokenStream {
let ident = item.ident.clone();
let mut const_as_array = vec![];
let mut as_ref_str = vec![];
let mut from_id = vec![];
let mut to_id = vec![];
let doc1 = item.attrs.iter().filter(|attr| attr.path.is_ident("doc"));
for (id, variant) in item.variants.iter().enumerate() {
let ident = &variant.ident;
let name = ident.to_string().to_case(Case::Kebab);
const_as_array.push(quote! {
Process { name: #name },
});
as_ref_str.push(quote! {
Self::#ident => #name,
});
from_id.push(quote! {
#id => Ok(Self::#ident),
});
to_id.push(quote! {
Self::#ident => #id,
});
}
let array_len = const_as_array.len();
quote! {
#(#doc1)*
impl #ident {
pub const fn as_array() -> [privsep::process::Process; #array_len] {
use privsep::process::Process;
[
#(#const_as_array)*
]
}
}
impl AsRef<str> for #ident {
fn as_ref(&self) -> &str {
match self {
#(#as_ref_str)*
}
}
}
impl std::convert::TryFrom<usize> for #ident {
type Error = &'static str;
fn try_from(id: usize) -> Result<Self, Self::Error> {
match id {
#(#from_id)*
_ => Err("Invalid privsep process ID"),
}
}
}
impl std::ops::Deref for #ident {
type Target = usize;
fn deref(&self) -> &Self::Target {
&match self {
#(#to_id)*
}
}
}
}
}