1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#[derive(Debug, PartialEq, Clone)]
/// A single or double quoted string
/// literal
pub enum StringLit<T> {
    Single(InnerString<T>),
    Double(InnerString<T>),
}
#[derive(Debug, PartialEq, Clone)]
pub struct InnerString<T> {
    pub content: T,
    pub contains_octal_escape: bool,
}

impl<T> ToString for StringLit<T>
where
    T: AsRef<str>,
{
    fn to_string(&self) -> String {
        match self {
            StringLit::Single(ref s) => format!(r#"'{}'"#, s.content.as_ref()),
            StringLit::Double(ref s) => format!(r#""{}""#, s.content.as_ref()),
        }
    }
}

impl<T> AsRef<str> for StringLit<T>
where
    T: AsRef<str>,
{
    fn as_ref(&self) -> &str {
        match self {
            StringLit::Single(s) | StringLit::Double(s) => s.as_ref(),
        }
    }
}

impl<T> AsRef<str> for InnerString<T>
where
    T: AsRef<str>,
{
    fn as_ref(&self) -> &str {
        &self.content.as_ref()
    }
}

impl<T> StringLit<T> {
    pub fn single(content: T, oct: bool) -> Self {
        StringLit::Single(InnerString {
            content,
            contains_octal_escape: oct,
        })
    }
    pub fn double(content: T, oct: bool) -> Self {
        StringLit::Double(InnerString {
            content,
            contains_octal_escape: oct,
        })
    }
    pub fn is_single(&self) -> bool {
        matches!(self, StringLit::Single(_))
    }
    pub fn is_double(&self) -> bool {
        matches!(self, StringLit::Double(_))
    }
    pub fn has_octal_escape(&self) -> bool {
        match self {
            StringLit::Single(ref inner) | StringLit::Double(ref inner) => {
                inner.contains_octal_escape
            }
        }
    }
}