Handle json! panick trying to serialize non utf8 lines

Instead of panicking when trying to handle non utf8 lines we should
return `null` as line output.

This way we can on the frontend detect if a file has any lines the api
wasn't able to parse and fallback to show a file as a `Binary File`
This commit is contained in:
Sebastian Martinez
2025-03-19 11:12:04 -06:00
committed by Rūdolfs Ošiņš
parent 856caf0ec1
commit f2c7bb71c7
4 changed files with 12 additions and 9 deletions
+3 -3
View File
@@ -66,7 +66,7 @@ export type CommitBlob = z.infer<typeof commitBlobSchema>;
type AdditionHunkLine = z.infer<typeof additionHunkLineSchema>;
const additionHunkLineSchema = object({
line: string(),
line: string().nullable(),
lineNo: number(),
type: literal("addition"),
});
@@ -74,7 +74,7 @@ const additionHunkLineSchema = object({
type DeletionHunkLine = z.infer<typeof deletionHunkLineSchema>;
const deletionHunkLineSchema = object({
line: string(),
line: string().nullable(),
lineNo: number(),
type: literal("deletion"),
});
@@ -95,7 +95,7 @@ const diffFileSchema = object({
type ContextHunkLine = z.infer<typeof contextHunkLineSchema>;
const contextHunkLineSchema = object({
line: string(),
line: string().nullable(),
lineNoNew: number(),
lineNoOld: number(),
type: literal("context"),
+4 -4
View File
@@ -1,4 +1,4 @@
use radicle_surf as surf;
use radicle_surf::{self as surf};
use serde_json::{json, Value};
use radicle::cob;
@@ -194,14 +194,14 @@ impl<'a> Modification<'a> {
surf::diff::Modification::Addition(addition) => {
json!({
"type": "addition",
"line": addition.line,
"line": addition.line.clone().from_utf8().ok(),
"lineNo": addition.line_no
})
}
surf::diff::Modification::Deletion(deletion) => {
json!({
"type": "deletion",
"line": deletion.line,
"line": deletion.line.clone().from_utf8().ok(),
"lineNo": deletion.line_no
})
}
@@ -212,7 +212,7 @@ impl<'a> Modification<'a> {
} => {
json!({
"type": "context",
"line": line,
"line": line.clone().from_utf8().ok(),
"lineNoOld": line_no_old,
"lineNoNew": line_no_new
})
+1 -1
View File
@@ -12,7 +12,7 @@ pub(crate) enum Comment<'a> {
Issue(&'a cob::thread::Comment),
}
impl<'a> Comment<'a> {
impl Comment<'_> {
pub fn as_json(&self, id: &Oid, aliases: &impl AliasStore) -> Value {
match self {
Comment::Issue(c) => json!({
+4 -1
View File
@@ -39,6 +39,9 @@
let highlighting: { new?: string[]; old?: string[] } | undefined = undefined;
let syntaxHighlightingLoading: boolean = false;
let preview = false;
const binaryLines =
fileDiff.type === "plain" &&
fileDiff.hunks.some(h => h.lines.some(l => !l.line));
$: extension = filePath.split(".").pop();
onMount(() => {
@@ -449,7 +452,7 @@
</svelte:fragment>
<div class="container">
{#if fileDiff.type === "plain"}
{#if fileDiff.type === "plain" && !binaryLines}
{#if fileDiff.hunks.length > 0 && !preview}
<table class="diff" data-file-diff-select>
<tbody>