summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--dhall/src/syntax/ast/span.rs12
-rw-r--r--serde_dhall/tests/de.rs3
2 files changed, 10 insertions, 5 deletions
diff --git a/dhall/src/syntax/ast/span.rs b/dhall/src/syntax/ast/span.rs
index 79bcc52..2e09863 100644
--- a/dhall/src/syntax/ast/span.rs
+++ b/dhall/src/syntax/ast/span.rs
@@ -75,13 +75,15 @@ impl Span {
}
/// Convert a byte idx into a string into a char idx for consumption by annotate_snippets.
+/// The byte idx must be at a char boundary.
fn char_idx_from_byte_idx(input: &str, idx: usize) -> usize {
+ use std::iter::once;
input
.char_indices()
+ .map(|(byte_i, _)| byte_i) // We don't care about the char
+ .chain(once(input.len())) // In case the idx points to the end of the string
.enumerate()
- .find(|(_, (i, _))| *i == idx)
- .map(|(i, (_, _))| i)
- // We should be able to unwrap() here, but somehow it panics on an example from
- // serde_dhall/lib.rs...
- .unwrap_or(0)
+ .find(|(_, byte_i)| *byte_i == idx)
+ .map(|(char_i, _)| char_i)
+ .unwrap()
}
diff --git a/serde_dhall/tests/de.rs b/serde_dhall/tests/de.rs
index 41a44bd..74912dd 100644
--- a/serde_dhall/tests/de.rs
+++ b/serde_dhall/tests/de.rs
@@ -82,4 +82,7 @@ fn test_de_untyped() {
parse::<BTreeMap<String, usize>>("{ x = 1, y = 2 }"),
expected_map
);
+
+ // https://github.com/Nadrieril/dhall-rust/issues/155
+ assert!(from_str::<bool>("List/length [True, 42]").is_err());
}