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
|
use crate::{
AnyOutput, ChainProcess, RenderResult, asset::dispatcher::Dispatcher,
error::ProgramExecuteError,
};
use std::{env, fmt::Display, pin::Pin};
pub mod exec;
pub mod setup;
mod config;
pub use config::*;
mod flag;
pub use flag::*;
use tokio::io::AsyncWriteExt;
#[derive(Default)]
pub struct Program<C, G>
where
C: ProgramCollect,
G: Display,
{
pub(crate) collect: std::marker::PhantomData<C>,
pub(crate) group: std::marker::PhantomData<G>,
pub(crate) args: Vec<String>,
pub(crate) dispatcher: Vec<Box<dyn Dispatcher<G>>>,
pub stdout_setting: ProgramStdoutSetting,
pub user_context: ProgramUserContext,
}
impl<C, G> Program<C, G>
where
C: ProgramCollect<Enum = G>,
G: Display,
{
/// Creates a new Program instance, initializing args from environment.
pub fn new() -> Self {
Program {
collect: std::marker::PhantomData,
group: std::marker::PhantomData,
args: env::args().collect(),
dispatcher: Vec::new(),
stdout_setting: Default::default(),
user_context: Default::default(),
}
}
/// Run the command line program
pub async fn exec_without_render(mut self) -> Result<RenderResult, ProgramExecuteError> {
self.args = self.args.iter().skip(1).cloned().collect();
crate::exec::exec(self).await.map_err(|e| e.into())
}
/// Run the command line program
pub async fn exec(self) {
let stdout_setting = self.stdout_setting.clone();
let result = match self.exec_without_render().await {
Ok(r) => r,
Err(e) => match e {
ProgramExecuteError::DispatcherNotFound => {
eprintln!("Dispatcher not found");
return;
}
ProgramExecuteError::RendererNotFound(renderer_name) => {
eprintln!("Renderer `{}` not found", renderer_name);
return;
}
ProgramExecuteError::Other(e) => {
eprintln!("{}", e);
return;
}
},
};
// Render result
if stdout_setting.render_output && !result.is_empty() {
print!("{}", result);
if let Err(e) = tokio::io::stdout().flush().await
&& stdout_setting.error_output
{
eprintln!("{}", e);
}
}
}
}
pub trait ProgramCollect {
type Enum: Display;
fn build_renderer_not_found(member_id: Self::Enum) -> AnyOutput<Self::Enum>;
fn build_dispatcher_not_found(args: Vec<String>) -> AnyOutput<Self::Enum>;
fn render(any: AnyOutput<Self::Enum>, r: &mut RenderResult);
fn do_chain(
any: AnyOutput<Self::Enum>,
) -> Pin<Box<dyn Future<Output = ChainProcess<Self::Enum>> + Send>>;
fn has_renderer(any: &AnyOutput<Self::Enum>) -> bool;
fn has_chain(any: &AnyOutput<Self::Enum>) -> bool;
}
#[macro_export]
#[doc(hidden)]
macro_rules! __dispatch_program_renderers {
(
$( $render_ty:ty => $prev_ty:ident, )*
) => {
fn render(any: mingling::AnyOutput<Self::Enum>, r: &mut mingling::RenderResult) {
match any.member_id {
$(
Self::$prev_ty => {
// SAFETY: The `type_id` check ensures that `any` contains a value of type `$prev_ty`,
// so downcasting to `$prev_ty` is safe.
let value = unsafe { any.downcast::<$prev_ty>().unwrap_unchecked() };
<$render_ty as mingling::Renderer>::render(value, r);
}
)*
_ => (),
}
}
};
}
#[macro_export]
#[doc(hidden)]
macro_rules! __dispatch_program_chains {
(
$( $chain_ty:ty => $chain_prev:ident, )*
) => {
fn do_chain(
any: mingling::AnyOutput<Self::Enum>,
) -> std::pin::Pin<Box<dyn Future<Output = mingling::ChainProcess<Self::Enum>> + Send>> {
match any.member_id {
$(
Self::$chain_prev => {
// SAFETY: The `type_id` check ensures that `any` contains a value of type `$chain_prev`,
// so downcasting to `$chain_prev` is safe.
let value = unsafe { any.downcast::<$chain_prev>().unwrap_unchecked() };
let fut = async { <$chain_ty as mingling::Chain<Self::Enum>>::proc(value).await };
Box::pin(fut)
}
)*
_ => panic!("No chain found for type id: {:?}", any.type_id),
}
}
};
}
|