Skip to main content

oxc_css_parser/
span_ignored_eq.rs

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