use ahash::AHashSet;
use alef_codegen::type_mapper::TypeMapper;
use alef_core::ir::PrimitiveType;
use std::borrow::Cow;
pub struct NapiMapper {
pub prefix: String,
pub trait_type_names: AHashSet<String>,
pub capsule_type_names: AHashSet<String>,
}
impl NapiMapper {
pub fn new(prefix: String) -> Self {
Self {
prefix,
trait_type_names: AHashSet::new(),
capsule_type_names: AHashSet::new(),
}
}
pub fn with_traits_and_capsules(
prefix: String,
trait_type_names: AHashSet<String>,
capsule_type_names: AHashSet<String>,
) -> Self {
Self {
prefix,
trait_type_names,
capsule_type_names,
}
}
}
impl TypeMapper for NapiMapper {
fn primitive(&self, prim: &PrimitiveType) -> Cow<'static, str> {
Cow::Borrowed(match prim {
PrimitiveType::Bool => "bool",
PrimitiveType::U8 => "u8",
PrimitiveType::U16 => "u16",
PrimitiveType::U32 => "u32",
PrimitiveType::U64 => "i64",
PrimitiveType::I8 => "i8",
PrimitiveType::I16 => "i16",
PrimitiveType::I32 => "i32",
PrimitiveType::I64 => "i64",
PrimitiveType::F32 => "f64", PrimitiveType::F64 => "f64",
PrimitiveType::Usize => "i64",
PrimitiveType::Isize => "i64",
})
}
fn named<'a>(&self, name: &'a str) -> Cow<'a, str> {
if self.trait_type_names.contains(name) {
Cow::Borrowed("JsVisitorRef")
} else if self.capsule_type_names.contains(name) {
Cow::Borrowed(name)
} else {
Cow::Owned(format!("{}{name}", self.prefix))
}
}
fn duration(&self) -> Cow<'static, str> {
Cow::Borrowed("i64")
}
fn json(&self) -> Cow<'static, str> {
Cow::Borrowed("serde_json::Value")
}
fn bytes(&self) -> Cow<'static, str> {
Cow::Borrowed("napi::bindgen_prelude::Buffer")
}
fn error_wrapper(&self) -> &str {
"Result"
}
}