use std::path::{Path, PathBuf};
use lsp_types::{Diagnostic, DiagnosticSeverity, NumberOrString, Position, Range, Url};
use crate::output::{OutputSeverity, RenderedDiagnostic};
pub fn uri_to_canonical_path(uri: &Url) -> Option<PathBuf> {
uri.to_file_path().ok()?.canonicalize().ok()
}
pub fn path_to_uri(path: &Path) -> Option<Url> {
Url::from_file_path(path).ok()
}
pub fn to_lsp_diagnostic(diagnostic: &RenderedDiagnostic) -> Diagnostic {
Diagnostic {
range: Range::new(
Position::new(diagnostic.line - 1, diagnostic.column - 1),
Position::new(diagnostic.end_line - 1, diagnostic.end_column - 1),
),
severity: Some(match diagnostic.severity {
OutputSeverity::Error => DiagnosticSeverity::ERROR,
OutputSeverity::Warn => DiagnosticSeverity::WARNING,
}),
code: Some(NumberOrString::String(diagnostic.rule_id.to_string())),
code_description: None,
source: Some("import-lint".to_string()),
message: diagnostic.message.clone(),
related_information: None,
tags: None,
data: None,
}
}