bacon/result/
location.rs

1use {
2    lazy_regex::*,
3    std::{
4        path::PathBuf,
5        str::FromStr,
6    },
7};
8
9#[derive(Debug, Clone, PartialEq, Eq, Hash)]
10pub struct Location {
11    pub path: PathBuf,
12    pub line: usize,
13    pub column: Option<usize>,
14}
15
16impl FromStr for Location {
17    type Err = &'static str;
18    fn from_str(s: &str) -> Result<Self, Self::Err> {
19        let Some((_, path, line, column)) = regex_captures!(r#"^([^:\s]+):(\d+)(?:\:(\d+))?$"#, s,)
20        else {
21            return Err("invalid location format");
22        };
23        let line = line.parse().map_err(|_| "invalid line number")?; // too many digits
24        let column = column.parse().ok();
25        Ok(Self {
26            path: PathBuf::from(path),
27            line,
28            column,
29        })
30    }
31}