web_sys_ec/
by.rs

1pub(crate) mod inner {
2    pub enum By {
3        Id(String),
4        Class(String),
5        TagName(String),
6        QuerySelector(String),
7    }
8
9    impl core::fmt::Display for By {
10        fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
11            match self {
12                By::Id(id) => write!(f, "HTML element with id '{}' (`{:?}`)", id, &self),
13                By::Class(class) => {
14                    write!(f, "HTML element with class '{}' (`{:?}`)", class, &self)
15                }
16                By::TagName(tag_name) => write!(
17                    f,
18                    "HTML element with tag name '{}' (`{:?}`)",
19                    tag_name, &self
20                ),
21                By::QuerySelector(selector) => write!(
22                    f,
23                    "HTML element queried with selector '{}' (`{:?}`)",
24                    selector, &self
25                ),
26            }
27        }
28    }
29
30    impl core::fmt::Debug for By {
31        fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
32            match self {
33                By::Id(id) => write!(f, "By::Id({id:?})"),
34                By::Class(class) => write!(f, "By::Class({class:?})"),
35                By::TagName(tag_name) => write!(f, "By::TagName({tag_name:?})"),
36                By::QuerySelector(selector) => write!(f, "By::QuerySelector({selector:?})"),
37            }
38        }
39    }
40}
41
42/// Selectors for finding elements in the DOM.
43///
44/// Set of selectors for finding elements for which certain expected conditions
45/// can be applied.
46#[allow(non_snake_case)]
47pub mod By {
48    use super::inner;
49
50    /// Selects an element by its identifier.
51    #[inline]
52    pub fn Id(id: impl Into<String>) -> inner::By {
53        inner::By::Id(id.into())
54    }
55
56    /// Selects an element by its class name.
57    #[inline]
58    pub fn Class(class: impl Into<String>) -> inner::By {
59        inner::By::Class(class.into())
60    }
61
62    /// Selects an element by its tag name.
63    #[inline]
64    pub fn TagName(tag_name: impl Into<String>) -> inner::By {
65        inner::By::TagName(tag_name.into())
66    }
67
68    /// Selects an element by its CSS selector.
69    #[inline]
70    pub fn QuerySelector(selector: impl Into<String>) -> inner::By {
71        inner::By::QuerySelector(selector.into())
72    }
73}