Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SourceCode implementation for files #297

Open
gavrilikhin-d opened this issue Sep 30, 2023 · 1 comment
Open

Add SourceCode implementation for files #297

gavrilikhin-d opened this issue Sep 30, 2023 · 1 comment

Comments

@gavrilikhin-d
Copy link
Contributor

gavrilikhin-d commented Sep 30, 2023

It would be grate to have at least basic (without caching and etc.) struct that implements SourceCode

E.g:

struct SourceFile {
  path: PathBuf
}

impl SourceCode<'static> for SourceFile { /* Reads from file. With or without caching */ }
@gavrilikhin-d
Copy link
Contributor Author

This is the most basic implementation I came up with.

/// Wrapper around [`PathBuf`] that implements [`SourceCode`]
#[derive(Debug)]
pub struct SourceFile {
    /// Path to file
    path: PathBuf,
    /// File contents
    source: NamedSource,
}

impl Clone for SourceFile {
    fn clone(&self) -> Self {
        Self::with_path(&self.path).unwrap()
    }
}

impl PartialEq for SourceFile {
    fn eq(&self, other: &Self) -> bool {
        self.path == other.path
    }
}

impl SourceFile {
    /// Wrap path to source file
    pub fn with_path(path: impl Into<PathBuf>) -> io::Result<Self> {
        let path = path.into();
        let name = path.file_name().unwrap().to_string_lossy().to_string();
        let source = fs::read_to_string(&path)?;
        Ok(Self {
            path,
            source: NamedSource::new(name, source),
        })
    }

    /// Get path to the source file
    pub fn path(&self) -> &Path {
        &self.path
    }
}

impl SourceCode for SourceFile {
    fn read_span<'a>(
        &'a self,
        span: &miette::SourceSpan,
        context_lines_before: usize,
        context_lines_after: usize,
    ) -> Result<Box<dyn SpanContents<'a> + 'a>, MietteError> {
        self.source
            .read_span(span, context_lines_before, context_lines_after)
    }
}

I tried to make it smarter (with caching, cache invalidation and reading file on-demand) but struggled with lifetimes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant