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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
use std::{fmt, ops::Deref, time::Duration}; use serde::{Deserialize, Serialize}; #[derive(Debug, Clone, Serialize, Deserialize)] pub struct ElementRect { pub x: f32, pub y: f32, pub width: f32, pub height: f32, } impl ElementRect { pub fn center(&self) -> (f32, f32) { (self.x + (self.width / 2.0), self.y + (self.height / 2.0)) } pub fn icenter(&self) -> (i32, i32) { let c = self.center(); (c.0 as i32, c.1 as i32) } } #[derive(Deserialize)] pub struct ElementRef { #[serde(rename(deserialize = "element-6066-11e4-a52e-4f735466cecf"))] pub id: String, } #[derive(Debug, Clone)] pub struct SessionId { id: String, } impl SessionId { pub fn null() -> Self { SessionId { id: String::new() } } } impl Deref for SessionId { type Target = String; fn deref(&self) -> &Self::Target { &self.id } } impl<S> From<S> for SessionId where S: Into<String>, { fn from(value: S) -> Self { SessionId { id: value.into() } } } impl fmt::Display for SessionId { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.id) } } #[derive(Debug, Clone, Serialize, PartialEq)] #[serde(transparent)] pub struct ElementId { id: String, } impl<S> From<S> for ElementId where S: Into<String>, { fn from(value: S) -> Self { ElementId { id: value.into() } } } impl fmt::Display for ElementId { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.id) } } #[derive(Debug, Clone)] pub struct WindowHandle { handle: String, } impl<S> From<S> for WindowHandle where S: Into<String>, { fn from(value: S) -> Self { WindowHandle { handle: value.into(), } } } impl fmt::Display for WindowHandle { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.handle) } } #[derive(Debug, Clone)] pub enum WindowType { Tab, Window, } impl fmt::Display for WindowType { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!( f, "{}", match self { WindowType::Tab => "tab", WindowType::Window => "window", } ) } } #[derive(Debug, Clone, Serialize, Deserialize)] pub struct Rect { x: i32, y: i32, width: i32, height: i32, } impl Rect { pub fn new(x: i32, y: i32, width: i32, height: i32) -> Self { Rect { x, y, width, height, } } } #[derive(Debug, Default, Clone, Serialize)] pub struct OptionRect { pub x: Option<i32>, pub y: Option<i32>, pub width: Option<i32>, pub height: Option<i32>, } impl OptionRect { pub fn new() -> Self { Default::default() } pub fn with_x(mut self, value: i32) -> Self { self.x = Some(value); self } pub fn with_y(mut self, value: i32) -> Self { self.y = Some(value); self } pub fn with_width(mut self, value: i32) -> Self { self.width = Some(value); self } pub fn with_height(mut self, value: i32) -> Self { self.height = Some(value); self } pub fn with_pos(mut self, x: i32, y: i32) -> Self { self.x = Some(x); self.y = Some(y); self } pub fn with_size(mut self, width: i32, height: i32) -> Self { self.width = Some(width); self.height = Some(height); self } } impl From<Rect> for OptionRect { fn from(value: Rect) -> Self { OptionRect { x: Some(value.x), y: Some(value.y), width: Some(value.width), height: Some(value.height), } } } #[derive(Debug, Clone, Serialize, Deserialize)] pub struct TimeoutConfiguration { #[serde(skip_serializing_if = "Option::is_none")] script: Option<u64>, #[serde(rename = "pageLoad", skip_serializing_if = "Option::is_none")] page_load: Option<u64>, #[serde(skip_serializing_if = "Option::is_none")] implicit: Option<u64>, } impl TimeoutConfiguration { pub fn new( script: Option<Duration>, page_load: Option<Duration>, implicit: Option<Duration>, ) -> Self { TimeoutConfiguration { script: script.map(|x| x.as_millis() as u64), page_load: page_load.map(|x| x.as_millis() as u64), implicit: implicit.map(|x| x.as_millis() as u64), } } pub fn script(&self) -> Option<Duration> { self.script.map(Duration::from_millis) } pub fn set_script(&mut self, timeout: Option<Duration>) { self.script = timeout.map(|x| x.as_millis() as u64); } pub fn page_load(&self) -> Option<Duration> { self.page_load.map(Duration::from_millis) } pub fn set_page_load(&mut self, timeout: Option<Duration>) { self.page_load = timeout.map(|x| x.as_millis() as u64); } pub fn implicit(&self) -> Option<Duration> { self.implicit.map(Duration::from_millis) } pub fn set_implicit(&mut self, timeout: Option<Duration>) { self.implicit = timeout.map(|x| x.as_millis() as u64); } }