1use serde::{Deserialize, Serialize};
2use std::path::Path;
3
4use proc_macro2::Span as PmSpan;
5
6#[derive(Debug, Clone, Serialize, Deserialize)]
7pub struct Location {
8 pub line: u32,
9 pub column: u32,
10}
11
12#[derive(Debug, Clone, Serialize, Deserialize)]
13pub struct Span {
14 pub file: String,
15 pub start: Location,
16 pub end: Location,
17}
18
19impl Span {
20 #[must_use]
21 pub fn new(file: &Path, start: Location, end: Location) -> Self {
22 Self {
23 file: file.to_string_lossy().to_string(),
24 start,
25 end,
26 }
27 }
28
29 #[must_use]
30 pub fn from_pm_span(file: &Path, span: PmSpan) -> Self {
31 let start = span.start();
32 let end = span.end();
33 Self::new(
34 file,
35 Location {
36 line: start.line as u32,
37 column: (start.column as u32).saturating_add(1),
38 },
39 Location {
40 line: end.line as u32,
41 column: (end.column as u32).saturating_add(1),
42 },
43 )
44 }
45}