#[derive(thiserror::Error, Debug, Clone)]
#[non_exhaustive]
pub enum Error {
#[error("Initialization error: {0}")]
Initialization(String),
#[error("Shader error: {0}")]
Shader(String),
#[error("Resource error: {0}")]
Resource(String),
#[error("Data error: {0}")]
Data(String),
#[error("Event listener error: {0}")]
Callback(String),
#[error("Rasterization error: {0}")]
Rasterization(String),
}
impl Error {
pub(crate) fn window_not_found() -> Self {
Self::Initialization("Unable to retrieve window".to_string())
}
pub(crate) fn document_not_found() -> Self {
Self::Initialization("Unable to retrieve document".to_string())
}
pub(crate) fn canvas_not_found() -> Self {
Self::Initialization("Unable to retrieve canvas".to_string())
}
#[cfg(target_arch = "wasm32")]
pub(crate) fn webgl_context_failed() -> Self {
Self::Initialization("Failed to retrieve WebGL2 rendering context".to_string())
}
#[cfg(target_arch = "wasm32")]
pub(crate) fn canvas_context_failed() -> Self {
Self::Initialization("Failed to retrieve canvas rendering context".to_string())
}
pub(crate) fn rasterizer_canvas_creation_failed(detail: impl std::fmt::Display) -> Self {
Self::Rasterization(format!("Failed to create offscreen canvas: {detail}"))
}
pub(crate) fn rasterizer_context_failed() -> Self {
Self::Rasterization("Failed to get 2d rendering context from offscreen canvas".to_string())
}
pub(crate) fn rasterizer_fill_text_failed(
grapheme: &str,
detail: impl std::fmt::Display,
) -> Self {
Self::Rasterization(format!("Failed to render glyph '{grapheme}': {detail}"))
}
pub(crate) fn rasterizer_get_image_data_failed(detail: impl std::fmt::Display) -> Self {
Self::Rasterization(format!("Failed to read pixel data from canvas: {detail}"))
}
pub(crate) fn rasterizer_measure_failed(detail: impl std::fmt::Display) -> Self {
Self::Rasterization(format!("Failed to measure cell metrics: {detail}"))
}
pub(crate) fn rasterizer_empty_reference_glyph() -> Self {
Self::Rasterization(
"Reference glyph rasterization produced no pixels; cannot determine cell size"
.to_string(),
)
}
}
impl From<beamterm_core::Error> for Error {
fn from(err: beamterm_core::Error) -> Self {
match err {
beamterm_core::Error::Shader(msg) => Error::Shader(msg),
beamterm_core::Error::Resource(msg) => Error::Resource(msg),
beamterm_core::Error::Data(msg) => Error::Data(msg),
other => Error::Resource(other.to_string()),
}
}
}
impl From<Error> for wasm_bindgen::JsValue {
fn from(err: Error) -> Self {
wasm_bindgen::JsValue::from_str(&err.to_string())
}
}