Skip to main content

rl_utils/
source.rs

1use std::sync::Arc;
2
3/// A named source file (or `<repl>` snippet) carried through each pipeline
4/// stage so error reports can quote the original source text.
5#[derive(Clone)]
6pub struct SourceFile {
7    /// The file name shown in error report headers (e.g. `"main.rl"`, `"<repl>"`).
8    pub name: Arc<str>,
9    /// The full source text, reference-counted to avoid cloning across pipeline stages.
10    pub text: Arc<String>,
11}
12
13impl SourceFile {
14    /// Creates a new [`SourceFile`] from a name and source text.
15    pub fn new(name: impl Into<Arc<str>>, text: impl Into<Arc<String>>) -> Self {
16        Self {
17            name: name.into(),
18            text: text.into(),
19        }
20    }
21}
22
23#[cfg(test)]
24mod tests {
25    use super::SourceFile;
26
27    #[test]
28    fn source_file_basic() {
29        let name = "main.rl";
30        let text = "println(\"foobar\")";
31
32        let source_file = SourceFile::new(name, text.to_string());
33
34        assert_eq!(&*source_file.name, name);
35        assert_eq!(&*source_file.text, text);
36    }
37}