aboutsummaryrefslogtreecommitdiff
path: root/mingling_core/src/program/repl_exec.rs
diff options
context:
space:
mode:
author魏曹先生 <1992414357@qq.com>2026-05-19 21:38:58 +0800
committer魏曹先生 <1992414357@qq.com>2026-05-19 21:38:58 +0800
commitbd4b09b06181093c95e865b04d4a9cdda7dd0728 (patch)
treeaee9b76455ce4597e718f472b02e59086a802ce7 /mingling_core/src/program/repl_exec.rs
parent5d61201e9532cfc92f4b93e34b7c10a97cfb35df (diff)
Conditionally disable panic unwind support for async feature
Diffstat (limited to 'mingling_core/src/program/repl_exec.rs')
-rw-r--r--mingling_core/src/program/repl_exec.rs53
1 files changed, 46 insertions, 7 deletions
diff --git a/mingling_core/src/program/repl_exec.rs b/mingling_core/src/program/repl_exec.rs
index f3dd9f7..5246ece 100644
--- a/mingling_core/src/program/repl_exec.rs
+++ b/mingling_core/src/program/repl_exec.rs
@@ -18,13 +18,6 @@ where
///
/// This method starts an infinite loop that continuously reads user input, parses commands, executes them,
/// and displays the execution result or error message. It is suitable for scenarios requiring command-line interaction with the user.
- ///
- /// # Important
- ///
- /// **REPL mode is currently only available when the `async` feature is disabled; it does not support async.**
- ///
- /// If the `async` feature is enabled, this method will be unavailable (as it is protected by `#[cfg(not(feature = "async"))]` conditional compilation).
- /// Future versions may support asynchronous REPL, but currently only synchronous mode is supported.
pub fn exec_repl(self) {
self.run_hook_repl_on_begin();
@@ -50,6 +43,41 @@ where
}
}
+#[cfg(feature = "async")]
+impl<C> Program<C>
+where
+ C: ProgramCollect<Enum = C> + Send + Sync + 'static,
+{
+ /// Executes the REPL interactive CLI mode.
+ ///
+ /// This method starts an infinite loop that continuously reads user input, parses commands, executes them,
+ /// and displays the execution result or error message. It is suitable for scenarios requiring command-line interaction with the user.
+ ///
+ /// **Note:** When the `async` feature is enabled, panic unwinding is not supported.
+ /// Any panics during command execution will result in an abort rather than being caught and handled gracefully.
+ pub async fn exec_repl(self) {
+ self.run_hook_repl_on_begin();
+
+ self.exec_wrapper(async |p| -> ! {
+ loop {
+ p.run_hook_repl_pre_readline();
+ let readline = readline_or_empty();
+ p.run_hook_repl_post_readline(&readline);
+
+ let args = split_input_string(readline.clone());
+
+ match exec_once(p, args).await {
+ Ok(r) => {
+ p.run_hook_repl_on_receive_result(&r);
+ }
+ _ => {}
+ }
+ }
+ })
+ .await;
+ }
+}
+
fn readline() -> Result<String, std::io::Error> {
let mut input = String::new();
std::io::stdout().flush()?;
@@ -99,3 +127,14 @@ where
exec_result
}
+
+#[cfg(feature = "async")]
+async fn exec_once<C>(
+ p: &'static Program<C>,
+ args: Vec<String>,
+) -> Result<RenderResult, ProgramInternalExecuteError>
+where
+ C: ProgramCollect<Enum = C> + Send + Sync + 'static,
+{
+ super::exec::exec_with_args(p, args).await
+}