Skip to content

Commit feeb66b

Browse files
jeffcharlesmatsadler
authored andcommitted
Fix error message when there's an invalid byte sequence
1 parent 6bbb0e2 commit feeb66b

2 files changed

Lines changed: 11 additions & 1 deletion

File tree

src/r_string.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1100,7 +1100,7 @@ impl RString {
11001100

11011101
unsafe fn as_str_unconstrained<'a>(self) -> Result<&'a str, Error> {
11021102
self.test_as_str_unconstrained().ok_or_else(|| {
1103-
let msg: Cow<'static, str> = if self.is_utf8_compatible_encoding() {
1103+
let msg: Cow<'static, str> = if !self.is_utf8_compatible_encoding() {
11041104
format!(
11051105
"expected utf-8, got {}",
11061106
RbEncoding::from(self.enc_get()).name()

tests/string.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,14 @@ fn it_converts_to_utf8_string() {
1010
let s = String::try_convert(val).unwrap();
1111

1212
assert_eq!("café", s);
13+
14+
let val: Value = magnus::eval!(r#""\xFF\xFF""#).unwrap();
15+
let err = String::try_convert(val).unwrap_err();
16+
17+
let expected_error = "invalid byte sequence in UTF-8";
18+
assert!(
19+
err.to_string().contains(expected_error),
20+
"Expected \"{}\" to contain \"{expected_error}\" but it didn't",
21+
err.to_string()
22+
);
1323
}

0 commit comments

Comments
 (0)