From 6b1058a26862c331f6d3bff7c146b92a44271e13 Mon Sep 17 00:00:00 2001 From: Andrew Toth Date: Mon, 11 Mar 2024 08:10:00 -0400 Subject: [PATCH] feat(napi-derive): add optional enum_string case conversion (#1995) --- crates/macro/src/parser/attrs.rs | 32 +++++++++- crates/macro/src/parser/mod.rs | 58 ++++++++++++------ .../__snapshots__/typegen.spec.ts.md | 6 ++ .../__snapshots__/typegen.spec.ts.snap | Bin 4708 -> 4743 bytes .../__snapshots__/values.spec.ts.snap | Bin 378 -> 380 bytes examples/napi/index.cjs | 1 + examples/napi/index.d.cts | 6 ++ examples/napi/src/enum.rs | 7 +++ 8 files changed, 91 insertions(+), 19 deletions(-) diff --git a/crates/macro/src/parser/attrs.rs b/crates/macro/src/parser/attrs.rs index f150597f..18d7ddd2 100644 --- a/crates/macro/src/parser/attrs.rs +++ b/crates/macro/src/parser/attrs.rs @@ -71,7 +71,7 @@ macro_rules! attrgen { (ts_return_type, TsReturnType(Span, String, Span)), (ts_type, TsType(Span, String, Span)), (ts_generic_types, TsGenericTypes(Span, String, Span)), - (string_enum, StringEnum(Span)), + (string_enum, StringEnum(Span, Option<(String, Span)>)), (use_nullable, UseNullable(Span, Option), false), // impl later @@ -132,6 +132,21 @@ macro_rules! methods { } }; + (@method $name:ident, $variant:ident(Span, Option<(String, Span)>)) => { + pub fn $name(&self) -> Option> { + self.attrs + .iter() + .filter_map(|a| match &a.1 { + BindgenAttr::$variant(_, s) => { + a.0.set(true); + Some(s.as_ref()) + } + _ => None, + }) + .next() + } + }; + (@method $name:ident, $variant:ident(Span, Option), $default_value:literal) => { pub fn $name(&self) -> bool { self.attrs @@ -396,6 +411,21 @@ impl Parse for BindgenAttr { return Ok(BindgenAttr::$variant(attr_span, val, span)) }); + (@parser $variant:ident(Span, Option<(String, Span)>)) => ({ + if let Ok(_) = input.parse::() { + let val = match input.parse::() { + Ok(str) => Some((str.value(), str.span())), + Err(_) => { + let ident = input.parse::()?.0; + Some((ident.to_string(), ident.span())) + } + }; + return Ok(BindgenAttr::$variant(attr_span, val)) + } else { + return Ok(BindgenAttr::$variant(attr_span, None)) + } + }); + (@parser $variant:ident(Span, Option), $default_value:literal) => ({ if let Ok(_) = input.parse::() { let (val, _) = match input.parse::() { diff --git a/crates/macro/src/parser/mod.rs b/crates/macro/src/parser/mod.rs index ff01a070..8951ffde 100644 --- a/crates/macro/src/parser/mod.rs +++ b/crates/macro/src/parser/mod.rs @@ -1062,26 +1062,48 @@ impl ConvertToAST for syn::ItemEnum { .map_or_else(|| self.ident.to_string(), |(s, _)| s.to_string()); let variants = match opts.string_enum() { - Some(_) => self - .variants - .iter() - .map(|v| { - if !matches!(v.fields, syn::Fields::Unit) { - bail_span!(v.fields, "Structured enum is not supported in #[napi]") + Some(case) => { + let case = case.map(|c| Ok::(match c.0.as_str() { + "lowercase" => Case::Flat, + "UPPERCASE" => Case::UpperFlat, + "PascalCase" => Case::Pascal, + "camelCase" => Case::Camel, + "snake_case" => Case::Snake, + "SCREAMING_SNAKE_CASE" => Case::UpperSnake, + "kebab-case" => Case::Kebab, + "SCREAMING-KEBAB-CASE" => Case::UpperKebab, + _ => { + bail_span!(self, "Unknown string enum case. Possible values are \"lowercase\", \"UPPERCASE\", \"PascalCase\", \"camelCase\", \"snake_case\", \"SCREAMING_SNAKE_CASE\", \"kebab-case\", or \"SCREAMING-KEBAB-CASE\"") } - if matches!(&v.discriminant, Some((_, _))) { - bail_span!( - v.fields, - "Literal values are not supported with string enum in #[napi]" - ) - } - Ok(NapiEnumVariant { - name: v.ident.clone(), - val: NapiEnumValue::String(v.ident.to_string()), - comments: extract_doc_comments(&v.attrs), + })).transpose()?; + + self + .variants + .iter() + .map(|v| { + if !matches!(v.fields, syn::Fields::Unit) { + bail_span!(v.fields, "Structured enum is not supported in #[napi]") + } + if matches!(&v.discriminant, Some((_, _))) { + bail_span!( + v.fields, + "Literal values are not supported with string enum in #[napi]" + ) + } + + let mut val = v.ident.to_string(); + if let Some(case) = case { + val = val.to_case(case) + }; + + Ok(NapiEnumVariant { + name: v.ident.clone(), + val: NapiEnumValue::String(val), + comments: extract_doc_comments(&v.attrs), + }) }) - }) - .collect::>>()?, + .collect::>>()? + } None => { let mut last_variant_val: i32 = -1; diff --git a/examples/napi/__tests__/__snapshots__/typegen.spec.ts.md b/examples/napi/__tests__/__snapshots__/typegen.spec.ts.md index ed0416e5..ab7a246a 100644 --- a/examples/napi/__tests__/__snapshots__/typegen.spec.ts.md +++ b/examples/napi/__tests__/__snapshots__/typegen.spec.ts.md @@ -617,6 +617,12 @@ Generated by [AVA](https://avajs.dev). name: string␊ }␊ ␊ + export const enum StringEnum {␊ + VariantOne = 'variantone',␊ + VariantTwo = 'varianttwo',␊ + VariantThree = 'variantthree'␊ + }␊ + ␊ export function sumBtreeMapping(nums: Record): number␊ ␊ export function sumIndexMapping(nums: Record): number␊ diff --git a/examples/napi/__tests__/__snapshots__/typegen.spec.ts.snap b/examples/napi/__tests__/__snapshots__/typegen.spec.ts.snap index 4855e659d0bf7d8c0d6853cd1af161607ccb46cc..37b58329a74a460b1933ddbb457e239846b1db9c 100644 GIT binary patch literal 4743 zcmV;25_s)FRzVHbuv}MPastCEe94XawYGC{ zjc-4{Dj$mo00000000B+Ty1Y0Ida}42miV7i#$uo3^+)tfh0moNHo!_C6E-RWXdvNiqoZ<3$6b2 zmo%lC{{AHUr*Hn{8~o=V{`s5V{NtyS2X_h0wYcYjQZiYQ%rZ-TjVbJDG^+0nI~GvpNLp$CODZ2Nq(|- zejihWKW*XCD+?cTwxF3=+=IJ!$s5K~BBq4e+jk#?QaEEg9hf0*^mxt`VT$NEgtJ?+ z;6|^c>=RBGFd#}x#%J3#AWEhb307nQdM;9nq%_qmAs0-~5h6g_9=HW;5i$d9uzTAc z9ZCJo`m8t_6%9wb6)Ar!z_|Vw=u6?lpuI+tflTc1zAwDzmw?F^) zf13Nakc+vX3szw;T)kcA{rmST%btOLA!cUb+oAOBY`+5ugi0VvEPy0)DrurYDnfZ$ zUJ?aA=f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{$5wB~hVZ*kJKnK=+PaV@NnS?MN_Ko^Khr~3)*J%Wm`e5b+B|Xq* zG4@QRe#lh>6Iv}iEtyX1W|1K46A(4}B8Bu-AD=w7zaL`;{Ei0^Dm>gbKh^C^@IClZ z-(&UTu{SAjEkixD@x&D(Ta_xCz@um>A~fwlfGO4Z?ho_J5mfO^la~r9EmZhL=maB` z1GdmU=>p6r6Dp)u*NzH#_i>JJX_eJ{p{Lsy{4}*z2=h(Lp>AuiEvI*Eim1b?uGmvN zbyFvs4~M7R=L3OaH`nzq0_4{!sM8qTr?#?~upM=>Nn~)NM^gNEk<^4BgpOwZ24Jw* z9mnTV%oDXBbjF>!(6v|qLmU;PSm%CE{StVl;&f%*xu?6ck+L=gcC_$J{&i+jnrFgB zynf&bE~)xPET|NfR9LAM$2n#wO!-t`N$PMJBn#tS@SMeD#WYu&^6m5D0~7PyWAihA zF&$5Wdsovle>MG1T{c-`IUQlD^>p1>*p-#fs$n&-gWi==43iV8?as>VNPXwshu9(PX3%v6s%VCrG2gb-a=>3oI6~1 z+Vv(Wiv9OEIkh03W~0t5$|tvQhE&+9ZL0sbV~XTliNgQXa>kV!khnhD5N1I$BesC+ecl8^sAAc<3`nAGn*F zCHN-nAgS4=iUCExFV^T_fJT4F?*ZGzuU0y~rScL!`>~UgTR$()@-x~W@j`!EsOs7_ zV^w#4v?J?(jzm-Iziy)`^S^adWkp4i_&j0@$da5US)%@`RVhgxzdLzzc6xL+b(W4Z m0nUp#LS-v0o_oDVBIPXGW#mL$Ib diff --git a/examples/napi/__tests__/__snapshots__/values.spec.ts.snap b/examples/napi/__tests__/__snapshots__/values.spec.ts.snap index d4241d4d7d0d6ebb4343f3ab1adc96ea29a9ee41..22206d792e490489581247c0692c5680fee4a4e0 100644 GIT binary patch literal 380 zcmV-?0fYWQRzV8k@#ITdcE-py zOSrHMh98Rv00000000A3kWWqmF%*Y?4;cRIK+uix2u`>H9K*y-nJH2%?WC^^Vadv! zcfiDjXYdl3cmc7uM$we0bK;M61`VL!q-ZduQ0Gl2<mekGCEQEg)|rPVA&LyF6~*eTv*Ve6vV zXvvzAoz3FD)I7DmP`nb|5}nQ}6E(-_BKRHaAVDHQUxIBtWLsQQRZ3t-U@s4zIn%BZ z)fLs%R_8HzC#EN+{eQf^@Pv)~%Ljs6r9W}Xk*k*&m6J73C%E>1RWi`! zF71*rT_1}G00000000A3kxfp+Fc3yPP1FC9R=@^v1fpJ{Jq8jRCvmBRYeyb8QI}n@ z;|@h)!5O#&w_wQ;U@Dj(BE7`=z9)HNAJ>_2`2!xFRnWnx5|uAhTB5TWt*mh40v?V< zA%nc_Tt}iWP7e%VR?xR39*I}to%keXAjA!^BJPNqcp_eiH{yf%BF>>Vhj`_^+yn3` zNfH4g0rZ}b(GeFA35e$~S<*B*^|;85{w)ExowaT7x-tsc=&*6w8jZwl>UTEitzs== zZMqEmO5wt|LgI$&F4xJj)jDhZ!oe%xJv>60D$|n$p8QV diff --git a/examples/napi/index.cjs b/examples/napi/index.cjs index cc92f5d1..c7c611ee 100644 --- a/examples/napi/index.cjs +++ b/examples/napi/index.cjs @@ -509,6 +509,7 @@ module.exports.roundtripStr = nativeBinding.roundtripStr module.exports.runScript = nativeBinding.runScript module.exports.setSymbolInObj = nativeBinding.setSymbolInObj module.exports.Status = nativeBinding.Status +module.exports.StringEnum = nativeBinding.StringEnum module.exports.sumBtreeMapping = nativeBinding.sumBtreeMapping module.exports.sumIndexMapping = nativeBinding.sumIndexMapping module.exports.sumMapping = nativeBinding.sumMapping diff --git a/examples/napi/index.d.cts b/examples/napi/index.d.cts index 24feb183..1ebc2ce4 100644 --- a/examples/napi/index.d.cts +++ b/examples/napi/index.d.cts @@ -607,6 +607,12 @@ export interface StrictObject { name: string } +export const enum StringEnum { + VariantOne = 'variantone', + VariantTwo = 'varianttwo', + VariantThree = 'variantthree' +} + export function sumBtreeMapping(nums: Record): number export function sumIndexMapping(nums: Record): number diff --git a/examples/napi/src/enum.rs b/examples/napi/src/enum.rs index 11435af7..7192662a 100644 --- a/examples/napi/src/enum.rs +++ b/examples/napi/src/enum.rs @@ -19,6 +19,13 @@ pub enum Status { Ready, } +#[napi(string_enum = "lowercase")] +pub enum StringEnum { + VariantOne, + VariantTwo, + VariantThree, +} + /// You could break the step and for an new continuous value. #[napi] pub enum CustomNumEnum {