Skip to content

Commit b5c2136

Browse files
committed
fix(lsp): safely serialize CodeLens arguments
Replace unwrap() with ok() and pattern matching to gracefully handle serialization failures instead of panicking.
1 parent 25ea500 commit b5c2136

File tree

1 file changed

+53
-42
lines changed

1 file changed

+53
-42
lines changed

codeinput/src/lsp/server.rs

Lines changed: 53 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -362,21 +362,25 @@ impl LanguageServer for LspServer {
362362
.collect::<Vec<_>>()
363363
.join(", ");
364364

365-
lenses.push(CodeLens {
366-
range: Range {
367-
start: Position::new(0, 0),
368-
end: Position::new(0, 0),
369-
},
370-
command: Some(Command {
371-
title: format!("$(organization) {}", owners_str),
372-
command: "codeinput.showOwners".to_string(),
373-
arguments: Some(vec![
374-
serde_json::to_value(file_uri.to_string()).unwrap(),
375-
serde_json::to_value(info.owners).unwrap(),
376-
]),
377-
}),
378-
data: None,
379-
});
365+
// Safely serialize arguments
366+
let args = (
367+
serde_json::to_value(file_uri.to_string()).ok(),
368+
serde_json::to_value(&info.owners).ok(),
369+
);
370+
if let (Some(uri_val), Some(owners_val)) = args {
371+
lenses.push(CodeLens {
372+
range: Range {
373+
start: Position::new(0, 0),
374+
end: Position::new(0, 0),
375+
},
376+
command: Some(Command {
377+
title: format!("$(organization) {}", owners_str),
378+
command: "codeinput.showOwners".to_string(),
379+
arguments: Some(vec![uri_val, owners_val]),
380+
}),
381+
data: None,
382+
});
383+
}
380384
}
381385

382386
// Add tags CodeLens if any
@@ -388,37 +392,44 @@ impl LanguageServer for LspServer {
388392
.collect::<Vec<_>>()
389393
.join(", ");
390394

391-
lenses.push(CodeLens {
392-
range: Range {
393-
start: Position::new(0, 0),
394-
end: Position::new(0, 0),
395-
},
396-
command: Some(Command {
397-
title: format!("$(tag) {}", tags_str),
398-
command: "codeinput.showTags".to_string(),
399-
arguments: Some(vec![
400-
serde_json::to_value(file_uri.to_string()).unwrap(),
401-
serde_json::to_value(info.tags).unwrap(),
402-
]),
403-
}),
404-
data: None,
405-
});
395+
// Safely serialize arguments
396+
let args = (
397+
serde_json::to_value(file_uri.to_string()).ok(),
398+
serde_json::to_value(&info.tags).ok(),
399+
);
400+
if let (Some(uri_val), Some(tags_val)) = args {
401+
lenses.push(CodeLens {
402+
range: Range {
403+
start: Position::new(0, 0),
404+
end: Position::new(0, 0),
405+
},
406+
command: Some(Command {
407+
title: format!("$(tag) {}", tags_str),
408+
command: "codeinput.showTags".to_string(),
409+
arguments: Some(vec![uri_val, tags_val]),
410+
}),
411+
data: None,
412+
});
413+
}
406414
}
407415

408416
// Add unowned warning CodeLens
409417
if info.is_unowned {
410-
lenses.push(CodeLens {
411-
range: Range {
412-
start: Position::new(0, 0),
413-
end: Position::new(0, 0),
414-
},
415-
command: Some(Command {
416-
title: "$(warning) Unowned file".to_string(),
417-
command: "codeinput.addOwner".to_string(),
418-
arguments: Some(vec![serde_json::to_value(file_uri.to_string()).unwrap()]),
419-
}),
420-
data: None,
421-
});
418+
// Safely serialize arguments
419+
if let Some(uri_val) = serde_json::to_value(file_uri.to_string()).ok() {
420+
lenses.push(CodeLens {
421+
range: Range {
422+
start: Position::new(0, 0),
423+
end: Position::new(0, 0),
424+
},
425+
command: Some(Command {
426+
title: "$(warning) Unowned file".to_string(),
427+
command: "codeinput.addOwner".to_string(),
428+
arguments: Some(vec![uri_val]),
429+
}),
430+
data: None,
431+
});
432+
}
422433
}
423434

424435
return Ok(Some(lenses));

0 commit comments

Comments
 (0)