Skip to main content

maa_framework/
pipeline.rs

1//! Pipeline configuration types for recognition and action definitions.
2
3use serde::{de::DeserializeOwned, Deserialize, Deserializer, Serialize};
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}
326
327/// Neural network classification - classifies fixed regions.
328///
329/// Uses ONNX model to classify images at fixed positions.
330#[derive(Serialize, Deserialize, Debug, Clone)]
331pub struct NeuralNetworkClassify {
332    /// Model file path relative to `model/classify`. Required.
333    pub model: String,
334    /// Expected class indices to match. Default: match all.
335    #[serde(default, deserialize_with = "scalar_or_vec")]
336    pub expected: Vec<i32>,
337    /// Recognition region. Default: \\[0,0,0,0\\] (full screen).
338    #[serde(default = "default_roi_zero")]
339    pub roi: Target,
340    /// Offset applied to the ROI.
341    #[serde(default)]
342    pub roi_offset: Rect,
343    /// Class labels for debugging. Default: "Unknown".
344    #[serde(default)]
345    pub labels: Vec<String>,
346    /// Result sorting method. Default: "Horizontal".
347    #[serde(default = "default_order_by")]
348    pub order_by: String,
349    /// Which result to select. Default: 0.
350    #[serde(default)]
351    pub index: i32,
352}
353
354/// Neural network detection - detects objects anywhere on screen.
355///
356/// Uses YOLO-style ONNX model to detect and locate objects.
357#[derive(Serialize, Deserialize, Debug, Clone)]
358pub struct NeuralNetworkDetect {
359    /// Model file path relative to `model/detect`. Required.
360    pub model: String,
361    /// Expected class indices to match. Default: match all.
362    #[serde(default, deserialize_with = "scalar_or_vec")]
363    pub expected: Vec<i32>,
364    /// Recognition region. Default: \\[0,0,0,0\\] (full screen).
365    #[serde(default = "default_roi_zero")]
366    pub roi: Target,
367    /// Offset applied to the ROI.
368    #[serde(default)]
369    pub roi_offset: Rect,
370    /// Class labels (auto-read from model metadata). Default: "Unknown".
371    #[serde(default)]
372    pub labels: Vec<String>,
373    /// Confidence threshold(s). Default: [0.3].
374    #[serde(
375        default = "default_detect_threshold",
376        deserialize_with = "scalar_or_vec"
377    )]
378    pub threshold: Vec<f64>,
379    /// Result sorting method. Default: "Horizontal".
380    #[serde(default = "default_order_by")]
381    pub order_by: String,
382    /// Which result to select. Default: 0.
383    #[serde(default)]
384    pub index: i32,
385}
386
387/// Custom recognition - uses user-registered recognition handler.
388///
389/// Invokes a handler registered via `MaaResourceRegisterCustomRecognition`.
390#[derive(Serialize, Deserialize, Debug, Clone)]
391pub struct CustomRecognition {
392    /// Handler name (as registered). Required.
393    pub custom_recognition: String,
394    /// Recognition region. Default: \\[0,0,0,0\\] (full screen).
395    #[serde(default = "default_roi_zero")]
396    pub roi: Target,
397    /// Offset applied to the ROI.
398    #[serde(default)]
399    pub roi_offset: Rect,
400    /// Custom parameters passed to the handler.
401    #[serde(default)]
402    pub custom_recognition_param: Value,
403}
404
405/// Logical AND recognition - all sub-recognitions must match.
406///
407/// Combines multiple recognitions; succeeds only when all match.
408#[derive(Serialize, Deserialize, Debug, Clone, Default)]
409pub struct And {
410    /// Sub-recognition list. All must match. Required.
411    #[serde(default)]
412    pub all_of: Vec<RecognitionRef>,
413    /// Which sub-recognition's bounding box to use. Default: 0.
414    #[serde(default)]
415    pub box_index: i32,
416}
417
418/// Logical OR recognition - first matching sub-recognition wins.
419///
420/// Combines multiple recognitions; succeeds when any one matches.
421#[derive(Serialize, Deserialize, Debug, Clone, Default)]
422pub struct Or {
423    /// Sub-recognition list. First match wins. Required.
424    #[serde(default)]
425    pub any_of: Vec<RecognitionRef>,
426}
427
428// --- Action Enums ---
429
430/// Action types executed after successful recognition.
431///
432/// - [`DoNothing`] - No action
433/// - [`Click`] - Tap/click
434/// - [`LongPress`] - Long press
435/// - [`Swipe`] - Linear swipe
436/// - [`MultiSwipe`] - Multi-touch swipe
437/// - Touch actions: `TouchDown`, `TouchMove`, [`TouchUp`]
438/// - Key actions: `ClickKey`, [`LongPressKey`], `KeyDown`, `KeyUp`
439/// - [`InputText`] - Text input
440/// - App control: `StartApp`, `StopApp`
441/// - [`StopTask`] - Stop current task
442/// - [`Scroll`] - Mouse wheel scroll
443/// - [`Command`] - Execute local command
444/// - [`Shell`] - Execute ADB shell command
445/// - `Custom` - User-defined action
446#[derive(Serialize, Deserialize, Debug, Clone)]
447#[serde(tag = "type", content = "param")]
448pub enum Action {
449    DoNothing(DoNothing),
450    Click(Click),
451    LongPress(LongPress),
452    Swipe(Swipe),
453    MultiSwipe(MultiSwipe),
454    TouchDown(Touch),
455    TouchMove(Touch),
456    TouchUp(TouchUp),
457    ClickKey(KeyList),
458    LongPressKey(LongPressKey),
459    KeyDown(SingleKey),
460    KeyUp(SingleKey),
461    InputText(InputText),
462    StartApp(App),
463    StopApp(App),
464    StopTask(StopTask),
465    Scroll(Scroll),
466    Command(Command),
467    Shell(Shell),
468    Custom(CustomAction),
469}
470
471// --- Action Structs ---
472
473/// Do nothing action.
474#[derive(Serialize, Deserialize, Debug, Clone, Default)]
475pub struct DoNothing {}
476
477/// Stop current task chain action.
478#[derive(Serialize, Deserialize, Debug, Clone, Default)]
479pub struct StopTask {}
480
481/// Click/tap action.
482///
483/// Performs a single tap at the target position.
484#[derive(Serialize, Deserialize, Debug, Clone, Default)]
485pub struct Click {
486    /// Click target position. Default: recognized position.
487    #[serde(default)]
488    pub target: Target,
489    /// Offset applied to target.
490    #[serde(default)]
491    pub target_offset: Rect,
492    /// Touch contact/button index. Default: 0.
493    #[serde(default)]
494    pub contact: i32,
495    /// Touch pressure. Default: 1.
496    #[serde(default = "default_pressure")]
497    pub pressure: i32,
498}
499
500/// Long press action.
501///
502/// Performs a sustained press at the target position.
503#[derive(Serialize, Deserialize, Debug, Clone)]
504pub struct LongPress {
505    /// Press target position. Default: recognized position.
506    #[serde(default)]
507    pub target: Target,
508    /// Offset applied to target.
509    #[serde(default)]
510    pub target_offset: Rect,
511    /// Press duration in milliseconds. Default: 1000.
512    #[serde(default = "default_long_press_duration")]
513    pub duration: i32,
514    /// Touch contact/button index. Default: 0.
515    #[serde(default)]
516    pub contact: i32,
517    /// Touch pressure. Default: 1.
518    #[serde(default = "default_pressure")]
519    pub pressure: i32,
520}
521
522/// Linear swipe action.
523///
524/// Swipes from begin to end position(s). Supports waypoints.
525#[derive(Serialize, Deserialize, Debug, Clone)]
526pub struct Swipe {
527    /// Start time offset in ms (for MultiSwipe). Default: 0.
528    #[serde(default)]
529    pub starting: i32,
530    /// Swipe start position. Default: recognized position.
531    #[serde(default)]
532    pub begin: Target,
533    /// Offset applied to begin.
534    #[serde(default)]
535    pub begin_offset: Rect,
536    /// Swipe end position(s). Supports waypoints. Default: recognized position.
537    #[serde(
538        default = "default_target_list_true",
539        deserialize_with = "scalar_or_vec"
540    )]
541    pub end: Vec<Target>,
542    /// Offset(s) applied to end.
543    #[serde(default = "default_rect_list_zero", deserialize_with = "scalar_or_vec")]
544    pub end_offset: Vec<Rect>,
545    /// Hold time at end position(s) in ms. Default: \\[0\\].
546    #[serde(default = "default_i32_list_zero", deserialize_with = "scalar_or_vec")]
547    pub end_hold: Vec<i32>,
548    /// Duration(s) in milliseconds. Default: \\[200\\].
549    #[serde(default = "default_duration_list", deserialize_with = "scalar_or_vec")]
550    pub duration: Vec<i32>,
551    /// Hover only (no press). Default: false.
552    #[serde(default)]
553    pub only_hover: bool,
554    /// Touch contact/button index. Default: 0.
555    #[serde(default)]
556    pub contact: i32,
557    /// Touch pressure. Default: 1.
558    #[serde(default = "default_pressure")]
559    pub pressure: i32,
560}
561
562/// Multi-finger swipe action.
563///
564/// Performs multiple simultaneous swipes (e.g., pinch gestures).
565#[derive(Serialize, Deserialize, Debug, Clone)]
566pub struct MultiSwipe {
567    /// List of swipe configurations.
568    #[serde(default)]
569    pub swipes: Vec<Swipe>,
570}
571
572/// Touch down/move action - initiates or moves a touch point.
573///
574/// Used for custom touch sequences. Pair with TouchUp to complete.
575#[derive(Serialize, Deserialize, Debug, Clone)]
576pub struct Touch {
577    /// Touch contact index. Default: 0.
578    #[serde(default)]
579    pub contact: i32,
580    /// Touch target position. Default: recognized position.
581    #[serde(default)]
582    pub target: Target,
583    /// Offset applied to target.
584    #[serde(default)]
585    pub target_offset: Rect,
586    /// Touch pressure. Default: 0.
587    #[serde(default)]
588    pub pressure: i32,
589}
590
591/// Touch up action - releases a touch point.
592#[derive(Serialize, Deserialize, Debug, Clone)]
593pub struct TouchUp {
594    /// Touch contact index to release. Default: 0.
595    #[serde(default)]
596    pub contact: i32,
597}
598
599/// Long press key action.
600#[derive(Serialize, Deserialize, Debug, Clone)]
601pub struct LongPressKey {
602    /// Virtual key code(s) to press. Required.
603    #[serde(deserialize_with = "scalar_or_vec")]
604    pub key: Vec<i32>,
605    /// Press duration in milliseconds. Default: 1000.
606    #[serde(default = "default_long_press_duration")]
607    pub duration: i32,
608}
609
610/// Click key action - single key press.
611#[derive(Serialize, Deserialize, Debug, Clone)]
612pub struct KeyList {
613    /// Virtual key code(s) to click. Required.
614    #[serde(deserialize_with = "scalar_or_vec")]
615    pub key: Vec<i32>,
616}
617
618/// Single key action - for KeyDown/KeyUp.
619#[derive(Serialize, Deserialize, Debug, Clone)]
620pub struct SingleKey {
621    /// Virtual key code. Required.
622    pub key: i32,
623}
624
625/// Text input action.
626#[derive(Serialize, Deserialize, Debug, Clone)]
627pub struct InputText {
628    /// Text to input (ASCII recommended). Required.
629    pub input_text: String,
630}
631
632/// App control action - for StartApp/StopApp.
633#[derive(Serialize, Deserialize, Debug, Clone)]
634pub struct App {
635    /// Package name or activity (e.g., "com.example.app"). Required.
636    pub package: String,
637}
638
639/// Mouse scroll action (Win32 only).
640#[derive(Serialize, Deserialize, Debug, Clone, Default)]
641pub struct Scroll {
642    /// Scroll target position. Default: recognized position.
643    #[serde(default)]
644    pub target: Target,
645    /// Offset applied to target.
646    #[serde(default)]
647    pub target_offset: Rect,
648    /// Horizontal scroll delta. Default: 0.
649    #[serde(default)]
650    pub dx: i32,
651    /// Vertical scroll delta. Default: 0.
652    #[serde(default)]
653    pub dy: i32,
654}
655
656/// Execute local command action.
657#[derive(Serialize, Deserialize, Debug, Clone)]
658pub struct Command {
659    /// Program path to execute. Required.
660    pub exec: String,
661    /// Command arguments. Supports runtime placeholders.
662    #[serde(default)]
663    pub args: Vec<String>,
664    /// Run in background (don't wait). Default: false.
665    #[serde(default)]
666    pub detach: bool,
667}
668
669/// Execute ADB shell command action.
670#[derive(Serialize, Deserialize, Debug, Clone)]
671pub struct Shell {
672    /// Shell command to execute. Required.
673    pub cmd: String,
674    /// Command timeout in milliseconds. Default: 20000.
675    #[serde(default = "default_timeout")]
676    pub timeout: i32,
677}
678
679/// Custom action - uses user-registered action handler.
680///
681/// Invokes a handler registered via `MaaResourceRegisterCustomAction`.
682#[derive(Serialize, Deserialize, Debug, Clone)]
683pub struct CustomAction {
684    /// Handler name (as registered). Required.
685    pub custom_action: String,
686    /// Target position passed to handler. Default: recognized position.
687    #[serde(default)]
688    pub target: Target,
689    /// Custom parameters passed to the handler.
690    #[serde(default)]
691    pub custom_action_param: Value,
692    /// Offset applied to target.
693    #[serde(default)]
694    pub target_offset: Rect,
695}
696
697// --- Pipeline Data ---
698
699/// Complete pipeline node configuration.
700///
701/// Defines a node's recognition, action, and flow control parameters.
702#[derive(Serialize, Deserialize, Debug, Clone)]
703pub struct PipelineData {
704    /// Recognition algorithm configuration.
705    pub recognition: Recognition,
706    /// Action to execute on match.
707    pub action: Action,
708    /// Next nodes to check after action. Default: [].
709    #[serde(default)]
710    pub next: Vec<NodeAttr>,
711    /// Recognition rate limit in ms. Default: 1000.
712    #[serde(default = "default_rate_limit")]
713    pub rate_limit: i32,
714    /// Overall timeout in ms. Default: 20000.
715    #[serde(default = "default_timeout")]
716    pub timeout: i32,
717    /// Nodes to check on timeout/error. Default: [].
718    #[serde(default)]
719    pub on_error: Vec<NodeAttr>,
720    /// Anchor names for this node. Default: [].
721    #[serde(default)]
722    pub anchor: Anchor,
723    /// Invert recognition result. Default: false.
724    #[serde(default)]
725    pub inverse: bool,
726    /// Enable this node. Default: true.
727    #[serde(default = "default_enabled")]
728    pub enabled: bool,
729    /// Delay before action in ms. Default: 200.
730    #[serde(default = "default_pre_delay")]
731    pub pre_delay: i32,
732    /// Delay after action in ms. Default: 200.
733    #[serde(default = "default_post_delay")]
734    pub post_delay: i32,
735    /// Wait for screen stability before action.
736    #[serde(default)]
737    pub pre_wait_freezes: Option<WaitFreezes>,
738    /// Wait for screen stability after action.
739    #[serde(default)]
740    pub post_wait_freezes: Option<WaitFreezes>,
741    /// Action repeat count. Default: 1.
742    #[serde(default = "default_repeat")]
743    pub repeat: i32,
744    /// Delay between repeats in ms. Default: 0.
745    #[serde(default)]
746    pub repeat_delay: i32,
747    /// Wait for stability between repeats.
748    #[serde(default)]
749    pub repeat_wait_freezes: Option<WaitFreezes>,
750    /// Maximum successful hits. Default: UINT_MAX.
751    #[serde(default = "default_max_hit")]
752    pub max_hit: u32,
753    /// Focus flag for extra callbacks. Default: null.
754    #[serde(default)]
755    pub focus: Option<Value>,
756    /// Attached custom data (merged with defaults).
757    #[serde(default)]
758    pub attach: Option<Value>,
759}
760
761// --- Defaults Helper Functions ---
762
763fn default_wait_time() -> i32 {
764    1
765}
766fn default_wait_threshold() -> f64 {
767    0.95
768}
769fn default_wait_method() -> i32 {
770    5
771}
772fn default_rate_limit() -> i32 {
773    1000
774}
775fn default_timeout() -> i32 {
776    20000
777}
778fn default_threshold() -> Vec<f64> {
779    vec![0.7]
780}
781fn default_order_by() -> String {
782    "Horizontal".to_string()
783}
784fn default_template_method() -> i32 {
785    5
786}
787fn default_detector() -> String {
788    "SIFT".to_string()
789}
790fn default_feature_count() -> i32 {
791    4
792}
793fn default_feature_ratio() -> f64 {
794    0.6
795}
796fn default_color_method() -> i32 {
797    4
798} // RGB
799fn default_count_one() -> i32 {
800    1
801}
802fn default_ocr_threshold() -> f64 {
803    0.3
804}
805fn default_detect_threshold() -> Vec<f64> {
806    vec![0.3]
807}
808fn default_pressure() -> i32 {
809    1
810}
811fn default_long_press_duration() -> i32 {
812    1000
813}
814fn default_target_list_true() -> Vec<Target> {
815    vec![Target::Bool(true)]
816}
817fn default_rect_list_zero() -> Vec<Rect> {
818    vec![(0, 0, 0, 0).into()]
819}
820fn default_i32_list_zero() -> Vec<i32> {
821    vec![0]
822}
823fn default_duration_list() -> Vec<i32> {
824    vec![200]
825}
826fn default_enabled() -> bool {
827    true
828}
829fn default_pre_delay() -> i32 {
830    200
831}
832fn default_post_delay() -> i32 {
833    200
834}
835fn default_repeat() -> i32 {
836    1
837}
838fn default_max_hit() -> u32 {
839    u32::MAX
840}
841fn default_roi_zero() -> Target {
842    Target::Rect((0, 0, 0, 0).into())
843}