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
use crate::number::digit;
use nom::{
    branch::alt,
    bytes::complete::tag,
    character::complete::{one_of, satisfy},
    combinator::{map, recognize, value},
    multi::many0,
    sequence::{delimited, tuple},
    IResult,
};

#[derive(Debug, PartialEq)]
pub struct JsonString(pub String);

/// Recognize string
/// ```rust
/// use wson::string::{string, JsonString};
/// # use std::error::Error;
/// # fn main() -> Result<(), Box<dyn Error>> {
/// let value = string("\"\"")?;
/// assert_eq!(value, ("", JsonString("".to_string())));
///
/// let value = string("\"hello\"")?;
/// assert_eq!(value, ("", JsonString("hello".to_string())));
///
/// let value = string("\"こんにちは\"")?;
/// assert_eq!(value, ("", JsonString("こんにちは".to_string())));
///
/// let value = string("\"abc123\"")?;
/// assert_eq!(value, ("", JsonString("abc123".to_string())));
///
/// let value = string("\"He\\\"\\\"llo\"")?;
/// assert_eq!(value, ("", JsonString("He\\\"\\\"llo".to_string())));
///
/// # Ok(())
/// # }
/// ```
pub fn string(input: &str) -> IResult<&str, JsonString> {
    map(delimited(tag("\""), characters, tag("\"")), |str: &str| {
        JsonString(str.to_string())
    })(input)
}

fn characters(input: &str) -> IResult<&str, &str> {
    recognize(many0(character))(input)
}

fn character(input: &str) -> IResult<&str, &str> {
    alt((
        recognize(tuple((tag("\\"), escape))),
        recognize(satisfy(|c| c != '"')),
        value("", one_of("")),
    ))(input)
}

// escape = '"' DoubleQuote
//        | '\' Backslash
//        | '/' Slash
//        | 'b' Boundary
//        | 'f' FormFeed
//        | 'n' NewLine
//        | 'r' CarriageReturn
//        | 't' Tab
//        | 'u' hex hex hex hex
fn escape(input: &str) -> IResult<&str, &str> {
    alt((
        recognize(one_of("\"\\/bfnrt")),
        recognize(tuple((tag("u"), hex, hex, hex, hex))),
    ))(input)
}

// hex = digit
//     | 'A' . 'F'
//     | 'a' . 'f'
fn hex(input: &str) -> IResult<&str, &str> {
    alt((recognize(digit), recognize(one_of("abcdefABCDEF"))))(input)
}

#[cfg(test)]
mod tests {
    use std::error::Error;

    use super::*;

    type TestResult = Result<(), Box<dyn Error>>;

    #[test]
    fn parse_empty_string() -> TestResult {
        let value = string("\"\"")?;
        assert_eq!(value, ("", JsonString("".to_string())));
        Ok(())
    }

    #[test]
    fn parse_hello_string() -> TestResult {
        let value = string("\"hello\"")?;
        assert_eq!(value, ("", JsonString("hello".to_string())));
        Ok(())
    }

    #[test]
    fn parse_utf8_string() -> TestResult {
        let value = string("\"こんにちは\"")?;
        assert_eq!(value, ("", JsonString("こんにちは".to_string())));
        Ok(())
    }

    #[test]
    fn parse_alphanum_string() -> TestResult {
        let value = string("\"abc123\"")?;
        assert_eq!(value, ("", JsonString("abc123".to_string())));
        Ok(())
    }

    #[test]
    fn hex_five() -> TestResult {
        let value = hex("5")?;
        assert_eq!(value, ("", "5"));
        Ok(())
    }

    #[test]
    fn hex_f() -> TestResult {
        let value = hex("f")?;
        assert_eq!(value, ("", "f"));
        Ok(())
    }

    #[test]
    fn hex_large_f() -> TestResult {
        let value = hex("F")?;
        assert_eq!(value, ("", "F"));
        Ok(())
    }

    #[test]
    fn escape_slash() -> TestResult {
        let value = escape("/")?;
        assert_eq!(value, ("", "/"));
        Ok(())
    }

    #[test]
    fn escape_unicode() -> TestResult {
        let value = escape("u1234")?;
        assert_eq!(value, ("", "u1234"));
        Ok(())
    }

    #[test]
    fn character_unicode() -> TestResult {
        let value = character("\\u1234")?;
        assert_eq!(value, ("", "\\u1234"));
        Ok(())
    }
}