Skip to main content

factorio_ir/meta/
origin.rs

1//! Rust source origin for sourcemaps and debug adapters.
2
3use std::sync::Arc;
4
5/// Location in a Rust source file (1-based line/column).
6#[derive(Debug, Clone, PartialEq, Eq, Hash)]
7pub struct Origin {
8    pub file: Arc<str>,
9    pub line: u32,
10    pub column: u32,
11}
12
13impl Origin {
14    #[must_use]
15    pub fn new(file: impl Into<Arc<str>>, line: u32, column: u32) -> Self {
16        Self {
17            file: file.into(),
18            line,
19            column: column.max(1),
20        }
21    }
22
23    /// Format as `path:line:column` (or `path:line` when column is 1).
24    #[must_use]
25    pub fn display(&self) -> String {
26        if self.column <= 1 {
27            format!("{}:{}", self.file, self.line)
28        } else {
29            format!("{}:{}:{}", self.file, self.line, self.column)
30        }
31    }
32}