Skip to main content

cradle_plugin_api/
checks.rs

1use cradle_shared::CheckResult;
2use std::ffi::c_void;
3
4/// Result struct for each plugin
5/// Specifies the plugin name and a vector of the results
6pub struct PluginResults {
7    plugin: &'static str,
8    results: Vec<CheckResult>,
9}
10
11impl PluginResults {
12    /// Creates a new [`PluginResults`] object
13    pub fn new(plugin: &'static str) -> Self {
14        Self {
15            plugin,
16            results: Vec::new(),
17        }
18    }
19    /// Helper to create a passing result check
20    pub fn pass(&mut self, check: &str, msg: impl Into<String>) {
21        self.results
22            .push(CheckResult::pass(self.plugin, check, msg));
23    }
24
25    /// Helper to create a warning result check
26    pub fn warn(&mut self, check: &str, msg: impl Into<String>) {
27        self.results
28            .push(CheckResult::warn(self.plugin, check, msg));
29    }
30
31    /// Helper to create a failure result check
32    pub fn fail(&mut self, check: &str, msg: impl Into<String>) {
33        self.results
34            .push(CheckResult::fail(self.plugin, check, msg));
35    }
36
37    /// Helper to create an information result check
38    pub fn info(&mut self, check: &str, msg: impl Into<String>) {
39        self.results
40            .push(CheckResult::info(self.plugin, check, msg));
41    }
42
43    /// Helper to create a warning result check with details
44    pub fn warn_with(&mut self, check: &str, msg: impl Into<String>, details: impl Into<String>) {
45        self.results
46            .push(CheckResult::warn(self.plugin, check, msg).with_details(details));
47    }
48
49    /// Helper to create a failure result check with details
50    pub fn fail_with(&mut self, check: &str, msg: impl Into<String>, details: impl Into<String>) {
51        self.results
52            .push(CheckResult::fail(self.plugin, check, msg).with_details(details));
53    }
54
55    /// Pushes a result into the array
56    pub fn push(&mut self, result: CheckResult) {
57        self.results.push(result);
58    }
59
60    /// Takes ownership of the stored results and returns them
61    pub fn take(&mut self) -> Vec<CheckResult> {
62        std::mem::take(&mut self.results)
63    }
64
65    /// Returns the length of the inner result array
66    pub fn len(&self) -> usize {
67        self.results.len()
68    }
69
70    /// Returns true if the inner results array is empty
71    pub fn is_empty(&self) -> bool {
72        self.results.is_empty()
73    }
74}
75
76/// Events emitted by the agent during execution
77#[non_exhaustive]
78pub enum AgentEvent<'a> {
79    /// A Process was created
80    ProcessCreated {
81        /// The PID of the created process
82        pid: u32,
83        /// The process handle
84        process: *mut c_void,
85        /// Path of the process
86        target_path: &'a str,
87        /// Bytes of the process
88        target_bytes: &'a [u8],
89    },
90    /// A DLL has been loaded
91    DllLoaded {
92        /// Name of the DLL
93        name: &'a str,
94        /// Base address
95        base: usize,
96    },
97    /// First breakpoint to be triggered
98    InitialBreakpoint,
99    /// A Thread has been created
100    ThreadCreated {
101        /// Thread ID
102        tid: u32,
103        /// Thread handle
104        handle: *mut c_void,
105    },
106    /// Process finished executing
107    Finished,
108    /// Thread finished executing
109    ThreadFinished {
110        /// Thread ID
111        tid: u32,
112    },
113    /// Process emitted a debug string
114    DebugString {
115        /// Debug string value
116        string: &'a str,
117    },
118    /// Cradle agent has finished initializing
119    AgentInitialized {
120        /// Target process path
121        target_path: &'a str,
122    },
123    /// Cradle agent has finished executing
124    AgentFinished,
125    /// Unknown event
126    Unknown,
127}