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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
use std::fmt;
#[derive(Debug, PartialEq, Clone)]
pub enum StringValue {
Top {
source: Option<String>,
},
Field {
source: Option<String>,
},
Input {
source: Option<String>,
},
Reason {
source: Option<String>,
},
}
impl fmt::Display for StringValue {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
StringValue::Top { source } => {
if let Some(description) = source {
if is_block_string_character(description) {
writeln!(f, "\"\"\"\n{}\n\"\"\"", description)?
} else {
writeln!(f, "\"{}\"", description)?
}
}
}
StringValue::Field { source } => {
if let Some(description) = source {
if is_block_string_character(description) {
write!(f, " \"\"\"")?;
for line in description.lines() {
write!(f, "\n {}", line)?;
}
writeln!(f, "\n \"\"\"")?;
} else {
writeln!(f, " \"{}\"", description)?
}
}
}
StringValue::Input { source } => {
if let Some(description) = source {
if is_block_string_character(description) {
write!(f, "\"\"\"\n{}\n\"\"\" ", description)?
} else {
write!(f, "\"{}\" ", description)?
}
}
}
StringValue::Reason { source } => {
if let Some(description) = source {
if is_block_string_character(description) {
write!(f, "\n \"\"\"")?;
for line in description.lines() {
write!(f, "\n {}", line)?;
}
write!(f, "\n \"\"\"\n ")?
} else {
write!(f, " \"{}\"", description)?
}
}
}
}
write!(f, "")
}
}
fn is_block_string_character(s: &str) -> bool {
s.contains('\n') || s.contains('"') || s.contains('\r')
}
#[cfg(test)]
mod test {
use super::*;
use pretty_assertions::assert_eq;
#[test]
fn it_encodes_description_without_block_string_character() {
let desc = StringValue::Top {
source: Some(
"Favourite cat nap spots include: plant corner, pile of clothes.".to_string(),
),
};
assert_eq!(
desc.to_string(),
r#""Favourite cat nap spots include: plant corner, pile of clothes."
"#
);
}
#[test]
fn it_encodes_description_with_quotations() {
let desc = StringValue::Top {
source: Some(
"Favourite \"cat\" nap spots include: plant corner, pile of clothes.".to_string(),
),
};
assert_eq!(
desc.to_string(),
r#""""
Favourite "cat" nap spots include: plant corner, pile of clothes.
"""
"#
);
}
#[test]
fn it_encodes_description_with_new_line() {
let desc = StringValue::Top {
source: Some(
"Favourite cat nap spots include:\nplant corner, pile of clothes.".to_string(),
),
};
assert_eq!(
desc.to_string(),
r#""""
Favourite cat nap spots include:
plant corner, pile of clothes.
"""
"#
);
}
#[test]
fn it_encodes_description_with_carriage_return() {
let desc = StringValue::Top {
source: Some(
"Favourite cat nap spots include:\rplant corner,\rpile of clothes.".to_string(),
),
};
assert_eq!(
desc.to_string(),
String::from(
"\"\"\"\nFavourite cat nap spots include:\rplant corner,\rpile of clothes.\n\"\"\"\n"
)
);
}
#[test]
fn it_encodes_indented_desciption() {
let desc = StringValue::Field {
source: Some(
"Favourite cat nap spots include:\r plant corner,\r pile of clothes.".to_string(),
),
};
assert_eq!(
desc.to_string(),
String::from(
" \"\"\"\n Favourite cat nap spots include:\r plant corner,\r pile of clothes.\n \"\"\"\n"
)
);
}
}