-
Notifications
You must be signed in to change notification settings - Fork 102
/
agent.rs
113 lines (109 loc) · 4.72 KB
/
agent.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
use crate::{Axis, Button, Coordinate, Direction, Enigo, InputResult, Key, Keyboard, Mouse};
use log::error;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Token {
/// Call the [`Keyboard::text`] fn with the string as text
#[cfg_attr(feature = "serde", serde(alias = "T"))]
#[cfg_attr(feature = "serde", serde(alias = "t"))]
Text(String),
/// Call the [`Keyboard::key`] fn with the given key and direction
#[cfg_attr(feature = "serde", serde(alias = "K"))]
#[cfg_attr(feature = "serde", serde(alias = "k"))]
Key(
Key,
#[cfg_attr(feature = "serde", serde(default))] Direction,
),
/// Call the [`Keyboard::raw`] fn with the given keycode and direction
#[cfg_attr(feature = "serde", serde(alias = "R"))]
#[cfg_attr(feature = "serde", serde(alias = "r"))]
Raw(
u16,
#[cfg_attr(feature = "serde", serde(default))] Direction,
),
/// Call the [`Mouse::button`] fn with the given mouse button and direction
#[cfg_attr(feature = "serde", serde(alias = "B"))]
#[cfg_attr(feature = "serde", serde(alias = "b"))]
Button(
Button,
#[cfg_attr(feature = "serde", serde(default))] Direction,
),
/// Call the [`Mouse::move_mouse`] fn. The first i32 is the value to move on
/// the x-axis and the second i32 is the value to move on the y-axis. The
/// coordinate defines if the given coordinates are absolute of relative to
/// the current position of the mouse.
#[cfg_attr(feature = "serde", serde(alias = "M"))]
#[cfg_attr(feature = "serde", serde(alias = "m"))]
MoveMouse(
i32,
i32,
#[cfg_attr(feature = "serde", serde(default))] Coordinate,
),
/// Call the [`Mouse::scroll`] fn.
#[cfg_attr(feature = "serde", serde(alias = "S"))]
#[cfg_attr(feature = "serde", serde(alias = "s"))]
Scroll(i32, #[cfg_attr(feature = "serde", serde(default))] Axis),
/// Call the [`Mouse::location`] fn and compare the return values with
/// the values of this enum. Log an error if they are not equal.
/// This variant contains the EXPECTED location of the mouse
#[cfg_attr(feature = "serde", serde(alias = "L"))]
#[cfg_attr(feature = "serde", serde(alias = "l"))]
Location(i32, i32),
/// Call the [`Mouse::main_display`] fn and compare the return values with
/// the values of this enum. Log an error if they are not equal.
/// This variant contains the EXPECTED size of the main display
#[cfg_attr(feature = "serde", serde(alias = "D"))]
#[cfg_attr(feature = "serde", serde(alias = "d"))]
MainDisplay(i32, i32),
}
pub trait Agent
where
Self: Keyboard,
Self: Mouse,
{
/// Execute the action associated with the token. A [`Token::Text`] will
/// enter text, a [`Token::Scroll`] will scroll and so forth. Have a look at
/// the documentation of the [`Token`] enum for more information.
///
/// # Errors
///
/// Same as the individual functions. Have a look at [`InputResult`] for a
/// list of possible errors
fn execute(&mut self, token: &Token) -> InputResult<()> {
match token {
Token::Text(text) => self.text(text),
Token::Key(key, direction) => self.key(*key, *direction),
Token::Raw(keycode, direction) => self.raw(*keycode, *direction),
Token::Button(button, direction) => self.button(*button, *direction),
Token::MoveMouse(x, y, coordinate) => self.move_mouse(*x, *y, *coordinate),
Token::Scroll(length, axis) => self.scroll(*length, *axis),
Token::Location(expected_x, expected_y) => match self.location() {
Ok((actual_x, actual_y)) => {
if actual_x != *expected_x || actual_y != *expected_y {
error!("The mouse is not at the expected location");
}
Ok(())
}
Err(e) => {
error!("There was an error getting the location of the mouse");
Err(e)
}
},
Token::MainDisplay(expected_width, expected_height) => match self.main_display() {
Ok((actual_x, actual_y)) => {
if actual_x != *expected_width || actual_y != *expected_height {
error!("The size of the main display is not what was expected");
}
Ok(())
}
Err(e) => {
error!("There was an error getting the size of the main display");
Err(e)
}
},
}
}
}
impl Agent for Enigo {}