1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
use std::fmt; use wain_ast::source::Source; pub fn describe_position(f: &mut fmt::Formatter<'_>, source: &str, start: usize) -> fmt::Result { if start == source.len() { write!(f, " caused at byte offset {} (end of input)", start) } else { let source = &source[start..]; let end = source .find(['\n', '\r'].as_ref()) .unwrap_or_else(|| source.len()); write!( f, " caused at byte offset {}\n\n ... {}\n ^\n starts from here", start, &source[..end], ) } } #[derive(Clone)] pub struct TextSource<'source>(pub(crate) &'source str); impl<'s> Source for TextSource<'s> { type Raw = &'s str; fn describe(&self, f: &mut fmt::Formatter<'_>, offset: usize) -> fmt::Result { describe_position(f, self.0, offset) } fn raw(&self) -> Self::Raw { self.0 } }