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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
use std::fmt;
use std::str::FromStr;

fn parse_path(s: &str) -> Option<(&str, &str)> {
    let path = s.strip_prefix("[string \"")?;
    let (path, after) = path.split_once("\"]:")?;

    // Remove line number after any found colon, this may
    // exist if the source path is from a rust source file
    let path = match path.split_once(':') {
        Some((before, _)) => before,
        None => path,
    };

    Some((path, after))
}

fn parse_function_name(s: &str) -> Option<&str> {
    s.strip_prefix("in function '")
        .and_then(|s| s.strip_suffix('\''))
}

fn parse_line_number(s: &str) -> (Option<usize>, &str) {
    match s.split_once(':') {
        Some((before, after)) => (before.parse::<usize>().ok(), after),
        None => (None, s),
    }
}

/**
    Source of a stack trace line parsed from a [`LuaError`].
*/
#[derive(Debug, Default, Clone, Copy)]
pub enum StackTraceSource {
    /// Error originated from a C / Rust function.
    C,
    /// Error originated from a Lua (user) function.
    #[default]
    Lua,
}

/**
    Stack trace line parsed from a [`LuaError`].
*/
#[derive(Debug, Default, Clone)]
pub struct StackTraceLine {
    source: StackTraceSource,
    path: Option<String>,
    line_number: Option<usize>,
    function_name: Option<String>,
}

impl StackTraceLine {
    /**
        Returns the source of the stack trace line.
    */
    #[must_use]
    pub fn source(&self) -> StackTraceSource {
        self.source
    }

    /**
        Returns the path, if it exists.
    */
    #[must_use]
    pub fn path(&self) -> Option<&str> {
        self.path.as_deref()
    }

    /**
        Returns the line number, if it exists.
    */
    #[must_use]
    pub fn line_number(&self) -> Option<usize> {
        self.line_number
    }

    /**
        Returns the function name, if it exists.
    */
    #[must_use]
    pub fn function_name(&self) -> Option<&str> {
        self.function_name.as_deref()
    }
}

impl FromStr for StackTraceLine {
    type Err = String;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        if let Some(after) = s.strip_prefix("[C]: ") {
            let function_name = parse_function_name(after).map(ToString::to_string);

            Ok(Self {
                source: StackTraceSource::C,
                path: None,
                line_number: None,
                function_name,
            })
        } else if let Some((path, after)) = parse_path(s) {
            let (line_number, after) = parse_line_number(after);
            let function_name = parse_function_name(after).map(ToString::to_string);

            Ok(Self {
                source: StackTraceSource::Lua,
                path: Some(path.to_string()),
                line_number,
                function_name,
            })
        } else {
            Err(String::from("unknown format"))
        }
    }
}

impl fmt::Display for StackTraceLine {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if matches!(self.source, StackTraceSource::C) {
            write!(f, "Script '[C]'")?;
        } else {
            write!(f, "Script '{}'", self.path.as_deref().unwrap_or("[?]"))?;
            if let Some(line_number) = self.line_number {
                write!(f, ", Line {line_number}")?;
            }
        }
        if let Some(function_name) = self.function_name.as_deref() {
            write!(f, " - function '{function_name}'")?;
        }
        Ok(())
    }
}

/**
    Stack trace parsed from a [`LuaError`].
*/
#[derive(Debug, Default, Clone)]
pub struct StackTrace {
    lines: Vec<StackTraceLine>,
}

impl StackTrace {
    /**
        Returns the individual stack trace lines.
    */
    #[must_use]
    pub fn lines(&self) -> &[StackTraceLine] {
        &self.lines
    }
}

impl FromStr for StackTrace {
    type Err = String;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let (_, after) = s
            .split_once("stack traceback:")
            .ok_or_else(|| String::from("missing 'stack traceback:' prefix"))?;
        let lines = after
            .trim()
            .lines()
            .filter_map(|line| {
                let line = line.trim();
                if line.is_empty() {
                    None
                } else {
                    Some(line.parse())
                }
            })
            .collect::<Result<Vec<_>, _>>()?;
        Ok(StackTrace { lines })
    }
}