xee_xpath_ast/
span.rs

1use chumsky::span::SimpleSpan as SourceSpan;
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq)]
4pub struct Spanned<T> {
5    pub value: T,
6    pub span: SourceSpan,
7}
8
9pub trait WithSpan
10where
11    Self: Sized,
12{
13    fn with_span(self, span: SourceSpan) -> Spanned<Self> {
14        Spanned { value: self, span }
15    }
16    fn with_empty_span(self) -> Spanned<Self> {
17        self.with_span((0..0).into())
18    }
19}
20
21// custom serializer that skips span, so we don't see it in the ron
22// snapshot tests
23#[cfg(feature = "serde")]
24impl<T: serde::Serialize> serde::Serialize for Spanned<T> {
25    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
26        self.value.serialize(serializer)
27    }
28}
29
30impl<T> Spanned<T> {
31    pub fn new(value: T, span: SourceSpan) -> Self {
32        Self { value, span }
33    }
34
35    pub fn map(self, f: impl FnOnce(T) -> T) -> Self {
36        Self {
37            value: f(self.value),
38            span: self.span,
39        }
40    }
41}
42
43#[cfg(test)]
44mod tests {
45    // use insta::assert_debug_snapshot;
46
47    // use crate::ast::parse_xpath;
48    // use crate::namespaces::Namespaces;
49
50    // #[test]
51    // fn test_span_sequence_ast() {
52    //     let expr = "(0, 1, 2)";
53    //     //          012345678
54    //     //          0       8
55    //     //  So from 0, 9 is expected
56    //     // let's examine the AST
57    //     let namespaces = Namespaces::new(None, None);
58    //     assert_debug_snapshot!(parse_xpath(expr, &namespaces, &[]));
59    // }
60}