-
Notifications
You must be signed in to change notification settings - Fork 499
/
string_literal.rs
48 lines (43 loc) · 953 Bytes
/
string_literal.rs
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
use super::*;
#[derive(PartialEq, Debug, Clone, Ord, Eq, PartialOrd)]
pub(crate) struct StringLiteral<'src> {
pub(crate) cooked: String,
pub(crate) expand: bool,
pub(crate) kind: StringKind,
pub(crate) raw: &'src str,
}
impl<'src> StringLiteral<'src> {
pub(crate) fn from_raw(raw: &'src str) -> Self {
Self {
cooked: raw.into(),
expand: false,
kind: StringKind {
delimiter: StringDelimiter::QuoteSingle,
indented: false,
},
raw,
}
}
}
impl Display for StringLiteral<'_> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
if self.expand {
write!(f, "x")?;
}
write!(
f,
"{}{}{}",
self.kind.delimiter(),
self.raw,
self.kind.delimiter()
)
}
}
impl Serialize for StringLiteral<'_> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&self.cooked)
}
}