1 | use super::{add_alias, ToTypeDef, TypeDef}; |
2 | use crate::{js_doc_from_comments, NapiEnum, NapiEnumValue}; |
3 | |
4 | impl ToTypeDef for NapiEnum { |
5 | fn to_type_def(&self) -> Option<TypeDef> { |
6 | if self.skip_typescript { |
7 | return None; |
8 | } |
9 | |
10 | add_alias(self.name.to_string(), self.js_name.to_string()); |
11 | |
12 | Some(TypeDef { |
13 | kind: "enum" .to_owned(), |
14 | name: self.js_name.to_owned(), |
15 | original_name: Some(self.name.to_string()), |
16 | def: self.gen_ts_variants(), |
17 | js_doc: js_doc_from_comments(&self.comments), |
18 | js_mod: self.js_mod.to_owned(), |
19 | }) |
20 | } |
21 | } |
22 | |
23 | impl NapiEnum { |
24 | fn gen_ts_variants(&self) -> String { |
25 | self |
26 | .variants |
27 | .iter() |
28 | .map(|v| { |
29 | let val = match &v.val { |
30 | NapiEnumValue::Number(num) => format!(" {}" , num), |
31 | NapiEnumValue::String(string) => format!("' {}'" , string), |
32 | }; |
33 | format!(" {}{} = {}" , js_doc_from_comments(&v.comments), v.name, val) |
34 | }) |
35 | .collect::<Vec<_>>() |
36 | .join(sep:", \n " ) |
37 | } |
38 | } |
39 | |