Skip to main content

openpql_pql_parser/ast/
str.rs

1use super::{Loc, LocInfo, Spanned, str};
2
3/// String literal borrowed from the source with its surrounding quotes stripped.
4#[derive(Clone, PartialEq, Eq, derive_more::From, derive_more::Debug)]
5#[debug("{:?}", self.inner)]
6pub struct Str<'i> {
7    /// Literal content without the surrounding quotes.
8    pub inner: &'i str,
9    /// Source span including the quotes.
10    pub loc: (Loc, Loc),
11}
12
13impl Spanned for Str<'_> {
14    fn loc(&self) -> LocInfo {
15        self.loc
16    }
17}
18
19#[cfg(test)]
20mod tests {
21    use super::*;
22    use crate::*;
23
24    fn assert_str(src: &str, expected: &str) {
25        let loc_start = 0;
26        let loc_end = src.len();
27        assert_eq!(parse_str(src), Ok((expected, (loc_start, loc_end)).into()));
28    }
29
30    #[test]
31    fn test_str() {
32        assert_str(r#""str""#, "str");
33        assert_str(r#""""#, "");
34
35        assert_str("'str'", "str");
36        assert_str("''", "");
37
38        assert_str("'one two three'", "one two three");
39    }
40
41    #[test]
42    fn test_dbg() {
43        assert_eq!(
44            format!("{:?}", Str::from(("content", (0, 1)))),
45            "\"content\""
46        );
47    }
48
49    #[test]
50    fn test_loc() {
51        let s = Str::from(("x", (1, 4)));
52        assert_eq!(s.loc(), (1, 4));
53    }
54}