rune/workspace/
source_loader.rs

1use std::path::Path;
2
3use crate::ast::Span;
4use crate::compile::WithSpan;
5use crate::workspace::WorkspaceError;
6use crate::Source;
7
8use super::WorkspaceErrorKind;
9
10/// A source loader.
11pub trait SourceLoader {
12    /// Load the given path.
13    fn load(&mut self, span: Span, path: &Path) -> Result<Source, WorkspaceError>;
14}
15
16/// A filesystem-based source loader.
17#[derive(Default)]
18pub struct FileSourceLoader {}
19
20impl FileSourceLoader {
21    /// Construct a new filesystem-based source loader.
22    pub fn new() -> Self {
23        Self::default()
24    }
25}
26
27impl SourceLoader for FileSourceLoader {
28    fn load(&mut self, span: Span, path: &Path) -> Result<Source, WorkspaceError> {
29        match Source::from_path(path) {
30            Ok(source) => Ok(source),
31            Err(error) => Err(WorkspaceError::new(
32                span,
33                WorkspaceErrorKind::Source {
34                    path: path.try_into().with_span(span)?,
35                    error,
36                },
37            )),
38        }
39    }
40}