From bb670049840e64e96b9bf0bf72897d6f3a928194 Mon Sep 17 00:00:00 2001 From: Freya Murphy Date: Sun, 23 Nov 2025 14:50:31 -0500 Subject: audio: refactor everything --- audio/src/parse/util.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 audio/src/parse/util.rs (limited to 'audio/src/parse/util.rs') diff --git a/audio/src/parse/util.rs b/audio/src/parse/util.rs new file mode 100644 index 0000000..47779c2 --- /dev/null +++ b/audio/src/parse/util.rs @@ -0,0 +1,42 @@ +use super::{ParserError, lexer::Token, pos::Span}; +use std::fmt; + +impl std::error::Error for ParserError {} + +impl fmt::Display for ParserError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + if let Some(file) = &self.file { + write!(f, "{file} | ")?; + } + write!(f, "{} | {}", self.span, self.msg) + } +} + +pub trait SpanParserError { + type Output; + fn span_err(self, span: Span) -> Self::Output; +} + +impl SpanParserError for Result { + type Output = Result; + fn span_err(self, span: Span) -> Self::Output { + self.map_err(|e| ParserError { + span, + msg: e.to_string(), + file: None, + }) + } +} + +pub trait TokenError { + fn token_err(self, msg: impl Into) -> Result; +} +impl TokenError for Token<'_> { + fn token_err(self, msg: impl Into) -> Result { + Err(ParserError { + span: self.span, + msg: msg.into(), + file: None, + }) + } +} -- cgit v1.2.3-freya