use std::path::PathBuf;
use serde::{Deserialize, Serialize};
#[derive(Clone, PartialEq, Serialize, Deserialize)]
pub struct Position {
pub start_offset: usize,
pub end_offset: usize,
pub line_number: usize,
pub end_line_number: usize,
pub path: PathBuf,
}
impl std::fmt::Debug for Position {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if std::env::var("VERBOSE").is_ok() {
f.debug_struct("Position")
.field("start_offset", &self.start_offset)
.field("end_offset", &self.end_offset)
.field("line_number", &self.line_number)
.field("end_line_number", &self.end_line_number)
.field("path", &self.path)
.finish()
} else {
if self.path == PathBuf::from("/position/todo") {
f.write_str("Position { TODO }")
} else {
f.write_str("Position { ... }")
}
}
}
}
impl Position {
pub fn todo() -> Self {
Self {
start_offset: 0,
end_offset: 0,
line_number: 0,
end_line_number: 0,
path: PathBuf::from("/position/todo"),
}
}
pub fn merge(first: &Self, second: &Self) -> Self {
Self {
start_offset: first.start_offset,
end_offset: second.end_offset,
line_number: first.line_number,
end_line_number: second.end_line_number,
path: first.path.clone(),
}
}
pub fn as_ide_string(&self) -> String {
format!(
"{}:{}",
self.path.display(),
self.line_number + 1,
)
}
pub fn contains_offset(&self, offset: usize) -> bool {
self.start_offset <= offset && offset <= self.end_offset
}
}