Skip to main content

shrub_core/
sourceinfo.rs

1//! Source info.
2
3use std::path::PathBuf;
4use std::sync::Arc;
5
6/// Source context.
7#[derive(Clone, Debug, Default)]
8#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
9pub struct SourceInfo {
10    /// The name of the source.
11    pub source: String,
12    /// Optionally indicates a starting location after the beginning of the
13    /// source. If `None`, the start is the beginning of the source.
14    pub start: Option<Arc<crate::SourcePosition>>,
15}
16
17impl From<&str> for SourceInfo {
18    fn from(source: &str) -> Self {
19        Self {
20            source: source.to_owned(),
21            start: None,
22        }
23    }
24}
25
26impl From<PathBuf> for SourceInfo {
27    fn from(path: PathBuf) -> Self {
28        Self {
29            source: path.to_string_lossy().to_string(),
30            start: None,
31        }
32    }
33}
34
35impl std::fmt::Display for SourceInfo {
36    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
37        write!(f, "{}", self.source)?;
38
39        if let Some(pos) = &self.start {
40            write!(f, ":{},{}", pos.line, pos.column)?;
41        }
42
43        Ok(())
44    }
45}