| 1 |
//! This example shows an example of how to parse an escaped string. The |
| 2 |
//! rules for the string are similar to JSON and rust. A string is: |
| 3 |
//! |
| 4 |
//! - Enclosed by double quotes |
| 5 |
//! - Can contain any raw unescaped code point besides \ and " |
| 6 |
//! - Matches the following escape sequences: \b, \f, \n, \r, \t, \", \\, \/ |
| 7 |
//! - Matches code points like Rust: \u{XXXX}, where XXXX can be up to 6 |
| 8 |
//! hex characters |
| 9 |
//! - an escape followed by whitespace consumes all whitespace between the |
| 10 |
//! escape and the next non-whitespace character |
| 11 |
|
| 12 |
#![cfg(feature = "alloc")] |
| 13 |
|
| 14 |
mod parser; |
| 15 |
|
| 16 |
use winnow::prelude::*; |
| 17 |
|
| 18 |
fn main() -> Result<(), lexopt::Error> { |
| 19 |
let args = Args::parse()?; |
| 20 |
|
| 21 |
let data = args.input.as_deref().unwrap_or("\"abc\""); |
| 22 |
let result = parser::parse_string::<()>.parse(data); |
| 23 |
match result { |
| 24 |
Ok(data) => println!("{data}"), |
| 25 |
Err(err) => println!("{err:?}"), |
| 26 |
} |
| 27 |
|
| 28 |
Ok(()) |
| 29 |
} |
| 30 |
|
| 31 |
#[derive(Default)] |
| 32 |
struct Args { |
| 33 |
input: Option<String>, |
| 34 |
} |
| 35 |
|
| 36 |
impl Args { |
| 37 |
fn parse() -> Result<Self, lexopt::Error> { |
| 38 |
use lexopt::prelude::*; |
| 39 |
|
| 40 |
let mut res = Args::default(); |
| 41 |
|
| 42 |
let mut args = lexopt::Parser::from_env(); |
| 43 |
while let Some(arg) = args.next()? { |
| 44 |
match arg { |
| 45 |
Value(input) => { |
| 46 |
res.input = Some(input.string()?); |
| 47 |
} |
| 48 |
_ => return Err(arg.unexpected()), |
| 49 |
} |
| 50 |
} |
| 51 |
Ok(res) |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
#[test] |
| 56 |
fn simple() { |
| 57 |
let data = "\"abc\""; |
| 58 |
let result = parser::parse_string::<()>.parse(data); |
| 59 |
assert_eq!(result, Ok(String::from("abc"))); |
| 60 |
} |
| 61 |
|
| 62 |
#[test] |
| 63 |
fn escaped() { |
| 64 |
let data = "\"tab:\\tafter tab, newline:\\nnew line, quote: \\\", emoji: \\u{1F602}, newline:\\nescaped whitespace: \\ abc\""; |
| 65 |
let result = parser::parse_string::<()>.parse(data); |
| 66 |
assert_eq!( |
| 67 |
result, |
| 68 |
Ok(String::from("tab:\tafter tab, newline:\nnew line, quote: \", emoji: 😂, newline:\nescaped whitespace: abc")) |
| 69 |
); |
| 70 |
} |