| 1 | use crate::{ |
| 2 | de::{Position, Span}, |
| 3 | error::SpannedResult, |
| 4 | }; |
| 5 | use alloc::string::String; |
| 6 | |
| 7 | impl Position { |
| 8 | /// Given a Position and a string, return the 0-indexed grapheme index into the |
| 9 | /// string at that position, or [None] if the Position is out of bounds of the string. |
| 10 | #[must_use] |
| 11 | pub fn grapheme_index(&self, s: &str) -> Option<usize> { |
| 12 | use unicode_segmentation::UnicodeSegmentation; |
| 13 | let mut line_no = 1; |
| 14 | let mut col_no = 1; |
| 15 | |
| 16 | if (self.line, self.col) == (1, 1) { |
| 17 | return Some(0); |
| 18 | } |
| 19 | |
| 20 | let mut i = 0; |
| 21 | |
| 22 | // Slightly non-intuitive arithmetic: a zero-length string at line 1, col 1 -> 0 |
| 23 | |
| 24 | if (line_no, col_no) == (self.line, self.col) { |
| 25 | return Some(i); |
| 26 | } |
| 27 | |
| 28 | for ch in s.graphemes(true) { |
| 29 | if (line_no, col_no) == (self.line, self.col) { |
| 30 | return Some(i); |
| 31 | } |
| 32 | |
| 33 | // "\n" and "\r\n" each come through the iterator as a single grapheme |
| 34 | if matches!(ch, "\n" | "\r\n") { |
| 35 | line_no += 1; |
| 36 | col_no = 1; |
| 37 | } else { |
| 38 | col_no += 1; |
| 39 | } |
| 40 | |
| 41 | i += 1; |
| 42 | } |
| 43 | |
| 44 | // ...and a string of length 7 at line 1, col 8 -> 7 |
| 45 | if (line_no, col_no) == (self.line, self.col) { |
| 46 | return Some(i); |
| 47 | } |
| 48 | |
| 49 | None |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | impl Span { |
| 54 | /// Given a `Span` and a string, form the resulting string selected exclusively (as in `[start..end`]) by the `Span` |
| 55 | /// or [`None`] if the span is out of bounds of the string at either end. |
| 56 | #[must_use] |
| 57 | pub fn substring_exclusive(&self, s: &str) -> Option<String> { |
| 58 | use alloc::vec::Vec; |
| 59 | use unicode_segmentation::UnicodeSegmentation; |
| 60 | |
| 61 | if let (Some(start), Some(end)) = (self.start.grapheme_index(s), self.end.grapheme_index(s)) |
| 62 | { |
| 63 | Some(s.graphemes(true).collect::<Vec<&str>>()[start..end].concat()) |
| 64 | } else { |
| 65 | None |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /// Given a `Span` and a string, form the resulting string selected inclusively (as in `[start..=end]`) by the `Span` |
| 70 | /// or [`None`] if the span is out of bounds of the string at either end. |
| 71 | #[must_use] |
| 72 | pub fn substring_inclusive(&self, s: &str) -> Option<String> { |
| 73 | use alloc::vec::Vec; |
| 74 | use unicode_segmentation::UnicodeSegmentation; |
| 75 | |
| 76 | if let (Some(start), Some(end)) = (self.start.grapheme_index(s), self.end.grapheme_index(s)) |
| 77 | { |
| 78 | Some(s.graphemes(true).collect::<Vec<&str>>()[start..=end].concat()) |
| 79 | } else { |
| 80 | None |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | /// Given a string `ron`, a [`SpannedResult`], and a substring, verify that trying to parse `ron` results in an error |
| 86 | /// equal to the [`SpannedResult`] with a Span that exclusively (as in `[start..end]`) selects that substring. |
| 87 | /// Note that there are two versions of this helper, inclusive and exclusive. This is because while the parser cursor |
| 88 | /// arithmetic that computes span positions always produces exclusive spans (as in `[start..end]`), |
| 89 | /// when doing validation against a target substring, the inclusive check including the final grapheme that triggered |
| 90 | /// the error is often a more intuitive target to check against. |
| 91 | /// Meanwhile, if the parser threw an EOF, for example, there is no final grapheme to check, and so |
| 92 | /// only the exclusive check would produce a meaningful result. |
| 93 | #[allow(clippy::unwrap_used)] |
| 94 | #[allow(clippy::missing_panics_doc)] |
| 95 | pub fn check_error_span_exclusive<T: serde::de::DeserializeOwned + PartialEq + core::fmt::Debug>( |
| 96 | ron: &str, |
| 97 | check: SpannedResult<T>, |
| 98 | substr: &str, |
| 99 | ) { |
| 100 | let res_str = crate::de::from_str::<T>(ron); |
| 101 | assert_eq!(res_str, check); |
| 102 | |
| 103 | let res_bytes = crate::de::from_bytes::<T>(ron.as_bytes()); |
| 104 | assert_eq!(res_bytes, check); |
| 105 | |
| 106 | #[cfg(feature = "std")] |
| 107 | { |
| 108 | let res_reader = crate::de::from_reader::<&[u8], T>(ron.as_bytes()); |
| 109 | assert_eq!(res_reader, check); |
| 110 | } |
| 111 | |
| 112 | assert_eq!( |
| 113 | check.unwrap_err().span.substring_exclusive(ron).unwrap(), |
| 114 | substr |
| 115 | ); |
| 116 | } |
| 117 | |
| 118 | /// Given a string `ron`, a [`SpannedResult`], and a substring, verify that trying to parse `ron` results in an error |
| 119 | /// equal to the [`SpannedResult`] with a Span that inclusively (as in `[start..=end`]) selects that substring. |
| 120 | /// See [`check_error_span_exclusive`] for the rationale behind both versions of this helper. |
| 121 | #[allow(clippy::unwrap_used)] |
| 122 | #[allow(clippy::missing_panics_doc)] |
| 123 | pub fn check_error_span_inclusive<T: serde::de::DeserializeOwned + PartialEq + core::fmt::Debug>( |
| 124 | ron: &str, |
| 125 | check: SpannedResult<T>, |
| 126 | substr: &str, |
| 127 | ) { |
| 128 | let res_str = crate::de::from_str::<T>(ron); |
| 129 | assert_eq!(res_str, check); |
| 130 | |
| 131 | let res_bytes = crate::de::from_bytes::<T>(ron.as_bytes()); |
| 132 | assert_eq!(res_bytes, check); |
| 133 | |
| 134 | #[cfg(feature = "std")] |
| 135 | { |
| 136 | let res_reader = crate::de::from_reader::<&[u8], T>(ron.as_bytes()); |
| 137 | assert_eq!(res_reader, check); |
| 138 | } |
| 139 | |
| 140 | assert_eq!( |
| 141 | check.unwrap_err().span.substring_inclusive(ron).unwrap(), |
| 142 | substr |
| 143 | ); |
| 144 | } |
| 145 | |
| 146 | #[cfg(test)] |
| 147 | mod tests { |
| 148 | use crate::de::{Position, Span}; |
| 149 | |
| 150 | fn span(start: Position, end: Position) -> Span { |
| 151 | Span { start, end } |
| 152 | } |
| 153 | |
| 154 | fn pos(line: usize, col: usize) -> Position { |
| 155 | Position { line, col } |
| 156 | } |
| 157 | |
| 158 | #[test] |
| 159 | fn ascii_basics() { |
| 160 | let text = "hello\nworld"; |
| 161 | |
| 162 | // first char / first col |
| 163 | assert_eq!(pos(1, 1).grapheme_index(text), Some(0)); |
| 164 | |
| 165 | // last char on first line ('o') |
| 166 | assert_eq!(pos(1, 5).grapheme_index(text), Some(4)); |
| 167 | |
| 168 | // start of second line ('w') |
| 169 | assert_eq!(pos(2, 1).grapheme_index(text), Some(6)); |
| 170 | |
| 171 | // span across the `\n` |
| 172 | assert_eq!( |
| 173 | span(pos(1, 4), pos(2, 2)) |
| 174 | .substring_exclusive(text) |
| 175 | .unwrap(), |
| 176 | "lo\nw" |
| 177 | ); |
| 178 | } |
| 179 | |
| 180 | #[test] |
| 181 | fn multibyte_greek() { |
| 182 | let text = "αβγ\ndeux\n三四五\r\nend"; |
| 183 | |
| 184 | // Beta |
| 185 | assert_eq!(pos(1, 2).grapheme_index(text), Some(1)); |
| 186 | |
| 187 | // 三 |
| 188 | assert_eq!(pos(3, 1).grapheme_index(text), Some(9)); |
| 189 | |
| 190 | // e |
| 191 | assert_eq!(pos(4, 1).grapheme_index(text), Some(13)); |
| 192 | |
| 193 | // span from α to start of “deux” |
| 194 | assert_eq!( |
| 195 | span(pos(1, 1), pos(2, 1)) |
| 196 | .substring_exclusive(text) |
| 197 | .unwrap(), |
| 198 | "αβγ\n" |
| 199 | ); |
| 200 | } |
| 201 | |
| 202 | #[test] |
| 203 | fn combining_mark_cluster() { |
| 204 | // é == [0x65, 0xCC, 0x81] in UTF-8 |
| 205 | let text = "e\u{0301}x\n"; |
| 206 | |
| 207 | // grapheme #1 (“é”) |
| 208 | assert_eq!(pos(1, 1).grapheme_index(text), Some(0)); |
| 209 | |
| 210 | // grapheme #2 (“x”) |
| 211 | assert_eq!(pos(1, 2).grapheme_index(text), Some(1)); |
| 212 | |
| 213 | // column 4 is past EOL |
| 214 | assert_eq!(pos(1, 4).grapheme_index(text), None); |
| 215 | |
| 216 | // full span |
| 217 | assert_eq!( |
| 218 | span(pos(1, 1), pos(1, 2)) |
| 219 | .substring_exclusive(text) |
| 220 | .unwrap(), |
| 221 | "e\u{0301}" |
| 222 | ); |
| 223 | } |
| 224 | |
| 225 | #[test] |
| 226 | fn zwj_emoji_cluster() { |
| 227 | let text = "👩👩👧👧 and 👨👩👦"; |
| 228 | |
| 229 | // The family emoji is the first grapheme on the line. |
| 230 | assert_eq!(pos(1, 1).grapheme_index(text), Some(0)); |
| 231 | |
| 232 | assert_eq!(pos(1, 2).grapheme_index(text), Some(1)); |
| 233 | |
| 234 | // Span selecting only the first emoji |
| 235 | assert_eq!( |
| 236 | span(pos(1, 1), pos(1, 2)) |
| 237 | .substring_exclusive(text) |
| 238 | .unwrap(), |
| 239 | "👩👩👧👧" |
| 240 | ); |
| 241 | |
| 242 | // Span selecting only the second emoji |
| 243 | assert_eq!( |
| 244 | span(pos(1, 7), pos(1, 8)) |
| 245 | .substring_exclusive(text) |
| 246 | .unwrap(), |
| 247 | "👨👩👦" |
| 248 | ); |
| 249 | } |
| 250 | |
| 251 | #[test] |
| 252 | fn mixed_newlines() { |
| 253 | let text = "one\r\ntwo\nthree\r\n"; |
| 254 | |
| 255 | // start of “two” (line numbers are 1-based) |
| 256 | assert_eq!(pos(2, 1).grapheme_index(text), Some(4)); |
| 257 | |
| 258 | // “three” |
| 259 | assert_eq!(pos(3, 1).grapheme_index(text), Some(8)); |
| 260 | |
| 261 | // span “two\n” |
| 262 | assert_eq!( |
| 263 | span(pos(2, 1), pos(3, 1)) |
| 264 | .substring_exclusive(text) |
| 265 | .unwrap(), |
| 266 | "two\n" |
| 267 | ); |
| 268 | |
| 269 | // span “two\nthree” |
| 270 | assert_eq!( |
| 271 | span(pos(2, 1), pos(3, 6)) |
| 272 | .substring_exclusive(text) |
| 273 | .unwrap(), |
| 274 | "two\nthree" |
| 275 | ); |
| 276 | } |
| 277 | |
| 278 | #[test] |
| 279 | fn oob_and_error_paths() { |
| 280 | let text = "short"; |
| 281 | |
| 282 | // line past EOF |
| 283 | assert_eq!(pos(2, 1).grapheme_index(text), None); |
| 284 | |
| 285 | // column past EOL |
| 286 | assert_eq!(pos(1, 10).grapheme_index(text), None); |
| 287 | |
| 288 | // span with either endpoint oob → None |
| 289 | assert_eq!(span(pos(1, 1), pos(2, 1)).substring_exclusive(text), None); |
| 290 | } |
| 291 | |
| 292 | #[test] |
| 293 | fn whole_text_span() { |
| 294 | let text = "αβγ\nδεζ"; |
| 295 | let all = span(pos(1, 1), pos(2, 4)); |
| 296 | assert_eq!(&all.substring_exclusive(text).unwrap(), text); |
| 297 | } |
| 298 | |
| 299 | #[test] |
| 300 | fn span_substring_helper() { |
| 301 | assert_eq!( |
| 302 | Span { |
| 303 | start: Position { line: 1, col: 1 }, |
| 304 | end: Position { line: 2, col: 1 }, |
| 305 | } |
| 306 | .substring_exclusive( |
| 307 | "In the first place, there are two sorts of bets, or toh.11 There is the |
| 308 | single axial bet in the center between the principals (toh ketengah), and |
| 309 | there is the cloud of peripheral ones around the ring between members |
| 310 | of the audience (toh kesasi). ", |
| 311 | ) |
| 312 | .unwrap(), |
| 313 | "In the first place, there are two sorts of bets, or toh.11 There is the\n" |
| 314 | ); |
| 315 | |
| 316 | assert_eq!( |
| 317 | Span { |
| 318 | start: Position { line: 2, col: 1 }, |
| 319 | end: Position { line: 3, col: 1 }, |
| 320 | } |
| 321 | .substring_exclusive( |
| 322 | "In the first place, there are two sorts of bets, or toh.11 There is the |
| 323 | single axial bet in the center between the principals (toh ketengah), and |
| 324 | there is the cloud of peripheral ones around the ring between members |
| 325 | of the audience (toh kesasi). ", |
| 326 | ) |
| 327 | .unwrap(), |
| 328 | "single axial bet in the center between the principals (toh ketengah), and\n" |
| 329 | ); |
| 330 | } |
| 331 | } |