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
175
176
177
178
179
180
181
182
183
184
185
186
use std::fmt;

#[derive(Debug, PartialEq, Eq, Clone)]
/// Convenience enum to create a Description. Can be a `Top` level, a `Field`
/// level or an `Input` level. The variants are distinguished by the way they
/// get displayed, e.g. number of leading spaces.
pub enum StringValue {
    /// Top-level description.
    Top {
        /// Description.
        source: String,
    },
    /// Field-level description.
    /// This description gets additional leading spaces.
    Field {
        /// Description.
        source: String,
    },
    /// Input-level description.
    /// This description get an additional space at the end.
    Input {
        /// Description.
        source: String,
    },
}

impl fmt::Display for StringValue {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            StringValue::Top { source } => {
                if is_block_string_character(source) {
                    writeln!(f, "\"\"\"\n{}\n\"\"\"", trim_double_quotes(source))?
                } else {
                    writeln!(f, "\"{}\"", source)?
                }
            }
            StringValue::Field { source } => {
                if is_block_string_character(source) {
                    write!(f, "  \"\"\"")?;
                    let desc = trim_double_quotes(source);
                    for line in desc.lines() {
                        write!(f, "\n  {}", line)?;
                    }
                    writeln!(f, "\n  \"\"\"")?;
                } else {
                    writeln!(f, "  \"{}\"", source)?
                }
            }
            StringValue::Input { source } => {
                if is_block_string_character(source) {
                    write!(f, "\"\"\"")?;
                    let desc = trim_double_quotes(source);
                    for line in desc.lines() {
                        write!(f, "\n    {}", line)?;
                    }
                    write!(f, "\n    \"\"\"")?
                } else {
                    write!(f, "\"{}\"", source)?
                }
            }
        }
        write!(f, "")
    }
}

#[allow(clippy::nonminimal_bool)]
fn trim_double_quotes(description: &str) -> String {
    let desc_len = description.len();
    if desc_len < 2 {
        return description.to_string();
    }

    if !description.starts_with('\"') || !description.ends_with('\"') {
        return description.to_string();
    }

    description
        .chars()
        .enumerate()
        .filter_map(|(i, c)| {
            if (i == 0 && c == '"') || (i == desc_len - 1 && c == '"') {
                None
            } else {
                Some(c)
            }
        })
        .collect()
}

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: "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: "\"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_other_languages() {
        let desc = StringValue::Top {
            source: "котя(猫, ねこ, قطة) любить дрімати в \"кутку\" з рослинами".to_string(),
        };

        assert_eq!(
            desc.to_string(),
            r#""""
котя(猫, ねこ, قطة) любить дрімати в "кутку" з рослинами
"""
"#
        );
    }

    #[test]
    fn it_encodes_description_with_new_line() {
        let desc = StringValue::Top {
            source: "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: "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: "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"
            )
        );
    }
}