1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
use cubob::display_list_from_iter;
use std::{
    fmt::{Debug, Display, Formatter, Result as FmtResult},
    iter::FusedIterator,
};

#[derive(Clone, PartialEq, Eq)]
pub struct CodePlace {
    pub file: &'static str,
    pub line: u32,
    pub column: u32,
}

impl CodePlace {
    pub const fn new(file: &'static str, line: u32, column: u32) -> Self {
        Self { file, line, column }
    }
}

impl Display for CodePlace {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        write!(f, "{}:{}:{}", self.file, self.line, self.column)
    }
}

impl Debug for CodePlace {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        Display::fmt(self, f)
    }
}

#[macro_export]
macro_rules! place {
    () => {
        $crate::CodePlace::new(file!(), line!(), column!())
    };
}

#[derive(Clone, PartialEq, Eq)]
pub struct CodePlaceChain {
    head: CodePlace,
    tail: Option<Box<CodePlaceChain>>,
}

impl CodePlaceChain {
    pub fn prepend(self, place: CodePlace) -> Self {
        Self {
            head: place,
            tail: Some(Box::new(self)),
        }
    }

    pub fn prepend_mut(&mut self, place: CodePlace) -> &mut Self {
        let mut new_node = Self {
            head: place,
            tail: None,
        };
        std::mem::swap(self, &mut new_node);
        self.tail = Some(Box::new(new_node));
        self
    }
}

impl From<CodePlace> for CodePlaceChain {
    fn from(src: CodePlace) -> Self {
        Self {
            head: src,
            tail: None,
        }
    }
}

impl<'a> IntoIterator for &'a CodePlaceChain {
    type Item = &'a CodePlace;
    type IntoIter = CodePlaceChainIter<'a>;

    fn into_iter(self) -> Self::IntoIter {
        CodePlaceChainIter(Some(self))
    }
}

pub struct CodePlaceChainIter<'a>(Option<&'a CodePlaceChain>);

impl<'a> Iterator for CodePlaceChainIter<'a> {
    type Item = &'a CodePlace;

    fn next(&mut self) -> Option<Self::Item> {
        match self.0 {
            None => None,
            Some(entry) => {
                let current = &entry.head;
                self.0 = entry.tail.as_deref();
                Some(current)
            }
        }
    }
}

impl FusedIterator for CodePlaceChainIter<'_> {}

impl Display for CodePlaceChain {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        display_list_from_iter(f, self.into_iter())
    }
}

impl Debug for CodePlaceChain {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        Display::fmt(self, f)
    }
}

#[cfg(test)]
mod tests;