Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

2.9 KB
Raw Blame
use crate::{Context, WebIdlError};

/// Converts one JavaScript value into a concrete WebIDL boundary type.
///
/// Implementations should perform the WebIDL conversion operation for one
/// value and return a `WebIdlError` with the supplied `Context` when conversion
/// fails. If conversion triggers a JavaScript exception, return
/// `WebIdlError::pending_exception(context)` after rethrowing through V8 so the
/// binding can preserve the original exception.
pub trait WebIdlConverter<'s>: Sized {
    /// Per-converter options supplied by derive attributes or explicit helpers.
    ///
    /// Most numeric and raw converters use `()`. String converters use
    /// `StringOptions` for WebIDL extended attributes such as
    /// `TreatNullAs=EmptyString`.
    type Options: Default;

    fn convert(
        scope: &mut v8::PinScope<'s, '_>,
        value: v8::Local<'s, v8::Value>,
        context: Context,
        options: &Self::Options,
    ) -> Result<Self, WebIdlError>;
}

/// Parses a WebIDL enum token after string conversion.
///
/// The derive uses `EnumValue<T>` as the converter wrapper for enum fields.
/// Implementations should accept exactly the lowercase/spec tokens exposed by
/// the relevant Web API and return `None` for invalid tokens so callers get a
/// consistent `InvalidEnum` WebIDL error.
pub trait WebIdlEnum: Sized {
    const NAME: &'static str;

    fn parse_token(value: &str) -> Option<Self>;
}

/// Parses a V8 native binding's positional argument list into a Rust struct.
///
/// Implementations are normally generated by `#[derive(WebIdlArgs)]`. The
/// generated parser handles positional indexes, required arguments, optional
/// defaults, nullable arguments, variadics, explicit converters, and custom
/// field parsers.
pub trait WebIdlArguments<'s>: Sized {
    fn parse_arguments(
        scope: &mut v8::PinScope<'s, '_>,
        args: &v8::FunctionCallbackArguments<'s>,
    ) -> Result<Self, WebIdlError>;
}

/// Parses a WebIDL dictionary object into a Rust struct.
///
/// Implementations are normally generated by `#[derive(WebIdlDictionary)]`.
/// Member access uses ordinary V8 property reads so page-defined getters,
/// inherited dictionary members, and thrown exceptions are observed at the
/// WebIDL boundary.
pub trait WebIdlDictionary<'s>: Sized {
    fn parse_dictionary(
        scope: &mut v8::PinScope<'s, '_>,
        object: v8::Local<'s, v8::Object>,
    ) -> Result<Self, WebIdlError>;
}

/// Outcome for custom parsers that may intentionally skip a value.
///
/// This is useful when a hand-written parser needs to distinguish "the value was
/// absent or intentionally ignored" from "the value was present and parsed".
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum ParseOutcome<T> {
    Parsed(T),
    Skip,
}

impl<T> ParseOutcome<T> {
    pub fn into_option(self) -> Option<T> {
        match self {
            Self::Parsed(value) => Some(value),
            Self::Skip => None,
        }
    }
}