aboutsummaryrefslogtreecommitdiff
path: root/examples/example-argument-picker/src/main.rs
diff options
context:
space:
mode:
author魏曹先生 <1992414357@qq.com>2026-08-17 05:49:19 +0800
committer魏曹先生 <1992414357@qq.com>2026-08-17 05:49:19 +0800
commit57c53affe3542cb6bd4e79ee4c18f20a1bd76b2d (patch)
tree1cd4aef44cb7a45a8cd9d520b598f5f181e24c76 /examples/example-argument-picker/src/main.rs
parentef23cd944402939605c78a4a853ef6e33af02c21 (diff)
refactor!: replace pack! macros with derive-based pipeline types
Remove the `pack!`, `pack_err!`, `pack_structural!`, and `pack_err_structural!` macros, replacing all pipeline type definitions with `#[derive(Grouped)]` and `#[derive(Grouped, Wrap)]` attributes. This changes the generated struct shape from named-field structs with an `inner` field to tuple structs accessed via `.0`, and removes the auto-generated `name` and `info` fields from error types.
Diffstat (limited to 'examples/example-argument-picker/src/main.rs')
-rw-r--r--examples/example-argument-picker/src/main.rs69
1 files changed, 42 insertions, 27 deletions
diff --git a/examples/example-argument-picker/src/main.rs b/examples/example-argument-picker/src/main.rs
index 99eee30..aefb86e 100644
--- a/examples/example-argument-picker/src/main.rs
+++ b/examples/example-argument-picker/src/main.rs
@@ -44,17 +44,32 @@ use mingling::setup::picker::BasicProgramSetup;
dispatcher!("calc", EntryCalculate);
-pack_err!(ErrorNumberANotProvided);
-pack_err!(ErrorNumberBNotProvided);
-pack_err!(ErrorNumberOperatorNotProvided);
-pack_err!(ErrorDivisionByZero);
+#[derive(Grouped, Default)]
+pub struct ErrorNumberANotProvided;
-pack!(StateAdd = (f32, f32));
-pack!(StateSubtract = (f32, f32));
-pack!(StateMultiply = (f32, f32));
-pack!(StateDivide = (f32, f32));
+#[derive(Grouped, Default)]
+pub struct ErrorNumberBNotProvided;
-pack!(ResultNumber = f32);
+#[derive(Grouped, Default)]
+pub struct ErrorNumberOperatorNotProvided;
+
+#[derive(Grouped, Default)]
+pub struct ErrorDivisionByZero;
+
+#[derive(Grouped, Wrap)]
+pub struct StateAdd((f32, f32));
+
+#[derive(Grouped, Wrap)]
+pub struct StateSubtract((f32, f32));
+
+#[derive(Grouped, Wrap)]
+pub struct StateMultiply((f32, f32));
+
+#[derive(Grouped, Wrap)]
+pub struct StateDivide((f32, f32));
+
+#[derive(Grouped, Wrap)]
+pub struct ResultNumber(f32);
#[derive(Grouped)]
struct StateCalculate {
@@ -135,13 +150,13 @@ fn handle_calc(args: EntryCalculate) -> Next {
// Use the arg! macro to define a positional argument of type f32
// |
// vvvvvvvvvv
- args.pick_or_route(&arg![f32], || ErrorNumberANotProvided::default().to_chain())
+ args.pick_or_route(&arg![f32], || ErrorNumberANotProvided.to_chain())
.pick_or_route(&arg![Operator], || {
- ErrorNumberOperatorNotProvided::default().to_chain()
+ ErrorNumberOperatorNotProvided.to_chain()
}) // Returns a routable type when not found or fails to parse
// |
- // vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
- .pick_or_route(&arg![f32], || ErrorNumberBNotProvided::default().to_chain())
+ // vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
+ .pick_or_route(&arg![f32], || ErrorNumberBNotProvided.to_chain())
// Use `to_result` to parse arguments
// and convert to Result<(Tuple, ...), Route> type
.to_result()
@@ -149,7 +164,7 @@ fn handle_calc(args: EntryCalculate) -> Next {
// --------- IMPORTANT ---------
if operator == Operator::Slash && number_b == 0. {
- return ErrorDivisionByZero::default().to_chain();
+ return ErrorDivisionByZero.to_chain();
}
StateCalculate {
@@ -163,41 +178,41 @@ fn handle_calc(args: EntryCalculate) -> Next {
#[chain]
fn handle_state_calculate(state: StateCalculate) -> Next {
match (state.operator, state.number_a, state.number_b) {
- (Operator::Plus, a, b) => StateAdd::new((a, b)).to_chain(),
- (Operator::Dash, a, b) => StateSubtract::new((a, b)).to_chain(),
- (Operator::Slash, a, b) => StateDivide::new((a, b)).to_chain(),
- (Operator::Star, a, b) => StateMultiply::new((a, b)).to_chain(),
+ (Operator::Plus, a, b) => StateAdd((a, b)).to_chain(),
+ (Operator::Dash, a, b) => StateSubtract((a, b)).to_chain(),
+ (Operator::Slash, a, b) => StateDivide((a, b)).to_chain(),
+ (Operator::Star, a, b) => StateMultiply((a, b)).to_chain(),
}
}
#[chain]
fn handle_state_add(state_add: StateAdd) -> ResultNumber {
- let (a, b) = state_add.inner;
- ResultNumber::new(a + b)
+ let (a, b) = state_add.0;
+ ResultNumber(a + b)
}
#[chain]
fn handle_state_subtract(state_subtract: StateSubtract) -> ResultNumber {
- let (a, b) = state_subtract.inner;
- ResultNumber::new(a - b)
+ let (a, b) = state_subtract.0;
+ ResultNumber(a - b)
}
#[chain]
fn handle_state_multiply(state_multiply: StateMultiply) -> ResultNumber {
- let (a, b) = state_multiply.inner;
- ResultNumber::new(a * b)
+ let (a, b) = state_multiply.0;
+ ResultNumber(a * b)
}
#[chain]
fn handle_state_divide(state_divide: StateDivide) -> ResultNumber {
- let (a, b) = state_divide.inner;
- ResultNumber::new(a / b)
+ let (a, b) = state_divide.0;
+ ResultNumber(a / b)
}
#[renderer]
fn render_result_number(result: ResultNumber, setting: &ResNumberDisplaySetting) -> String {
let round = setting.round;
- let result = if round { result.round() } else { result.inner };
+ let result = if round { result.round() } else { result.0 };
format!("Result: {}", result)
}