maa_framework/lib.rs
1//! > **Rust bindings for MaaFramework**
2//!
3//! This crate provides Rust bindings for [MaaFramework](https://github.com/MaaAssistantArknights/MaaFramework).
4//! On top of the raw bindings generated by bindgen, we provide a safe and more rust-friendly wrapper for use.
5//!
6//! ## Pre-requisites
7//!
8//! This crate utilizes cmake to find and link to the MaaFramework library. You will need to have cmake installed on your system and make sure that MaaFramework is installed in a place where cmake can find it.
9//! In addition, you will also need the MaaFramework library installed on your system to run any tests or binaries that use this crate.
10//!
11//! ## Usage
12//!
13//! Refer to the concerning struct for usage examples. Furthermore, you should check the MaaFramework repo to get a more in-depth understanding of the API.
14//!
15//! If you have no idea where to start, you can check the [instance] module for starter.
16//!
17//! ## Features
18//!
19//! - `internal`: Enable internal API for MaaFramework. This enables you to directly access the raw bindings.
20//! - `toolkit`: Enable MaaToolkit.
21//! - `sync_context`: Enable sync context for MaaFramework.
22//! - `adb`: Enable adb controller for MaaFramework.
23//! - `win32`: Enable win32 controller for MaaFramework.
24//! - `dbg`: Enable debug controller for MaaFramework. This is most likely not needed.
25//! - `custom_recognizer`: Enable custom recognizer for MaaFramework.
26//! - `custom_controller`: Enable custom controller for MaaFramework.
27//! - `custom_action`: Enable custom action for MaaFramework.
28//! - `custom`: Enable all custom features for MaaFramework.
29//!
30//! The default features include all features so you might want to disable some of them if you don't need them.
31
32#![feature(doc_cfg)]
33
34use msg::MaaMsg;
35use serde::{Deserialize, Serialize};
36
37#[cfg(feature = "internal")]
38#[doc(cfg(feature = "internal"))]
39pub mod internal;
40
41#[cfg(not(feature = "internal"))]
42mod internal;
43
44#[cfg(feature = "toolkit")]
45#[doc(cfg(feature = "toolkit"))]
46pub mod toolkit;
47
48pub mod custom;
49
50#[cfg(feature = "sync_context")]
51#[doc(cfg(feature = "sync_context"))]
52pub mod sync_context;
53
54pub mod buffer;
55pub mod controller;
56pub mod diff_task;
57pub mod error;
58pub mod instance;
59pub mod msg;
60pub mod resource;
61pub mod utility;
62
63use error::Error;
64
65pub type MaaResult<T> = Result<T, error::Error>;
66
67#[derive(Debug, Serialize, Deserialize)]
68pub enum MaaStatus {
69 Invalid,
70 Pending,
71 Running,
72 Success,
73 Failed,
74}
75
76impl TryFrom<internal::MaaStatus> for MaaStatus {
77 type Error = error::Error;
78
79 fn try_from(status: internal::MaaStatus) -> Result<Self, Self::Error> {
80 match status {
81 internal::MaaStatusEnum_MaaStatus_Invalid => Ok(MaaStatus::Invalid),
82 internal::MaaStatusEnum_MaaStatus_Pending => Ok(MaaStatus::Pending),
83 internal::MaaStatusEnum_MaaStatus_Running => Ok(MaaStatus::Running),
84 internal::MaaStatusEnum_MaaStatus_Success => Ok(MaaStatus::Success),
85 internal::MaaStatusEnum_MaaStatus_Failed => Ok(MaaStatus::Failed),
86 _ => Err(error::Error::MaaStatusConversionError(status)),
87 }
88 }
89}
90
91/// The callback handler trait.
92///
93/// This trait is used to handle the callback from MaaFramework.
94pub trait CallbackHandler {
95 fn handle(&mut self, msg: MaaMsg);
96}
97
98pub fn maa_version() -> String {
99 let version = unsafe { internal::MaaVersion() };
100
101 string!(version)
102}