use std::{fmt::Debug, str::Utf8Error};
use serde::Deserialize;
pub enum Error {
NotFound,
Load(dlopen::Error),
GraalVM(i32),
Ffi(String),
YAMLScript(LibYSError),
Serde(serde_json::Error),
Utf8(Utf8Error),
}
impl Debug for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::NotFound => write!(
f,
"Shared library file 'libys.so.{0}' not found
Try: curl https://yamlscript.org/install | VERSION={0} LIB=1 bash
See: https://github.com/yaml/yamlscript/wiki/Installing-YAMLScript",
&super::LIBYS_VERSION
),
Error::Load(e) => write!(f, "Error::Load({e:?})"),
Error::GraalVM(e) => write!(f, "Error::GraalVM({e:?})"),
Error::Ffi(e) => write!(f, "Error::Ffi({e:?})"),
Error::YAMLScript(e) => write!(f, "Error::YAMLScript({e:?})"),
Error::Serde(e) => write!(f, "Error::Serde({e:?})"),
Error::Utf8(e) => write!(f, "Error::Utf8({e:?})"),
}
}
}
#[allow(clippy::module_name_repetitions)]
#[derive(Deserialize, Debug)]
pub struct LibYSError {
pub cause: String,
pub trace: Vec<(String, String, Option<String>, i64)>,
#[serde(rename = "type")]
pub type_: String,
}
impl From<dlopen::Error> for Error {
fn from(value: dlopen::Error) -> Self {
Self::Load(value)
}
}
impl From<serde_json::Error> for Error {
fn from(value: serde_json::Error) -> Self {
Self::Serde(value)
}
}
impl From<Utf8Error> for Error {
fn from(value: Utf8Error) -> Self {
Self::Utf8(value)
}
}