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
#![deny(
missing_docs,
rust_2018_idioms,
unused,
unused_import_braces,
unused_lifetimes,
unused_qualifications,
warnings,
)]
use {
proc_macro::TokenStream,
proc_macro2::Span,
quote::quote,
syn::{
AttributeArgs,
Ident,
ItemFn,
Lit,
Meta,
MetaNameValue,
NestedMeta,
ReturnType,
Type,
TypePath,
parse_macro_input,
},
};
#[proc_macro_attribute]
pub fn command(_: TokenStream, item: TokenStream) -> TokenStream {
let command_fn = parse_macro_input!(item as ItemFn);
let command_name = &command_fn.sig.ident;
let command_name_str = command_name.to_string();
let wrapper_name = Ident::new(&format!("{}_wrapper", command_name), Span::call_site());
TokenStream::from(quote! {
fn #wrapper_name(program: ::std::ffi::OsString, args: Vec<::std::ffi::OsString>) {
#command_fn
match #command_name(args.into_iter()) {
Ok(subcommand) => {},
Err(e) => {
bitbar_notify(format!("{}: {}", #command_name_str, e));
::std::process::exit(1);
}
}
}
inventory::submit! {
Subcommand {
name: #command_name_str,
func: #wrapper_name,
}
}
})
}
#[proc_macro_attribute]
pub fn main(args: TokenStream, item: TokenStream) -> TokenStream {
let args = parse_macro_input!(args as AttributeArgs);
let error_template_image = match &args[..] {
[] => quote!(),
[NestedMeta::Meta(Meta::NameValue(MetaNameValue { path, lit: Lit::Str(path_lit), ..}))]
if path.get_ident().map_or(false, |arg| arg.to_string() == "error_template_image") => {
quote!(.template_image(&include_bytes!(#path_lit)[..]).never_unwrap())
}
_ => return TokenStream::from(quote!(compile_error!("unexpected bitbar::main arguments"))),
};
let main_fn = parse_macro_input!(item as ItemFn);
let asyncness = &main_fn.sig.asyncness;
let use_tokio = asyncness.as_ref().map(|_| quote!(use ::bitbar::tokio;));
let main_prefix = asyncness.as_ref().map(|async_keyword| quote!(#[tokio::main] #async_keyword));
let awaitness = asyncness.as_ref().map(|_| quote!(.await));
let ret = main_fn.sig.output;
let mut main_ret_match_body = quote! {
menu => print!("{}", menu),
};
if let ReturnType::Type(_, ref ty) = ret {
if let Type::Path(TypePath { qself: None, ref path }) = **ty {
if path.segments.len() == 1 && path.segments[0].ident.to_string() == "Result" {
main_ret_match_body = quote! {
::core::result::Result::Ok(menu) => print!("{}", menu),
::core::result::Result::Err(e) => {
let mut menu = Menu(vec![
::bitbar::ContentItem::new("?")#error_template_image.into(),
::bitbar::MenuItem::Sep,
]);
menu.extend(::bitbar::Menu::from(e));
print!("{}", menu);
}
}
}
}
};
let body = main_fn.block;
TokenStream::from(quote! {
use ::bitbar::inventory;
#use_tokio
struct Subcommand {
name: &'static str,
func: fn(::std::ffi::OsString, Vec<::std::ffi::OsString>),
}
inventory::collect!(Subcommand);
#asyncness fn main_inner() #ret #body
fn bitbar_notify(body: impl ::std::fmt::Display) {
let _ = ::bitbar::notify_rust::Notification::default()
.summary(&env!("CARGO_PKG_NAME"))
.sound_name("Funky")
.body(&body.to_string())
.show();
}
#main_prefix fn main() {
let mut args = ::std::env::args_os();
let program = args.next().expect("missing program name");
if let ::core::option::Option::Some(subcommand) = args.next() {
let subcommand = match subcommand.into_string() {
::core::result::Result::Ok(subcommand) => subcommand,
::core::result::Result::Err(_) => {
bitbar_notify("subcommand is not valid UTF-8");
::std::process::exit(1);
}
};
if let ::core::option::Option::Some(command) = inventory::iter::<Subcommand>.into_iter().find(|command| command.name == subcommand) {
(command.func)(program, args.collect());
} else {
bitbar_notify(format!("no such subcommand: {}", subcommand));
::std::process::exit(1);
}
} else {
match main_inner()#awaitness { #main_ret_match_body }
}
}
})
}