thirtyfour/common/
types.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
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
use crate::{ElementRef, WebElement};
use futures::future::BoxFuture;
use std::{fmt, ops::Deref};

use crate::error::WebDriverResult;
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ElementRect {
    pub x: f64,
    pub y: f64,
    pub width: f64,
    pub height: f64,
}

impl ElementRect {
    pub fn icenter(&self) -> (i64, i64) {
        (self.x as i64 + (self.width / 2.0) as i64, self.y as i64 + (self.height / 2.0) as i64)
    }

    pub fn center(&self) -> (f64, f64) {
        (self.x + (self.width / 2.0), self.y + (self.height / 2.0))
    }
}

/// Helper to Deserialize ElementRef from JSON Value.
#[derive(Debug, Deserialize)]
#[serde(untagged)]
pub enum ElementRefHelper {
    Element {
        #[serde(rename(deserialize = "element-6066-11e4-a52e-4f735466cecf"))]
        id: String,
    },
    ShadowElement {
        #[serde(rename(deserialize = "shadow-6066-11e4-a52e-4f735466cecf"))]
        id: String,
    },
}

impl ElementRefHelper {
    pub fn id(&self) -> &str {
        match &self {
            ElementRefHelper::Element {
                id,
            } => id,
            ElementRefHelper::ShadowElement {
                id,
            } => id,
        }
    }
}

impl From<ElementRefHelper> for ElementRef {
    fn from(element_ref: ElementRefHelper) -> Self {
        let id = match element_ref {
            ElementRefHelper::Element {
                id,
            } => id,
            ElementRefHelper::ShadowElement {
                id,
            } => id,
        };
        ElementRef::from(id)
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, Hash, Eq, PartialEq)]
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)]
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, PartialEq, Eq)]
pub struct Rect {
    pub x: i64,
    pub y: i64,
    pub width: i64,
    pub height: i64,
}

impl Rect {
    pub fn new(x: i64, y: i64, width: i64, height: i64) -> Self {
        Rect {
            x,
            y,
            width,
            height,
        }
    }
}

/// Generic element query function that returns some type T.
pub type ElementQueryFn<T> =
    Box<dyn Fn(&WebElement) -> BoxFuture<WebDriverResult<T>> + Send + Sync + 'static>;

/// Function signature for element predicates.
pub type ElementPredicate = ElementQueryFn<bool>;