pub_just/
string_literal.rs

1use super::*;
2
3#[derive(PartialEq, Debug, Clone, Ord, Eq, PartialOrd)]
4pub struct StringLiteral<'src> {
5  pub cooked: String,
6  pub expand: bool,
7  pub kind: StringKind,
8  pub raw: &'src str,
9}
10
11impl<'src> StringLiteral<'src> {
12  pub fn from_raw(raw: &'src str) -> Self {
13    Self {
14      cooked: raw.into(),
15      expand: false,
16      kind: StringKind {
17        delimiter: StringDelimiter::QuoteSingle,
18        indented: false,
19      },
20      raw,
21    }
22  }
23}
24
25impl<'src> Display for StringLiteral<'src> {
26  fn fmt(&self, f: &mut Formatter) -> fmt::Result {
27    if self.expand {
28      write!(f, "x")?;
29    }
30
31    write!(
32      f,
33      "{}{}{}",
34      self.kind.delimiter(),
35      self.raw,
36      self.kind.delimiter()
37    )
38  }
39}
40
41impl<'src> Serialize for StringLiteral<'src> {
42  fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
43  where
44    S: Serializer,
45  {
46    serializer.serialize_str(&self.cooked)
47  }
48}