mirror of https://github.com/zkat/miette.git
SourceCode provides access to bytes
This commit is contained in:
parent
3497508aa9
commit
7a3b008c06
|
|
@ -53,4 +53,8 @@ impl SourceCode for NamedSource {
|
|||
contents.line_count(),
|
||||
)))
|
||||
}
|
||||
|
||||
fn source_bytes(&self) -> &[u8] {
|
||||
self.inner().source_bytes()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -192,6 +192,15 @@ pub trait SourceCode: Send + Sync {
|
|||
context_lines_before: usize,
|
||||
context_lines_after: usize,
|
||||
) -> Result<Box<dyn SpanContents<'a> + 'a>, MietteError>;
|
||||
|
||||
/// Borrow the source bytes in their entirety.
|
||||
fn source_bytes(&self) -> &[u8];
|
||||
|
||||
/// Borrow the source bytes as a string slice.
|
||||
/// Returns None if the source is not a valid utf-8 string.
|
||||
fn source_utf8(&self) -> Result<&str, std::str::Utf8Error> {
|
||||
std::str::from_utf8(self.source_bytes())
|
||||
}
|
||||
}
|
||||
|
||||
/// A labeled [`SourceSpan`].
|
||||
|
|
|
|||
|
|
@ -106,6 +106,10 @@ impl SourceCode for [u8] {
|
|||
let contents = context_info(self, span, context_lines_before, context_lines_after)?;
|
||||
Ok(Box::new(contents))
|
||||
}
|
||||
|
||||
fn source_bytes(&self) -> &[u8] {
|
||||
&self[..]
|
||||
}
|
||||
}
|
||||
|
||||
impl<'src> SourceCode for &'src [u8] {
|
||||
|
|
@ -117,6 +121,12 @@ impl<'src> SourceCode for &'src [u8] {
|
|||
) -> Result<Box<dyn SpanContents<'a> + 'a>, MietteError> {
|
||||
<[u8] as SourceCode>::read_span(self, span, context_lines_before, context_lines_after)
|
||||
}
|
||||
|
||||
fn source_bytes(&self) -> &[u8] {
|
||||
self
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
impl SourceCode for Vec<u8> {
|
||||
|
|
@ -128,6 +138,10 @@ impl SourceCode for Vec<u8> {
|
|||
) -> Result<Box<dyn SpanContents<'a> + 'a>, MietteError> {
|
||||
<[u8] as SourceCode>::read_span(self, span, context_lines_before, context_lines_after)
|
||||
}
|
||||
|
||||
fn source_bytes(&self) -> &[u8] {
|
||||
self.as_slice()
|
||||
}
|
||||
}
|
||||
|
||||
impl SourceCode for str {
|
||||
|
|
@ -144,6 +158,10 @@ impl SourceCode for str {
|
|||
context_lines_after,
|
||||
)
|
||||
}
|
||||
|
||||
fn source_bytes(&self) -> &[u8] {
|
||||
self.as_bytes()
|
||||
}
|
||||
}
|
||||
|
||||
/// Makes `src: &'static str` or `struct S<'a> { src: &'a str }` usable.
|
||||
|
|
@ -156,6 +174,10 @@ impl<'s> SourceCode for &'s str {
|
|||
) -> Result<Box<dyn SpanContents<'a> + 'a>, MietteError> {
|
||||
<str as SourceCode>::read_span(self, span, context_lines_before, context_lines_after)
|
||||
}
|
||||
|
||||
fn source_bytes(&self) -> &[u8] {
|
||||
self.as_bytes()
|
||||
}
|
||||
}
|
||||
|
||||
impl SourceCode for String {
|
||||
|
|
@ -167,6 +189,10 @@ impl SourceCode for String {
|
|||
) -> Result<Box<dyn SpanContents<'a> + 'a>, MietteError> {
|
||||
<str as SourceCode>::read_span(self, span, context_lines_before, context_lines_after)
|
||||
}
|
||||
|
||||
fn source_bytes(&self) -> &[u8] {
|
||||
self.as_bytes()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ?Sized + SourceCode> SourceCode for Arc<T> {
|
||||
|
|
@ -179,6 +205,10 @@ impl<T: ?Sized + SourceCode> SourceCode for Arc<T> {
|
|||
self.as_ref()
|
||||
.read_span(span, context_lines_before, context_lines_after)
|
||||
}
|
||||
|
||||
fn source_bytes(&self) -> &[u8] {
|
||||
self.as_ref().source_bytes()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ?Sized + SourceCode + ToOwned> SourceCode for Cow<'_, T>
|
||||
|
|
@ -199,6 +229,10 @@ where
|
|||
self.as_ref()
|
||||
.read_span(span, context_lines_before, context_lines_after)
|
||||
}
|
||||
|
||||
fn source_bytes(&self) -> &[u8] {
|
||||
self.as_ref().source_bytes()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
|
|||
Loading…
Reference in New Issue