Skip to main content

brush_core/
sourceinfo.rs

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