We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
SourceCode
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 */ }
The text was updated successfully, but these errors were encountered:
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
Sorry, something went wrong.
No branches or pull requests
It would be grate to have at least basic (without caching and etc.) struct that implements
SourceCode
E.g:
The text was updated successfully, but these errors were encountered: