napi-rs / napi-rs

A framework for building compiled Node.js add-ons in Rust via Node-API
https://napi.rs
Other
5.68k stars 243 forks source link

Export existing enum instead of redefining #1463

Open jowparks opened 1 year ago

jowparks commented 1 year ago

Ideally I would like to reexport an enum like so:

#[napi]
pub use bip39::Language as Language;

Currently I have to recreate the enum(along with a From<> conversion) in order to use that enum in my exported NAPI bindings:

pub use bip39::Language as Language;
#[napi]
pub enum LanguageCode {
    English,
    ChineseSimplified,
    ChineseTraditional,
    French,
    Italian,
    Japanese,
    Korean,
    Spanish,
}
impl From<LanguageCode> for Language {
    fn from(item: LanguageCode) -> Self {
        match item {
            LanguageCode::English => Language::English,
            LanguageCode::ChineseSimplified => Language::ChineseSimplified,
            LanguageCode::ChineseTraditional => Language::ChineseTraditional,
            LanguageCode::French => Language::French,
            LanguageCode::Italian => Language::Italian,
            LanguageCode::Japanese => Language::Japanese,
            LanguageCode::Korean => Language::Korean,
            LanguageCode::Spanish => Language::Spanish,
        }
    }
}
Brooooooklyn commented 1 year ago

@jowparks It's not possible for the derived macro, for now, I can only access the AST in the Rust code via #[napi], like pub use bip39::Language as Language; in your example, but I don't know what bip39::Language is in the compile time.

yuezk commented 1 year ago

Or can we generate the convert function so we don't have to write the boilerplate code?