Skip to content

Commit

Permalink
Factor out find_index and test it.
Browse files Browse the repository at this point in the history
  • Loading branch information
jhugman committed Sep 18, 2023
1 parent e9f4c8d commit 1f8216d
Showing 1 changed file with 45 additions and 27 deletions.
72 changes: 45 additions & 27 deletions components/support/nimbus-fml/src/client/inspector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,12 @@ impl FmlFeatureInspector {
fn get_syntax_error(&self, string: &str) -> Result<Value, FmlEditorError> {
let json = serde_json::from_str::<Value>(string);
if let Err(e) = json {
let col = e.column();
return Err(FmlEditorError {
message: "Need valid JSON object".to_string(),
// serde_json errors are 1 indexed.
line: e.line() as u32 - 1,
col: e.line() as u32 - 1,
col: if col == 0 { 0 } else { col - 1 } as u32,
hightlight: None,
});
}
Expand Down Expand Up @@ -116,7 +117,9 @@ fn find_err(src: &str, path: impl Iterator<Item = String>) -> (usize, usize) {
// If we haven't had our first match of the line, then start there at the beginning.
// Otherwise, start one char on from where we were last time.
let start = if !first_match { 0 } else { col_no + 1 };
if let Some(i) = cur[start..].find(&p).map(|i| i + start) {

// if let Some(i) = cur[start..].find(&p).map(|i| i + start) {
if let Some(i) = find_index(&mut cur, &p, start) {
col_no = i;
first_match = true;
break;
Expand All @@ -136,6 +139,10 @@ fn find_err(src: &str, path: impl Iterator<Item = String>) -> (usize, usize) {
(line_no, col_no)
}

fn find_index(cur: &str, pattern: &str, start: usize) -> Option<usize> {
cur.match_indices(pattern).find(|(i, _)| i >= &start).map(|(i, _)| i)
}

#[derive(Debug, PartialEq)]
pub struct FmlEditorError {
pub message: String,
Expand Down Expand Up @@ -182,24 +189,24 @@ mod unit_tests {
.get_feature_inspector("dialog-appearance".to_string())
.unwrap();

fn test_syntax_error(f: &FmlFeatureInspector, input: &str, highlight: bool) {
fn test_syntax_error(f: &FmlFeatureInspector, input: &str, col: u32, highlight: bool) {
if let Some(e) = f.get_first_error(input.to_string()) {
let highlight = if highlight { Some(input) } else { None };
assert_eq!(e, error("Need valid JSON object", 0, 0, highlight))
assert_eq!(e, error("Need valid JSON object", 0, col, highlight))
} else {
unreachable!("No error for \"{input}\"");
}
}

test_syntax_error(&f, "", false);
test_syntax_error(&f, "x", false);
test_syntax_error(&f, "{ \"\" }, ", false);
test_syntax_error(&f, "{ \"foo\":", false);
test_syntax_error(&f, "", 0, false);
test_syntax_error(&f, "x", 0, false);
test_syntax_error(&f, "{ \"\" }, ", 5, false);
test_syntax_error(&f, "{ \"foo\":", 7, false);

test_syntax_error(&f, "[]", true);
test_syntax_error(&f, "1", true);
test_syntax_error(&f, "true", true);
test_syntax_error(&f, "\"string\"", true);
test_syntax_error(&f, "[]", 0, true);
test_syntax_error(&f, "1", 0, true);
test_syntax_error(&f, "true", 0, true);
test_syntax_error(&f, "\"string\"", 0,true);

assert!(f.get_first_error("{}".to_string()).is_none());
Ok(())
Expand Down Expand Up @@ -268,33 +275,44 @@ mod unit_tests {

do_multi(
&[
"{", //
" boolean: true,", //
" object: {", //
" integer: \"string\"", //
" }", //
"}", //
"{", // 0
" boolean: true,", // 1
" object: {", // 2
" integer: \"string\"", // 3
" }", // 4
"}", // 5
],
&["object", "integer", "\"string\""],
(3, 13),
);

// patholgical case
// pathological case
do_multi(
&[
"{", //
" boolean: true,", //
" object: {", //
" integer: 1,", //
" astring: \"string\"", //
" },", //
" integer: \"string\"", //
"}", //
"{", // 0
" boolean: true,", // 1
" object: {", // 2
" integer: 1,", // 3
" astring: \"string\"", // 4
" },", // 5
" integer: \"string\"", // 6
"}", // 7
],
&["integer", "\"string\""],
(4, 13),
);

Ok(())
}

#[test]
fn test_find_index_from() -> Result<()> {
assert_eq!(find_index("012345601", "01", 0), Some(0));
assert_eq!(find_index("012345601", "01", 1), Some(7));
assert_eq!(find_index("012345602", "01", 1), None);

// TODO unicode indexing does not work.
// assert_eq!(find_index("åéîø token", "token", 0), Some(5));
Ok(())
}
}

0 comments on commit 1f8216d

Please sign in to comment.