Skip to main content

ocelot_base/
source_file.rs

1use crate::file_path::FilePath;
2use crate::shared_string::SharedString;
3
4/// Source file contents together with their logical path.
5#[derive(Debug, Clone, PartialEq, Eq, Default)]
6pub struct SourceFile {
7    pub path: FilePath,
8    pub source: SharedString,
9}
10
11impl SourceFile {
12    /// Creates a source file from its path and source contents.
13    pub fn new(path: impl Into<FilePath>, source: impl Into<SharedString>) -> Self {
14        Self {
15            path: path.into(),
16            source: source.into(),
17        }
18    }
19
20    /// Returns the source contents as a string slice.
21    pub fn source(&self) -> &str {
22        self.source.as_str()
23    }
24}
25
26#[cfg(test)]
27mod tests {
28    use super::SourceFile;
29
30    #[test]
31    fn source_file_stores_path_and_source() {
32        let source_file = SourceFile::new("examples/hello.ocelot", "println(\"hello\");");
33
34        assert_eq!(source_file.path.as_str(), "examples/hello.ocelot");
35        assert_eq!(source_file.source(), "println(\"hello\");");
36    }
37}