aboutsummaryrefslogtreecommitdiff
path: root/mingling_core/src/program.rs
diff options
context:
space:
mode:
author魏曹先生 <1992414357@qq.com>2026-04-22 13:27:43 +0800
committer魏曹先生 <1992414357@qq.com>2026-04-22 13:30:13 +0800
commit3785202ec5b949cba5501b20729b16f4c29ea626 (patch)
tree2a276df54128e73557463af54d626a1a7c250e94 /mingling_core/src/program.rs
parentc2f9fb47832c5e12fe37762616c81b016de00464 (diff)
Add new_with_args and dispatch_args_dynamic to Program
Remove Display bound from Group type parameter and delete dispatcher_render macro. This is a breaking change.
Diffstat (limited to 'mingling_core/src/program.rs')
-rw-r--r--mingling_core/src/program.rs65
1 files changed, 41 insertions, 24 deletions
diff --git a/mingling_core/src/program.rs b/mingling_core/src/program.rs
index db5957b..d655c74 100644
--- a/mingling_core/src/program.rs
+++ b/mingling_core/src/program.rs
@@ -8,8 +8,9 @@ use crate::error::GeneralRendererSerializeError;
use std::env;
use crate::{
- AnyOutput, ChainProcess, RenderResult, asset::dispatcher::Dispatcher,
- error::ProgramExecuteError,
+ AnyOutput, ChainProcess, RenderResult,
+ asset::dispatcher::Dispatcher,
+ error::{ChainProcessError, ProgramExecuteError},
};
use std::{fmt::Display, sync::OnceLock};
@@ -27,13 +28,16 @@ pub use config::*;
mod flag;
pub use flag::*;
+mod string_vec;
+pub use string_vec::*;
+
/// Global static reference to the current program instance
static THIS_PROGRAM: OnceLock<Option<Box<dyn std::any::Any + Send + Sync>>> = OnceLock::new();
/// Returns a reference to the current program instance, panics if not set.
pub fn this<C>() -> &'static Program<C, C>
where
- C: ProgramCollect + Display + 'static,
+ C: ProgramCollect + 'static,
{
try_get_this_program().expect("Program not initialized")
}
@@ -41,7 +45,7 @@ where
/// Returns a reference to the current program instance, if set.
fn try_get_this_program<C>() -> Option<&'static Program<C, C>>
where
- C: ProgramCollect + Display + 'static,
+ C: ProgramCollect + 'static,
{
THIS_PROGRAM
.get()?
@@ -54,7 +58,6 @@ where
pub struct Program<C, G>
where
C: ProgramCollect,
- G: Display,
{
pub(crate) collect: std::marker::PhantomData<C>,
pub(crate) group: std::marker::PhantomData<G>,
@@ -72,26 +75,31 @@ where
impl<C, G> Program<C, G>
where
C: ProgramCollect<Enum = G>,
- G: Display,
{
- /// Creates a new Program instance, initializing args from environment.
+ /// Creates a new Program instance, initializing command-line arguments from the environment.
pub fn new() -> Self {
+ #[cfg(not(windows))]
+ return Self::new_with_args(env::args().collect::<Vec<String>>());
+
+ #[cfg(windows)]
+ return Self::new_with_args({
+ std::env::args_os()
+ .map(|arg| {
+ use std::os::windows::ffi::OsStrExt;
+
+ let wide: Vec<u16> = arg.encode_wide().collect();
+ String::from_utf16_lossy(&wide)
+ })
+ .collect()
+ });
+ }
+
+ /// Creates a new Program instance with the provided command-line arguments.
+ pub fn new_with_args(args: impl Into<StringVec>) -> Self {
Program {
collect: std::marker::PhantomData,
group: std::marker::PhantomData,
- #[cfg(not(windows))]
- args: env::args().collect(),
- #[cfg(windows)]
- args: {
- std::env::args_os()
- .map(|arg| {
- use std::os::windows::ffi::OsStrExt;
-
- let wide: Vec<u16> = arg.encode_wide().collect();
- String::from_utf16_lossy(&wide)
- })
- .collect()
- },
+ args: args.into().into(),
dispatcher: Vec::new(),
stdout_setting: Default::default(),
user_context: Default::default(),
@@ -116,10 +124,21 @@ where
.unwrap()
}
- // Get all registered dispatcher names from the program
+ /// Get all registered dispatcher names from the program
pub fn get_nodes(&self) -> Vec<(String, &(dyn Dispatcher<G> + Send + Sync))> {
get_nodes(self)
}
+
+ /// Dynamically dispatch input arguments to registered entry types
+ pub fn dispatch_args_dynamic(
+ &self,
+ args: impl Into<StringVec>,
+ ) -> Result<AnyOutput<G>, ChainProcessError> {
+ match exec::dispatch_args_dynamic(self, args.into().into()) {
+ Ok(ok) => Ok(ok),
+ Err(e) => Err(e.into()),
+ }
+ }
}
// Async program
@@ -127,7 +146,6 @@ where
impl<C, G> Program<C, G>
where
C: ProgramCollect<Enum = G>,
- G: Display,
{
/// Sets the current program instance and runs the provided async function.
async fn set_instance_and_run<F, Fut>(self, f: F) -> Fut::Output
@@ -201,7 +219,6 @@ where
impl<C, G> Program<C, G>
where
C: ProgramCollect<Enum = G>,
- G: Display,
{
/// Sets the current program instance and runs the provided function.
fn set_instance_and_run<F, R>(self, f: F) -> R
@@ -386,7 +403,7 @@ macro_rules! __dispatch_program_chains {
}
/// Get all registered dispatcher names from the program
-pub fn get_nodes<C: ProgramCollect<Enum = G>, G: Display>(
+pub fn get_nodes<C: ProgramCollect<Enum = G>, G>(
program: &Program<C, G>,
) -> Vec<(String, &(dyn Dispatcher<G> + Send + Sync))> {
program