aboutsummaryrefslogtreecommitdiff
path: root/vendor/miette/src/named_source.rs
diff options
context:
space:
mode:
authorValentin Popov <valentin@popov.link>2024-01-08 00:21:28 +0300
committerValentin Popov <valentin@popov.link>2024-01-08 00:21:28 +0300
commit1b6a04ca5504955c571d1c97504fb45ea0befee4 (patch)
tree7579f518b23313e8a9748a88ab6173d5e030b227 /vendor/miette/src/named_source.rs
parent5ecd8cf2cba827454317368b68571df0d13d7842 (diff)
downloadfparkan-1b6a04ca5504955c571d1c97504fb45ea0befee4.tar.xz
fparkan-1b6a04ca5504955c571d1c97504fb45ea0befee4.zip
Initial vendor packages
Signed-off-by: Valentin Popov <valentin@popov.link>
Diffstat (limited to 'vendor/miette/src/named_source.rs')
-rw-r--r--vendor/miette/src/named_source.rs61
1 files changed, 61 insertions, 0 deletions
diff --git a/vendor/miette/src/named_source.rs b/vendor/miette/src/named_source.rs
new file mode 100644
index 0000000..31ad1d1
--- /dev/null
+++ b/vendor/miette/src/named_source.rs
@@ -0,0 +1,61 @@
+use crate::{MietteError, MietteSpanContents, SourceCode, SpanContents};
+
+/// Utility struct for when you have a regular [`SourceCode`] type that doesn't
+/// implement `name`. For example [`String`]. Or if you want to override the
+/// `name` returned by the `SourceCode`.
+pub struct NamedSource {
+ source: Box<dyn SourceCode + 'static>,
+ name: String,
+}
+
+impl std::fmt::Debug for NamedSource {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ f.debug_struct("NamedSource")
+ .field("name", &self.name)
+ .field("source", &"<redacted>");
+ Ok(())
+ }
+}
+
+impl NamedSource {
+ /// Create a new `NamedSource` using a regular [`SourceCode`] and giving
+ /// its returned [`SpanContents`] a name.
+ pub fn new(name: impl AsRef<str>, source: impl SourceCode + Send + Sync + 'static) -> Self {
+ Self {
+ source: Box::new(source),
+ name: name.as_ref().to_string(),
+ }
+ }
+
+ /// Gets the name of this `NamedSource`.
+ pub fn name(&self) -> &str {
+ &self.name
+ }
+
+ /// Returns a reference the inner [`SourceCode`] type for this
+ /// `NamedSource`.
+ pub fn inner(&self) -> &(dyn SourceCode + 'static) {
+ &*self.source
+ }
+}
+
+impl SourceCode for NamedSource {
+ fn read_span<'a>(
+ &'a self,
+ span: &crate::SourceSpan,
+ context_lines_before: usize,
+ context_lines_after: usize,
+ ) -> Result<Box<dyn SpanContents<'a> + 'a>, MietteError> {
+ let contents = self
+ .inner()
+ .read_span(span, context_lines_before, context_lines_after)?;
+ Ok(Box::new(MietteSpanContents::new_named(
+ self.name.clone(),
+ contents.data(),
+ *contents.span(),
+ contents.line(),
+ contents.column(),
+ contents.line_count(),
+ )))
+ }
+}