use alloc::boxed::Box;
use oxc_ast::ast::FunctionBody;
use vize_l0::Span;
use vize_l1::embed::{EmbedSource, SourceError, syntax::RetainedHandlerBody};
use crate::resolution::source::HandlerReferenceSource;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum HandlerInputErrorKind {
IncompleteSyntax,
}
#[derive(Debug)]
pub struct RejectedHandlerInput<'a> {
syntax: RetainedHandlerBody<'a>,
pub kind: HandlerInputErrorKind,
pub span: Span,
}
impl<'a> RejectedHandlerInput<'a> {
#[must_use]
pub const fn syntax(&self) -> &RetainedHandlerBody<'a> {
&self.syntax
}
#[must_use]
pub fn into_syntax(self) -> RetainedHandlerBody<'a> {
self.syntax
}
}
#[derive(Debug)]
pub struct HandlerInput<'a> {
syntax: RetainedHandlerBody<'a>,
body: &'a FunctionBody<'a>,
}
impl<'a> HandlerInput<'a> {
pub fn new(syntax: RetainedHandlerBody<'a>) -> Result<Self, Box<RejectedHandlerInput<'a>>> {
let Some(body) = HandlerReferenceSource::checked(&syntax).map(|source| source.body())
else {
return Err(Box::new(RejectedHandlerInput {
kind: HandlerInputErrorKind::IncompleteSyntax,
span: syntax.source().span(),
syntax,
}));
};
Ok(Self { syntax, body })
}
#[must_use]
pub const fn syntax(&self) -> &RetainedHandlerBody<'a> {
&self.syntax
}
#[must_use]
pub const fn source(&self) -> EmbedSource<'a> {
self.syntax.source()
}
#[must_use]
pub fn body(&self) -> &'a FunctionBody<'a> {
self.body
}
pub fn decoded_span(&self, span: oxc_span::Span) -> Result<Span, SourceError> {
self.references().decoded_span(span)
}
pub fn authored_span(&self, span: oxc_span::Span) -> Result<Span, SourceError> {
self.references().authored_span(span)
}
#[must_use]
pub fn into_syntax(self) -> RetainedHandlerBody<'a> {
self.syntax
}
pub(crate) fn references(&self) -> HandlerReferenceSource<'_, 'a> {
HandlerReferenceSource::from_input(self)
}
}
#[cfg(test)]
mod tests;
mod native;
pub use native::{NativeHandlerInput, RejectedNativeHandlerInput};