Skip to main content

oxc_css_parser/
span_ignored_eq.rs

1use crate::Span;
2use smallvec::SmallVec;
3use std::borrow::Cow;
4
5/// Compare equality of two AST nodes without respecting their spans.
6///
7/// This allows to compare AST nodes content
8/// while they're in different locations of syntax tree.
9pub trait SpanIgnoredEq {
10    /// Compare equality of two AST nodes without respecting their spans.
11    #[must_use]
12    fn span_ignored_eq(&self, other: &Self) -> bool;
13}
14
15impl SpanIgnoredEq for str {
16    #[inline]
17    fn span_ignored_eq(&self, other: &Self) -> bool {
18        self == other
19    }
20}
21
22impl SpanIgnoredEq for &str {
23    #[inline]
24    fn span_ignored_eq(&self, other: &Self) -> bool {
25        self == other
26    }
27}
28
29impl SpanIgnoredEq for String {
30    #[inline]
31    fn span_ignored_eq(&self, other: &Self) -> bool {
32        self == other
33    }
34}
35
36impl SpanIgnoredEq for Cow<'_, str> {
37    #[inline]
38    fn span_ignored_eq(&self, other: &Self) -> bool {
39        self == other
40    }
41}
42
43impl SpanIgnoredEq for Span {
44    #[inline]
45    fn span_ignored_eq(&self, _: &Self) -> bool {
46        true
47    }
48}
49
50impl SpanIgnoredEq for f32 {
51    #[inline]
52    fn span_ignored_eq(&self, other: &Self) -> bool {
53        self == other
54    }
55}
56
57impl SpanIgnoredEq for i32 {
58    #[inline]
59    fn span_ignored_eq(&self, other: &Self) -> bool {
60        self == other
61    }
62}
63
64impl SpanIgnoredEq for u32 {
65    #[inline]
66    fn span_ignored_eq(&self, other: &Self) -> bool {
67        self == other
68    }
69}
70
71impl SpanIgnoredEq for bool {
72    #[inline]
73    fn span_ignored_eq(&self, other: &Self) -> bool {
74        self == other
75    }
76}
77
78impl SpanIgnoredEq for char {
79    #[inline]
80    fn span_ignored_eq(&self, other: &Self) -> bool {
81        self == other
82    }
83}
84
85impl<T> SpanIgnoredEq for Vec<T>
86where
87    T: SpanIgnoredEq,
88{
89    #[inline]
90    fn span_ignored_eq(&self, other: &Self) -> bool {
91        self.len() == other.len()
92            && self
93                .iter()
94                .zip(other.iter())
95                .all(|(a, b)| a.span_ignored_eq(b))
96    }
97}
98
99impl<T, const N: usize> SpanIgnoredEq for SmallVec<[T; N]>
100where
101    T: SpanIgnoredEq,
102{
103    #[inline]
104    fn span_ignored_eq(&self, other: &Self) -> bool {
105        self.len() == other.len()
106            && self
107                .iter()
108                .zip(other.iter())
109                .all(|(a, b)| a.span_ignored_eq(b))
110    }
111}
112
113impl<T> SpanIgnoredEq for Option<T>
114where
115    T: SpanIgnoredEq,
116{
117    #[inline]
118    fn span_ignored_eq(&self, other: &Self) -> bool {
119        match (self, other) {
120            (Some(a), Some(b)) => a.span_ignored_eq(b),
121            (None, None) => true,
122            _ => false,
123        }
124    }
125}
126
127impl<T> SpanIgnoredEq for Box<T>
128where
129    T: SpanIgnoredEq,
130{
131    #[inline]
132    fn span_ignored_eq(&self, other: &Self) -> bool {
133        self.as_ref().span_ignored_eq(other.as_ref())
134    }
135}