diff options
| author | Michael Williamson <mike@zwobble.org> | 2026-07-19 22:10:57 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2026-07-19 22:10:57 +0100 |
| commit | 7d100fcc4d6d33a48d43d4f115cb0fe957e3b9a5 (patch) | |
| tree | 6698775bf4a1889f9a7f6e18d394edbee4017347 /src/main/java/org/zwobble | |
| parent | 0a2046ff66bcac5c1d26178f3e4d60a5b00c826b (diff) | |
Don't panic when UTF-8 data is invalid
Diffstat (limited to 'src/main/java/org/zwobble')
| -rw-r--r-- | src/main/java/org/zwobble/hobgoblin/compiler/output/generators/rusttransient0/RustTransient0Generator.java | 31 |
1 files changed, 27 insertions, 4 deletions
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/rusttransient0/RustTransient0Generator.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/rusttransient0/RustTransient0Generator.java index b53345e..4a37e4a 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/rusttransient0/RustTransient0Generator.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/rusttransient0/RustTransient0Generator.java @@ -259,12 +259,35 @@ public class RustTransient0Generator implements Generator { generateReaderReadExact(RustPath.of(len)) ) ), - Optional.of(unwrap( + Optional.of( + new RustTryPropagationExpression(mapErr( + new RustCallExpression( + RustPath.global("std", "string", "String", "from_utf8"), + List.of(RustPath.of(bytes)) + ) + )) + ) + ) + ); + } + + private RustExpression mapErr(RustExpression result) { + var error = RustIdentifier.of("error"); + + return methodCall( + result, + RustIdentifier.of("map_err"), + List.of( + new RustClosureExpression( + List.of(new RustIdentifierPattern(error)), new RustCallExpression( - RustPath.global("std", "string", "String", "from_utf8"), - List.of(RustPath.of(bytes)) + RustPath.global("std", "io", "Error", "new"), + List.of( + RustPath.global("std", "io", "ErrorKind", "InvalidData"), + RustPath.of(error) + ) ) - )) + ) ) ); } |
