| 85 | type OptionParserResult<T> = std::result::Result<T, OptionParserError>; |
| 86 | |
| 87 | fn split_commas(s: &str) -> OptionParserResult<Vec<String>> { |
| 88 | let mut list: Vec<String> = Vec::new(); |
| 89 | let mut opened_brackets = 0u64; |
| 90 | let mut in_quotes = false; |
| 91 | let mut current = String::new(); |
| 92 | |
| 93 | for c in s.trim().chars() { |
| 94 | match c { |
| 95 | // In quotes, only '"' is special |
| 96 | '"' => in_quotes = !in_quotes, |
| 97 | _ if in_quotes => {} |
| 98 | '[' => opened_brackets += 1, |
| 99 | ']' => { |
| 100 | if opened_brackets < 1 { |
| 101 | return Err(OptionParserError::InvalidSyntax(s.to_owned())); |
| 102 | } |
| 103 | opened_brackets -= 1; |
| 104 | } |
| 105 | ',' if opened_brackets == 0 => { |
| 106 | list.push(current); |
| 107 | current = String::new(); |
| 108 | continue; |
| 109 | } |
| 110 | _ => {} |
| 111 | } |
| 112 | current.push(c); |
| 113 | } |
| 114 | list.push(current); |
| 115 | |
| 116 | if in_quotes || opened_brackets != 0 { |
| 117 | return Err(OptionParserError::InvalidSyntax(s.to_owned())); |
| 118 | } |
| 119 | |
| 120 | Ok(list) |
| 121 | } |
| 122 | |
| 123 | impl OptionParser { |
| 124 | /// Creates an empty `OptionParser` with no registered options. |