1use crate::error::{BrowserError, Result};
9use crate::page::ObservedNode;
10use crate::policy::ActionKind;
11use serde::Serialize;
12use std::collections::hash_map::DefaultHasher;
13use std::hash::{Hash, Hasher};
14
15pub const DEFAULT_ACTION_LOOP_LIMIT: u32 = 5;
17
18#[derive(Clone, Debug, PartialEq)]
20pub enum RefAction {
21 Click,
22 DoubleClick,
23 Hover,
24 Fill {
26 text: String,
27 clear: bool,
28 },
29 SelectOption {
31 value: String,
32 },
33}
34
35impl RefAction {
36 pub(crate) fn admission(&self) -> (ActionKind, Option<String>) {
38 match self {
39 RefAction::Click => (ActionKind::Click, None),
40 RefAction::DoubleClick => (ActionKind::DoubleClick, None),
41 RefAction::Hover => (ActionKind::Hover, None),
42 RefAction::Fill { text, clear } => (
43 ActionKind::Fill,
44 Some(format!("chars={} clear={clear}", text.chars().count())),
45 ),
46 RefAction::SelectOption { value } => {
47 (ActionKind::Select, Some(format!("value={value}")))
48 }
49 }
50 }
51
52 pub(crate) fn watch_key(&self, node_ref: &str) -> String {
54 let what = match self {
55 RefAction::Click => "click".to_string(),
56 RefAction::DoubleClick => "double_click".to_string(),
57 RefAction::Hover => "hover".to_string(),
58 RefAction::Fill { text, clear } => format!("fill:{clear}:{}", hash_of(text)),
59 RefAction::SelectOption { value } => format!("select:{}", hash_of(value)),
60 };
61 format!("{node_ref}|{what}")
62 }
63}
64
65fn hash_of(text: &str) -> u64 {
66 let mut h = DefaultHasher::new();
67 text.hash(&mut h);
68 h.finish()
69}
70
71#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
73pub struct NodeState {
74 pub role: String,
75 pub name: String,
76 pub visible: bool,
77 pub disabled: bool,
78}
79
80impl NodeState {
81 pub(crate) fn of(node: &ObservedNode) -> NodeState {
82 NodeState {
83 role: node.role.clone(),
84 name: node.name.clone(),
85 visible: node.visible,
86 disabled: node.disabled,
87 }
88 }
89}
90
91#[derive(Clone, Debug, Serialize)]
93pub struct NodeChange {
94 pub node_ref: String,
96 pub selector: String,
97 pub before: NodeState,
98 pub after: NodeState,
99}
100
101#[derive(Clone, Debug, Serialize)]
104pub struct ObservationDiff {
105 pub epoch_before: u64,
106 pub epoch_after: u64,
107 pub url_before: String,
108 pub url_after: String,
109 pub document_replaced: bool,
111 pub added: Vec<ObservedNode>,
113 pub removed: Vec<String>,
115 pub changed: Vec<NodeChange>,
116}
117
118impl ObservationDiff {
119 pub fn is_empty(&self) -> bool {
121 !self.document_replaced
122 && self.url_before == self.url_after
123 && self.added.is_empty()
124 && self.removed.is_empty()
125 && self.changed.is_empty()
126 }
127}
128
129#[derive(Clone, Debug, Serialize)]
131pub struct ActResult {
132 pub page_id: String,
133 pub node_ref: String,
135 pub epoch: u64,
138 pub diff: ObservationDiff,
139 pub repeats: u32,
141}
142
143#[derive(Debug)]
145pub(crate) struct ActionWatch {
146 limit: u32,
147 last: Option<String>,
148 repeats: u32,
149 stopped: bool,
150}
151
152impl Default for ActionWatch {
153 fn default() -> Self {
154 ActionWatch {
155 limit: DEFAULT_ACTION_LOOP_LIMIT,
156 last: None,
157 repeats: 0,
158 stopped: false,
159 }
160 }
161}
162
163impl ActionWatch {
164 pub(crate) fn set_limit(&mut self, limit: u32) -> Result<()> {
166 if limit == 0 {
167 return Err(BrowserError::Invalid(
168 "the action-loop limit must be at least 1".into(),
169 ));
170 }
171 self.limit = limit;
172 Ok(())
173 }
174
175 pub(crate) fn reset(&mut self) {
177 self.last = None;
178 self.repeats = 0;
179 self.stopped = false;
180 }
181
182 pub(crate) fn before(&mut self, key: &str) -> Result<()> {
185 if self.stopped {
186 return Err(BrowserError::ActionLoop(format!(
187 "the run was stopped after {} identical actions with no effect",
188 self.limit
189 )));
190 }
191 if self.last.as_deref() == Some(key) && self.repeats >= self.limit {
192 self.stopped = true;
193 return Err(BrowserError::ActionLoop(format!(
194 "the same action on the same element ran {} times with no effect",
195 self.repeats
196 )));
197 }
198 Ok(())
199 }
200
201 pub(crate) fn after(&mut self, key: String, no_effect: bool) -> u32 {
204 if !no_effect {
205 self.last = None;
206 self.repeats = 0;
207 } else if self.last.as_deref() == Some(key.as_str()) {
208 self.repeats = self.repeats.saturating_add(1);
209 } else {
210 self.last = Some(key);
211 self.repeats = 1;
212 }
213 self.repeats
214 }
215}