nativeview-codegen 0.1.0-rc.1

Multi-language ABI, policy, flat-op codec, and hybrid Direct-FFI code generator for Native View Engine.
Documentation
use std::fmt;

/// Error returned by the bi-directional RN <-> NV code generator.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CodegenError {
    /// Lexical or syntactic error while parsing a React Native TypeScript/Flow spec.
    Parse {
        line: usize,
        col: usize,
        message: String,
    },
    /// Semantic or validation error in a parsed RN spec or `#[nv_hybrid_view]` spec.
    Validation(String),
    /// Filesystem I/O failure when reading specs or writing generated outputs.
    Io { path: String, message: String },
    /// This build was compiled from other templates than the checkout's (a stale cached build).
    StaleTemplates(crate::TemplateDrift),
}

impl CodegenError {
    pub fn parse(line: usize, col: usize, message: impl Into<String>) -> Self {
        Self::Parse {
            line,
            col,
            message: message.into(),
        }
    }

    pub fn validation(message: impl Into<String>) -> Self {
        Self::Validation(message.into())
    }

    pub fn io(path: impl Into<String>, err: impl fmt::Display) -> Self {
        Self::Io {
            path: path.into(),
            message: err.to_string(),
        }
    }

    pub fn io_path(path: &std::path::Path, err: impl fmt::Display) -> Self {
        Self::io(path.display().to_string(), err)
    }
}

impl fmt::Display for CodegenError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Parse { line, col, message } => {
                write!(f, "spec parse error at {line}:{col}: {message}")
            }
            Self::Validation(msg) => write!(f, "spec validation error: {msg}"),
            Self::Io { path, message } => write!(f, "I/O error for '{path}': {message}"),
            Self::StaleTemplates(drift) => drift.fmt(f),
        }
    }
}

impl From<nativeview_hybrid_spec::SpecError> for CodegenError {
    fn from(err: nativeview_hybrid_spec::SpecError) -> Self {
        match err {
            nativeview_hybrid_spec::SpecError::Parse {
                line, col, message, ..
            } => Self::Parse { line, col, message },
            nativeview_hybrid_spec::SpecError::Validation { message, .. } => Self::Validation(message),
            nativeview_hybrid_spec::SpecError::Io { path, message } => Self::Io { path, message },
        }
    }
}

impl std::error::Error for CodegenError {}