Skip to main content

Context

Struct Context 

Source
pub struct Context { /* private fields */ }
Expand description

Represents the execution context of a task.

This struct provides an interface for interacting with the current task’s runtime state. Capabilities include:

  • Executing sub-tasks.
  • Overriding pipeline configurations dynamically.
  • Performing direct recognition and actions.
  • Managing node hit counts and control flow anchors.

§Safety

Context is a wrapper around a non-owning pointer (MaaContext). The underlying resources are managed by the Tasker. Users must ensure the Context does not outlive the validity of the underlying task or callback scope.

Implementations§

Source§

impl Context

Source

pub fn run_task(&self, entry: &str, pipeline_override: &str) -> MaaResult<i64>

Submits a new task for execution.

§Arguments
  • entry - The name of the task entry point.
  • pipeline_override - A JSON string specifying pipeline parameter overrides.
§Returns

Returns the job ID (MaaId) associated with the submitted task.

Source

pub fn override_pipeline(&self, override_json: &str) -> MaaResult<()>

Overrides pipeline parameters for the current context.

§Arguments
  • override_json - A JSON string containing the parameters to override.
Source

pub fn raw(&self) -> *mut MaaContext

Returns the underlying raw MaaContext pointer.

Useful for advanced FFI interop scenarios.

Source

pub fn run_recognition( &self, entry: &str, pipeline_override: &str, image: &MaaImageBuffer, ) -> MaaResult<i64>

Runs a specific recognition task with an input image.

§Arguments
  • entry - The task entry name.
  • pipeline_override - A JSON string for parameter overrides.
  • image - The input image buffer.
§Returns

Returns the job ID associated with the recognition task.

Source

pub fn run_recognition_direct( &self, reco_type: &str, reco_param: &str, image: &MaaImageBuffer, ) -> MaaResult<Option<RecognitionDetail>>

Performs a direct recognition operation.

This executes a specific recognition algorithm immediately, bypassing the pipeline structure.

§Arguments
  • reco_type - The specific recognition algorithm type (e.g., “TemplateMatch”, “OCR”).
  • reco_param - A JSON string containing the recognition parameters.
  • image - The image buffer to perform recognition on.
§Returns

Returns Some(RecognitionDetail) if successful, or None if the operation failed to initiate or yielded no result.

Source

pub fn run_action( &self, entry: &str, pipeline_override: &str, box_rect: &Rect, reco_detail: &str, ) -> MaaResult<i64>

Runs a specific action with context parameters.

§Arguments
  • entry - The task entry name.
  • pipeline_override - A JSON string for parameter overrides.
  • box_rect - The target region for the action.
  • reco_detail - A string containing details from a previous recognition step.
§Returns

Returns the job ID associated with the action.

Source

pub fn run_action_direct( &self, action_type: &str, action_param: &str, box_rect: &Rect, reco_detail: &str, ) -> MaaResult<Option<ActionDetail>>

Performs a direct action operation.

This executes a specific action immediately, bypassing the pipeline structure.

§Arguments
  • action_type - The type of action to perform (e.g., “Click”, “Swipe”).
  • action_param - A JSON string containing the action parameters.
  • box_rect - The target region for the action (e.g., derived from recognition results).
  • reco_detail - Contextual details from a previous recognition step.
§Returns

Returns Some(ActionDetail) on success, or None if the operation failed.

Source

pub fn override_next( &self, node_name: &str, next_list: &[&str], ) -> MaaResult<bool>

Overrides the execution list for a specific node.

§Arguments
  • node_name - The name of the target node.
  • next_list - A slice of strings representing the new next list. Supports special signals like [JumpBack] and [Anchor].
§Returns
  • Ok(true) if the override was successful.
  • Ok(false) if the operation failed.
Source

pub fn get_node_data(&self, node_name: &str) -> MaaResult<Option<String>>

Retrieves data associated with a specific node.

§Arguments
  • node_name - The name of the node.
§Returns

Returns the node data as a String if available, or None otherwise.

Source

pub fn get_node_object( &self, node_name: &str, ) -> MaaResult<Option<PipelineData>>

Retrieves and deserializes the object associated with a node.

This is a convenience wrapper around get_node_data that parses the result into PipelineData.

§Arguments
  • node_name - The name of the node.
§Errors

Returns MaaError::InvalidConfig if the data cannot be parsed.

Source

pub fn task_id(&self) -> MaaId

Returns the ID of the current task.

Source

pub fn set_anchor(&self, anchor_name: &str, node_name: &str) -> MaaResult<()>

Associates an anchor with a specific node.

§Arguments
  • anchor_name - The name of the anchor.
  • node_name - The name of the target node.
Source

pub fn get_anchor(&self, anchor_name: &str) -> MaaResult<Option<String>>

Retrieves the node name associated with an anchor.

§Arguments
  • anchor_name - The name of the anchor.
§Returns

Returns the node name as a String if the anchor exists.

Source

pub fn get_hit_count(&self, node_name: &str) -> MaaResult<u64>

Retrieves the hit count for a specific node.

Hit count tracks how many times a node has been executed.

§Arguments
  • node_name - The name of the node to query.
§Returns

The number of times the node has been executed, or 0 if not found.

Source

pub fn clear_hit_count(&self, node_name: &str) -> MaaResult<()>

Resets the hit count for a specific node to zero.

§Arguments
  • node_name - The name of the node to reset.
Source

pub fn clone_context(&self) -> MaaResult<Self>

Creates a clone of the current context.

The new context can be used for independent execution threads, preventing state interference with the original context.

Source

pub fn tasker_handle(&self) -> *mut MaaTasker

Returns the raw handle to the associated Tasker instance.

§Safety

The returned pointer is owned by the framework. The caller must not attempt to destroy or free it.

Source

pub fn override_image( &self, image_name: &str, image: &MaaImageBuffer, ) -> MaaResult<()>

Overrides a global image resource with a provided buffer.

§Arguments
  • image_name - The identifier of the image to override.
  • image - The new image data buffer.
Source

pub fn get_task_job<'a>(&'a self) -> JobWithResult<'a, TaskDetail>

Retrieves a job handle representing the current task’s execution state.

The returned job is bound to the lifetime of the Context to ensure safety.

Source

pub fn clear_all_hit_counts(&self) -> MaaResult<()>

Resets hit counts for all nodes in the context.

This clears the execution history for all nodes, resetting their hit counts to zero.

Trait Implementations§

Source§

impl Debug for Context

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Send for Context

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.