use nom_locate::LocatedSpan;
pub type Span<'a> = LocatedSpan<&'a str>;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SpanInfo {
pub start: usize,
pub end: usize,
pub start_line: usize,
pub start_column: usize,
pub end_line: usize,
pub end_column: usize,
}
impl SpanInfo {
pub fn from_span(span: Span) -> Self {
let offset = span.location_offset();
let line = span.location_line() as usize;
let column = span.get_column().saturating_sub(1);
Self {
start: offset,
end: offset,
start_line: line,
start_column: column,
end_line: line,
end_column: column,
}
}
pub fn from_range(start_span: Span, end_span: Span) -> Self {
let start_offset = start_span.location_offset();
let end_offset = end_span.location_offset();
let start_line = start_span.location_line() as usize;
let start_column = start_span.get_column().saturating_sub(1);
let end_line = end_span.location_line() as usize;
let end_column = end_span.get_column().saturating_sub(1);
Self {
start: start_offset,
end: end_offset,
start_line,
start_column,
end_line,
end_column,
}
}
pub fn from_span_and_len(start_span: Span, content_len: usize) -> Self {
let start_offset = start_span.location_offset();
let end_offset = start_offset + content_len;
let start_line = start_span.location_line() as usize;
let start_column = start_span.get_column().saturating_sub(1);
let end_line = start_line;
let end_column = start_column + content_len;
Self {
start: start_offset,
end: end_offset,
start_line,
start_column,
end_line,
end_column,
}
}
pub fn len(&self) -> usize {
self.end - self.start
}
pub fn is_empty(&self) -> bool {
self.start == self.end
}
}