nvrim/diagnostics/sorter.rs
1//! Diagnostic sorting utilities.
2//!
3//! Supplies a severity sorter used by statusline / floating windows. Missing severities default to
4//! [`nvim_oxi::Integer::MIN`] to push them last relative to valid levels.
5
6use nvim_oxi::Dictionary;
7use nvim_oxi::Integer;
8
9/// Sorts diagnostics by severity.
10pub fn sort(mut lsp_diags: Vec<Dictionary>) -> Vec<Dictionary> {
11 lsp_diags.sort_by_key(get_severity_or_default);
12 lsp_diags
13}
14
15/// Gets the severity from a [`Dictionary`], defaulting to [`Integer::MIN`] if not present.
16fn get_severity_or_default(dict: &Dictionary) -> Integer {
17 dict.get("severity")
18 .map_or(Ok(Integer::MIN), |o| Integer::try_from(o.clone()))
19 .unwrap_or(Integer::MIN)
20}