use std::collections::BTreeMap;
use serde::Deserialize;
use serde::Serialize;
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct SourceLocation {
pub file: String,
pub start: isize,
pub end: isize,
}
impl SourceLocation {
pub fn new(file: String) -> Self {
Self {
file,
start: -1,
end: -1,
}
}
pub fn new_with_offsets(file: String, start: isize, end: isize) -> Self {
Self { file, start, end }
}
pub fn try_from_ast(source: &str, id_paths: &BTreeMap<usize, &String>) -> Option<Self> {
let mut parts = source.split(':');
let start = parts
.next()
.map(|string| string.parse::<isize>())
.and_then(Result::ok)
.unwrap_or_default();
let length = parts
.next()
.map(|string| string.parse::<isize>())
.and_then(Result::ok)
.unwrap_or_default();
let path = parts
.next()
.and_then(|string| string.parse::<usize>().ok())
.and_then(|file_id| id_paths.get(&file_id))?;
Some(Self::new_with_offsets(
(*path).to_owned(),
start,
start + length,
))
}
}