Skip to main content

maa_framework/
pipeline.rs

1//! Pipeline configuration types for recognition and action definitions.
2
3use serde::{Deserialize, Deserializer, Serialize, de::DeserializeOwned};
4use serde_json::Value;
5use std::collections::HashMap;
6
7pub use crate::common::Rect;
8
9// --- Custom Deserializers for Scalar/Array Polymorphism ---
10// The C API may return either a scalar or an array for some fields.
11
12/// Deserialize a value that can be either T or Vec<T> into Vec<T>
13fn scalar_or_vec<'de, D, T>(deserializer: D) -> Result<Vec<T>, D::Error>
14where
15    D: Deserializer<'de>,
16    T: DeserializeOwned,
17{
18    let value = Value::deserialize(deserializer)?;
19
20    // Try to parse as Vec<T> first
21    if let Ok(vec) = serde_json::from_value::<Vec<T>>(value.clone()) {
22        return Ok(vec);
23    }
24
25    // Fallback to T
26    if let Ok(single) = serde_json::from_value::<T>(value) {
27        return Ok(vec![single]);
28    }
29
30    Err(serde::de::Error::custom("Expected T or Vec<T>"))
31}
32
33// --- Common Types ---
34
35/// Region of interest: (x, y, width, height). Use [0, 0, 0, 0] for full screen.
36pub type Roi = (i32, i32, i32, i32);
37
38/// Target can be:
39/// - true: recognized position
40/// - "NodeName": position from previously executed node
41/// - \[ x, y \]: point (2 elements)
42/// - \[ x, y, w, h \]: area (4 elements)
43#[derive(Serialize, Deserialize, Debug, Clone)]
44#[serde(untagged)]
45pub enum Target {
46    Bool(bool),
47    Name(String),
48    Point((i32, i32)),
49    Rect(Rect),
50}
51
52impl Default for Target {
53    fn default() -> Self {
54        Target::Bool(true)
55    }
56}
57
58/// Anchor configuration.
59///
60/// Can be:
61/// - String: Set anchor to current node.
62/// - List of strings: Set multiple anchors to current node.
63/// - Map: Set anchors to specific nodes (or clear if empty).
64#[derive(Serialize, Deserialize, Debug, Clone)]
65#[serde(untagged)]
66pub enum Anchor {
67    Name(String),
68    List(Vec<String>),
69    Map(HashMap<String, String>),
70}
71
72impl Default for Anchor {
73    fn default() -> Self {
74        Anchor::List(Vec::new())
75    }
76}
77
78// --- Node Attribute ---
79
80/// Node attribute for specifying behavior in `next` and `on_error` lists.
81///
82/// Allows setting additional control parameters when referencing nodes.
83#[derive(Serialize, Deserialize, Debug, Clone, Default)]
84pub struct NodeAttr {
85    /// Node name to reference.
86    #[serde(default)]
87    pub name: String,
88    /// Whether to return to this node after the referenced node completes.
89    #[serde(default)]
90    pub jump_back: bool,
91    /// Whether to use an anchor reference.
92    #[serde(default)]
93    pub anchor: bool,
94}
95
96// --- Wait Freezes ---
97
98/// Configuration for waiting until the screen stops changing.
99///
100/// Used in `pre_wait_freezes`, `post_wait_freezes`, and `repeat_wait_freezes`
101/// to wait for the screen to stabilize before/after actions.
102#[derive(Serialize, Deserialize, Debug, Clone)]
103pub struct WaitFreezes {
104    /// Duration in milliseconds the screen must remain stable. Default: 1.
105    #[serde(default = "default_wait_time")]
106    pub time: i32,
107    /// Target area to monitor for changes.
108    #[serde(default)]
109    pub target: Target,
110    /// Offset applied to the target area.
111    #[serde(default)]
112    pub target_offset: Rect,
113    /// Similarity threshold for detecting changes. Default: 0.95.
114    #[serde(default = "default_wait_threshold")]
115    pub threshold: f64,
116    /// Comparison method (cv::TemplateMatchModes). Default: 5.
117    #[serde(default = "default_wait_method")]
118    pub method: i32,
119    /// Minimum interval between checks in milliseconds. Default: 1000.
120    #[serde(default = "default_rate_limit")]
121    pub rate_limit: i32,
122    /// Overall timeout in milliseconds. Default: 20000.
123    #[serde(default = "default_timeout")]
124    pub timeout: i32,
125}
126
127impl Default for WaitFreezes {
128    fn default() -> Self {
129        Self {
130            time: default_wait_time(),
131            target: Target::default(),
132            target_offset: Rect::default(),
133            threshold: default_wait_threshold(),
134            method: default_wait_method(),
135            rate_limit: default_rate_limit(),
136            timeout: default_timeout(),
137        }
138    }
139}
140
141// --- Recognition Enums ---
142
143/// Recognition algorithm types.
144///
145/// Determines how the framework identifies targets on screen:
146/// - [`DirectHit`] - No recognition, always matches
147/// - [`TemplateMatch`] - Image template matching
148/// - [`FeatureMatch`] - Feature-based matching (rotation/scale invariant)
149/// - [`ColorMatch`] - Color-based matching
150/// - [`OCR`] - Optical character recognition
151/// - [`NeuralNetworkClassify`] - Deep learning classification
152/// - [`NeuralNetworkDetect`] - Deep learning object detection
153/// - [`And`] - Logical AND of multiple recognitions
154/// - [`Or`] - Logical OR of multiple recognitions
155/// - `Custom` - User-defined recognition
156#[derive(Serialize, Deserialize, Debug, Clone)]
157#[serde(tag = "type", content = "param")]
158pub enum Recognition {
159    DirectHit(DirectHit),
160    TemplateMatch(TemplateMatch),
161    FeatureMatch(FeatureMatch),
162    ColorMatch(ColorMatch),
163    OCR(OCR),
164    NeuralNetworkClassify(NeuralNetworkClassify),
165    NeuralNetworkDetect(NeuralNetworkDetect),
166    And(And),
167    Or(Or),
168    Custom(CustomRecognition),
169}
170
171/// Reference to a recognition: either an inline definition or a node name.
172#[derive(Serialize, Deserialize, Debug, Clone)]
173#[serde(untagged)]
174pub enum RecognitionRef {
175    NodeName(String),
176    Inline(Recognition),
177}
178
179// --- Specific Recognition Structs ---
180
181/// Direct hit recognition - always matches without performing actual recognition.
182///
183/// Use when you want to execute an action without image matching.
184#[derive(Serialize, Deserialize, Debug, Clone, Default)]
185pub struct DirectHit {
186    /// Recognition region. Default: \\[0,0,0,0\\] (full screen).
187    #[serde(default = "default_roi_zero")]
188    pub roi: Target,
189    /// Offset applied to the ROI.
190    #[serde(default)]
191    pub roi_offset: Rect,
192}
193
194/// Template matching recognition - finds images using OpenCV template matching.
195///
196/// The most common recognition method for "finding images" on screen.
197#[derive(Serialize, Deserialize, Debug, Clone)]
198pub struct TemplateMatch {
199    /// Template image paths relative to `image` folder. Required.
200    #[serde(deserialize_with = "scalar_or_vec")]
201    pub template: Vec<String>,
202    /// Recognition region. Default: \\[0,0,0,0\\] (full screen).
203    #[serde(default = "default_roi_zero")]
204    pub roi: Target,
205    /// Offset applied to the ROI.
206    #[serde(default)]
207    pub roi_offset: Rect,
208    /// Matching threshold(s). Default: [0.7].
209    #[serde(default = "default_threshold", deserialize_with = "scalar_or_vec")]
210    pub threshold: Vec<f64>,
211    /// Result sorting: "Horizontal", "Vertical", "Score", "Random". Default: "Horizontal".
212    #[serde(default = "default_order_by")]
213    pub order_by: String,
214    /// Which result to select (0-indexed, negative for reverse). Default: 0.
215    #[serde(default)]
216    pub index: i32,
217    /// OpenCV matching method (cv::TemplateMatchModes). Default: 5 (TM_CCOEFF_NORMED).
218    #[serde(default = "default_template_method")]
219    pub method: i32,
220    /// Use green (0,255,0) as mask color. Default: false.
221    #[serde(default)]
222    pub green_mask: bool,
223}
224
225/// Feature-based matching - scale and rotation invariant image matching.
226///
227/// More robust than template matching for detecting objects under transformation.
228#[derive(Serialize, Deserialize, Debug, Clone)]
229pub struct FeatureMatch {
230    /// Template image paths relative to `image` folder. Required.
231    #[serde(deserialize_with = "scalar_or_vec")]
232    pub template: Vec<String>,
233    /// Recognition region. Default: \\[0,0,0,0\\] (full screen).
234    #[serde(default = "default_roi_zero")]
235    pub roi: Target,
236    /// Offset applied to the ROI.
237    #[serde(default)]
238    pub roi_offset: Rect,
239    /// Feature detector: "SIFT", "KAZE", "AKAZE", "BRISK", "ORB". Default: "SIFT".
240    #[serde(default = "default_detector")]
241    pub detector: String,
242    /// Result sorting method. Default: "Horizontal".
243    #[serde(default = "default_order_by")]
244    pub order_by: String,
245    /// Minimum feature point matches required. Default: 4.
246    #[serde(default = "default_feature_count")]
247    pub count: i32,
248    /// Which result to select. Default: 0.
249    #[serde(default)]
250    pub index: i32,
251    /// Use green (0,255,0) as mask color. Default: false.
252    #[serde(default)]
253    pub green_mask: bool,
254    /// KNN distance ratio threshold [0-1.0]. Default: 0.6.
255    #[serde(default = "default_feature_ratio")]
256    pub ratio: f64,
257}
258
259/// Color matching recognition - finds regions by color range.
260///
261/// Matches pixels within specified color bounds.
262#[derive(Serialize, Deserialize, Debug, Clone)]
263pub struct ColorMatch {
264    /// Lower color bounds. Required. Format depends on method.
265    #[serde(deserialize_with = "scalar_or_vec")]
266    pub lower: Vec<Vec<i32>>,
267    /// Upper color bounds. Required. Format depends on method.
268    #[serde(deserialize_with = "scalar_or_vec")]
269    pub upper: Vec<Vec<i32>>,
270    /// Recognition region. Default: \\[0,0,0,0\\] (full screen).
271    #[serde(default = "default_roi_zero")]
272    pub roi: Target,
273    /// Offset applied to the ROI.
274    #[serde(default)]
275    pub roi_offset: Rect,
276    /// Result sorting method. Default: "Horizontal".
277    #[serde(default = "default_order_by")]
278    pub order_by: String,
279    /// Color conversion code (cv::ColorConversionCodes). Default: 4 (RGB).
280    #[serde(default = "default_color_method")]
281    pub method: i32,
282    /// Minimum matching pixel count. Default: 1.
283    #[serde(default = "default_count_one")]
284    pub count: i32,
285    /// Which result to select. Default: 0.
286    #[serde(default)]
287    pub index: i32,
288    /// Only count connected pixels. Default: false.
289    #[serde(default)]
290    pub connected: bool,
291}
292
293/// Optical character recognition - finds and reads text.
294///
295/// Uses OCR model to detect and recognize text in the specified region.
296#[derive(Serialize, Deserialize, Debug, Clone, Default)]
297pub struct OCR {
298    /// Expected text patterns (supports regex). Default: match all.
299    #[serde(default, deserialize_with = "scalar_or_vec")]
300    pub expected: Vec<String>,
301    /// Recognition region. Default: \\[0,0,0,0\\] (full screen).
302    #[serde(default = "default_roi_zero")]
303    pub roi: Target,
304    /// Offset applied to the ROI.
305    #[serde(default)]
306    pub roi_offset: Rect,
307    /// Model confidence threshold. Default: 0.3.
308    #[serde(default = "default_ocr_threshold")]
309    pub threshold: f64,
310    /// Text replacement pairs [[from, to], ...] for fixing OCR errors.
311    #[serde(default)]
312    pub replace: Vec<Vec<String>>,
313    /// Result sorting method. Default: "Horizontal".
314    #[serde(default = "default_order_by")]
315    pub order_by: String,
316    /// Which result to select. Default: 0.
317    #[serde(default)]
318    pub index: i32,
319    /// Recognition only (skip detection, requires precise ROI). Default: false.
320    #[serde(default)]
321    pub only_rec: bool,
322    /// Model folder path relative to `model/ocr`. Default: root.
323    #[serde(default)]
324    pub model: String,
325    /// Color filter expression. Default: empty.
326    #[serde(default)]
327    pub color_filter: String,
328}
329
330/// Neural network classification - classifies fixed regions.
331///
332/// Uses ONNX model to classify images at fixed positions.
333#[derive(Serialize, Deserialize, Debug, Clone)]
334pub struct NeuralNetworkClassify {
335    /// Model file path relative to `model/classify`. Required.
336    pub model: String,
337    /// Expected class indices to match. Default: match all.
338    #[serde(default, deserialize_with = "scalar_or_vec")]
339    pub expected: Vec<i32>,
340    /// Recognition region. Default: \\[0,0,0,0\\] (full screen).
341    #[serde(default = "default_roi_zero")]
342    pub roi: Target,
343    /// Offset applied to the ROI.
344    #[serde(default)]
345    pub roi_offset: Rect,
346    /// Class labels for debugging. Default: "Unknown".
347    #[serde(default)]
348    pub labels: Vec<String>,
349    /// Result sorting method. Default: "Horizontal".
350    #[serde(default = "default_order_by")]
351    pub order_by: String,
352    /// Which result to select. Default: 0.
353    #[serde(default)]
354    pub index: i32,
355}
356
357/// Neural network detection - detects objects anywhere on screen.
358///
359/// Uses YOLO-style ONNX model to detect and locate objects.
360#[derive(Serialize, Deserialize, Debug, Clone)]
361pub struct NeuralNetworkDetect {
362    /// Model file path relative to `model/detect`. Required.
363    pub model: String,
364    /// Expected class indices to match. Default: match all.
365    #[serde(default, deserialize_with = "scalar_or_vec")]
366    pub expected: Vec<i32>,
367    /// Recognition region. Default: \\[0,0,0,0\\] (full screen).
368    #[serde(default = "default_roi_zero")]
369    pub roi: Target,
370    /// Offset applied to the ROI.
371    #[serde(default)]
372    pub roi_offset: Rect,
373    /// Class labels (auto-read from model metadata). Default: "Unknown".
374    #[serde(default)]
375    pub labels: Vec<String>,
376    /// Confidence threshold(s). Default: [0.3].
377    #[serde(
378        default = "default_detect_threshold",
379        deserialize_with = "scalar_or_vec"
380    )]
381    pub threshold: Vec<f64>,
382    /// Result sorting method. Default: "Horizontal".
383    #[serde(default = "default_order_by")]
384    pub order_by: String,
385    /// Which result to select. Default: 0.
386    #[serde(default)]
387    pub index: i32,
388}
389
390/// Custom recognition - uses user-registered recognition handler.
391///
392/// Invokes a handler registered via `MaaResourceRegisterCustomRecognition`.
393#[derive(Serialize, Deserialize, Debug, Clone)]
394pub struct CustomRecognition {
395    /// Handler name (as registered). Required.
396    pub custom_recognition: String,
397    /// Recognition region. Default: \\[0,0,0,0\\] (full screen).
398    #[serde(default = "default_roi_zero")]
399    pub roi: Target,
400    /// Offset applied to the ROI.
401    #[serde(default)]
402    pub roi_offset: Rect,
403    /// Custom parameters passed to the handler.
404    #[serde(default)]
405    pub custom_recognition_param: Value,
406}
407
408/// Logical AND recognition - all sub-recognitions must match.
409///
410/// Combines multiple recognitions; succeeds only when all match.
411#[derive(Serialize, Deserialize, Debug, Clone, Default)]
412pub struct And {
413    /// Sub-recognition list. All must match. Required.
414    #[serde(default)]
415    pub all_of: Vec<RecognitionRef>,
416    /// Which sub-recognition's bounding box to use. Default: 0.
417    #[serde(default)]
418    pub box_index: i32,
419}
420
421/// Logical OR recognition - first matching sub-recognition wins.
422///
423/// Combines multiple recognitions; succeeds when any one matches.
424#[derive(Serialize, Deserialize, Debug, Clone, Default)]
425pub struct Or {
426    /// Sub-recognition list. First match wins. Required.
427    #[serde(default)]
428    pub any_of: Vec<RecognitionRef>,
429}
430
431// --- Action Enums ---
432
433/// Action types executed after successful recognition.
434///
435/// - [`DoNothing`] - No action
436/// - [`Click`] - Tap/click
437/// - [`LongPress`] - Long press
438/// - [`Swipe`] - Linear swipe
439/// - [`MultiSwipe`] - Multi-touch swipe
440/// - Touch actions: `TouchDown`, `TouchMove`, [`TouchUp`]
441/// - Key actions: `ClickKey`, [`LongPressKey`], `KeyDown`, `KeyUp`
442/// - [`InputText`] - Text input
443/// - App control: `StartApp`, `StopApp`
444/// - [`StopTask`] - Stop current task
445/// - [`Scroll`] - Mouse wheel scroll
446/// - [`Command`] - Execute local command
447/// - [`Shell`] - Execute ADB shell command
448/// - `Custom` - User-defined action
449#[derive(Serialize, Deserialize, Debug, Clone)]
450#[serde(tag = "type", content = "param")]
451pub enum Action {
452    DoNothing(DoNothing),
453    Click(Click),
454    LongPress(LongPress),
455    Swipe(Swipe),
456    MultiSwipe(MultiSwipe),
457    TouchDown(Touch),
458    TouchMove(Touch),
459    TouchUp(TouchUp),
460    ClickKey(KeyList),
461    LongPressKey(LongPressKey),
462    KeyDown(SingleKey),
463    KeyUp(SingleKey),
464    InputText(InputText),
465    StartApp(App),
466    StopApp(App),
467    StopTask(StopTask),
468    Scroll(Scroll),
469    Command(Command),
470    Shell(Shell),
471    Custom(CustomAction),
472}
473
474// --- Action Structs ---
475
476/// Do nothing action.
477#[derive(Serialize, Deserialize, Debug, Clone, Default)]
478pub struct DoNothing {}
479
480/// Stop current task chain action.
481#[derive(Serialize, Deserialize, Debug, Clone, Default)]
482pub struct StopTask {}
483
484/// Click/tap action.
485///
486/// Performs a single tap at the target position.
487#[derive(Serialize, Deserialize, Debug, Clone, Default)]
488pub struct Click {
489    /// Click target position. Default: recognized position.
490    #[serde(default)]
491    pub target: Target,
492    /// Offset applied to target.
493    #[serde(default)]
494    pub target_offset: Rect,
495    /// Touch contact/button index. Default: 0.
496    #[serde(default)]
497    pub contact: i32,
498    /// Touch pressure. Default: 1.
499    #[serde(default = "default_pressure")]
500    pub pressure: i32,
501}
502
503/// Long press action.
504///
505/// Performs a sustained press at the target position.
506#[derive(Serialize, Deserialize, Debug, Clone)]
507pub struct LongPress {
508    /// Press target position. Default: recognized position.
509    #[serde(default)]
510    pub target: Target,
511    /// Offset applied to target.
512    #[serde(default)]
513    pub target_offset: Rect,
514    /// Press duration in milliseconds. Default: 1000.
515    #[serde(default = "default_long_press_duration")]
516    pub duration: i32,
517    /// Touch contact/button index. Default: 0.
518    #[serde(default)]
519    pub contact: i32,
520    /// Touch pressure. Default: 1.
521    #[serde(default = "default_pressure")]
522    pub pressure: i32,
523}
524
525/// Linear swipe action.
526///
527/// Swipes from begin to end position(s). Supports waypoints.
528#[derive(Serialize, Deserialize, Debug, Clone)]
529pub struct Swipe {
530    /// Start time offset in ms (for MultiSwipe). Default: 0.
531    #[serde(default)]
532    pub starting: i32,
533    /// Swipe start position. Default: recognized position.
534    #[serde(default)]
535    pub begin: Target,
536    /// Offset applied to begin.
537    #[serde(default)]
538    pub begin_offset: Rect,
539    /// Swipe end position(s). Supports waypoints. Default: recognized position.
540    #[serde(
541        default = "default_target_list_true",
542        deserialize_with = "scalar_or_vec"
543    )]
544    pub end: Vec<Target>,
545    /// Offset(s) applied to end.
546    #[serde(default = "default_rect_list_zero", deserialize_with = "scalar_or_vec")]
547    pub end_offset: Vec<Rect>,
548    /// Hold time at end position(s) in ms. Default: \\[0\\].
549    #[serde(default = "default_i32_list_zero", deserialize_with = "scalar_or_vec")]
550    pub end_hold: Vec<i32>,
551    /// Duration(s) in milliseconds. Default: \\[200\\].
552    #[serde(default = "default_duration_list", deserialize_with = "scalar_or_vec")]
553    pub duration: Vec<i32>,
554    /// Hover only (no press). Default: false.
555    #[serde(default)]
556    pub only_hover: bool,
557    /// Touch contact/button index. Default: 0.
558    #[serde(default)]
559    pub contact: i32,
560    /// Touch pressure. Default: 1.
561    #[serde(default = "default_pressure")]
562    pub pressure: i32,
563}
564
565/// Multi-finger swipe action.
566///
567/// Performs multiple simultaneous swipes (e.g., pinch gestures).
568#[derive(Serialize, Deserialize, Debug, Clone)]
569pub struct MultiSwipe {
570    /// List of swipe configurations.
571    #[serde(default)]
572    pub swipes: Vec<Swipe>,
573}
574
575/// Touch down/move action - initiates or moves a touch point.
576///
577/// Used for custom touch sequences. Pair with TouchUp to complete.
578#[derive(Serialize, Deserialize, Debug, Clone)]
579pub struct Touch {
580    /// Touch contact index. Default: 0.
581    #[serde(default)]
582    pub contact: i32,
583    /// Touch target position. Default: recognized position.
584    #[serde(default)]
585    pub target: Target,
586    /// Offset applied to target.
587    #[serde(default)]
588    pub target_offset: Rect,
589    /// Touch pressure. Default: 0.
590    #[serde(default)]
591    pub pressure: i32,
592}
593
594/// Touch up action - releases a touch point.
595#[derive(Serialize, Deserialize, Debug, Clone)]
596pub struct TouchUp {
597    /// Touch contact index to release. Default: 0.
598    #[serde(default)]
599    pub contact: i32,
600}
601
602/// Long press key action.
603#[derive(Serialize, Deserialize, Debug, Clone)]
604pub struct LongPressKey {
605    /// Virtual key code(s) to press. Required.
606    #[serde(deserialize_with = "scalar_or_vec")]
607    pub key: Vec<i32>,
608    /// Press duration in milliseconds. Default: 1000.
609    #[serde(default = "default_long_press_duration")]
610    pub duration: i32,
611}
612
613/// Click key action - single key press.
614#[derive(Serialize, Deserialize, Debug, Clone)]
615pub struct KeyList {
616    /// Virtual key code(s) to click. Required.
617    #[serde(deserialize_with = "scalar_or_vec")]
618    pub key: Vec<i32>,
619}
620
621/// Single key action - for KeyDown/KeyUp.
622#[derive(Serialize, Deserialize, Debug, Clone)]
623pub struct SingleKey {
624    /// Virtual key code. Required.
625    pub key: i32,
626}
627
628/// Text input action.
629#[derive(Serialize, Deserialize, Debug, Clone)]
630pub struct InputText {
631    /// Text to input (ASCII recommended). Required.
632    pub input_text: String,
633}
634
635/// App control action - for StartApp/StopApp.
636#[derive(Serialize, Deserialize, Debug, Clone)]
637pub struct App {
638    /// Package name or activity (e.g., "com.example.app"). Required.
639    pub package: String,
640}
641
642/// Mouse scroll action (Win32 only).
643#[derive(Serialize, Deserialize, Debug, Clone, Default)]
644pub struct Scroll {
645    /// Scroll target position. Default: recognized position.
646    #[serde(default)]
647    pub target: Target,
648    /// Offset applied to target.
649    #[serde(default)]
650    pub target_offset: Rect,
651    /// Horizontal scroll delta. Default: 0.
652    #[serde(default)]
653    pub dx: i32,
654    /// Vertical scroll delta. Default: 0.
655    #[serde(default)]
656    pub dy: i32,
657}
658
659/// Execute local command action.
660#[derive(Serialize, Deserialize, Debug, Clone)]
661pub struct Command {
662    /// Program path to execute. Required.
663    pub exec: String,
664    /// Command arguments. Supports runtime placeholders.
665    #[serde(default)]
666    pub args: Vec<String>,
667    /// Run in background (don't wait). Default: false.
668    #[serde(default)]
669    pub detach: bool,
670}
671
672/// Execute ADB shell command action.
673#[derive(Serialize, Deserialize, Debug, Clone)]
674pub struct Shell {
675    /// Shell command to execute. Required.
676    pub cmd: String,
677    /// Command timeout in milliseconds. Default: 20000.
678    #[serde(default = "default_timeout")]
679    pub shell_timeout: i32,
680}
681
682/// Custom action - uses user-registered action handler.
683///
684/// Invokes a handler registered via `MaaResourceRegisterCustomAction`.
685#[derive(Serialize, Deserialize, Debug, Clone)]
686pub struct CustomAction {
687    /// Handler name (as registered). Required.
688    pub custom_action: String,
689    /// Target position passed to handler. Default: recognized position.
690    #[serde(default)]
691    pub target: Target,
692    /// Custom parameters passed to the handler.
693    #[serde(default)]
694    pub custom_action_param: Value,
695    /// Offset applied to target.
696    #[serde(default)]
697    pub target_offset: Rect,
698}
699
700// --- Pipeline Data ---
701
702/// Complete pipeline node configuration.
703///
704/// Defines a node's recognition, action, and flow control parameters.
705#[derive(Serialize, Deserialize, Debug, Clone)]
706pub struct PipelineData {
707    /// Recognition algorithm configuration.
708    pub recognition: Recognition,
709    /// Action to execute on match.
710    pub action: Action,
711    /// Next nodes to check after action. Default: [].
712    #[serde(default)]
713    pub next: Vec<NodeAttr>,
714    /// Recognition rate limit in ms. Default: 1000.
715    #[serde(default = "default_rate_limit")]
716    pub rate_limit: i32,
717    /// Overall timeout in ms. Default: 20000.
718    #[serde(default = "default_timeout")]
719    pub timeout: i32,
720    /// Nodes to check on timeout/error. Default: [].
721    #[serde(default)]
722    pub on_error: Vec<NodeAttr>,
723    /// Anchor names for this node. Default: [].
724    #[serde(default)]
725    pub anchor: Anchor,
726    /// Invert recognition result. Default: false.
727    #[serde(default)]
728    pub inverse: bool,
729    /// Enable this node. Default: true.
730    #[serde(default = "default_enabled")]
731    pub enabled: bool,
732    /// Delay before action in ms. Default: 200.
733    #[serde(default = "default_pre_delay")]
734    pub pre_delay: i32,
735    /// Delay after action in ms. Default: 200.
736    #[serde(default = "default_post_delay")]
737    pub post_delay: i32,
738    /// Wait for screen stability before action.
739    #[serde(default)]
740    pub pre_wait_freezes: Option<WaitFreezes>,
741    /// Wait for screen stability after action.
742    #[serde(default)]
743    pub post_wait_freezes: Option<WaitFreezes>,
744    /// Action repeat count. Default: 1.
745    #[serde(default = "default_repeat")]
746    pub repeat: i32,
747    /// Delay between repeats in ms. Default: 0.
748    #[serde(default)]
749    pub repeat_delay: i32,
750    /// Wait for stability between repeats.
751    #[serde(default)]
752    pub repeat_wait_freezes: Option<WaitFreezes>,
753    /// Maximum successful hits. Default: UINT_MAX.
754    #[serde(default = "default_max_hit")]
755    pub max_hit: u32,
756    /// Focus flag for extra callbacks. Default: null.
757    #[serde(default)]
758    pub focus: Option<Value>,
759    /// Attached custom data (merged with defaults).
760    #[serde(default)]
761    pub attach: Option<Value>,
762}
763
764// --- Defaults Helper Functions ---
765
766fn default_wait_time() -> i32 {
767    1
768}
769fn default_wait_threshold() -> f64 {
770    0.95
771}
772fn default_wait_method() -> i32 {
773    5
774}
775fn default_rate_limit() -> i32 {
776    1000
777}
778fn default_timeout() -> i32 {
779    20000
780}
781fn default_threshold() -> Vec<f64> {
782    vec![0.7]
783}
784fn default_order_by() -> String {
785    "Horizontal".to_string()
786}
787fn default_template_method() -> i32 {
788    5
789}
790fn default_detector() -> String {
791    "SIFT".to_string()
792}
793fn default_feature_count() -> i32 {
794    4
795}
796fn default_feature_ratio() -> f64 {
797    0.6
798}
799fn default_color_method() -> i32 {
800    4
801} // RGB
802fn default_count_one() -> i32 {
803    1
804}
805fn default_ocr_threshold() -> f64 {
806    0.3
807}
808fn default_detect_threshold() -> Vec<f64> {
809    vec![0.3]
810}
811fn default_pressure() -> i32 {
812    1
813}
814fn default_long_press_duration() -> i32 {
815    1000
816}
817fn default_target_list_true() -> Vec<Target> {
818    vec![Target::Bool(true)]
819}
820fn default_rect_list_zero() -> Vec<Rect> {
821    vec![(0, 0, 0, 0).into()]
822}
823fn default_i32_list_zero() -> Vec<i32> {
824    vec![0]
825}
826fn default_duration_list() -> Vec<i32> {
827    vec![200]
828}
829fn default_enabled() -> bool {
830    true
831}
832fn default_pre_delay() -> i32 {
833    200
834}
835fn default_post_delay() -> i32 {
836    200
837}
838fn default_repeat() -> i32 {
839    1
840}
841fn default_max_hit() -> u32 {
842    u32::MAX
843}
844fn default_roi_zero() -> Target {
845    Target::Rect((0, 0, 0, 0).into())
846}