1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
//! Functions to transpile Rust to TypeScript.

use crate::{join_path, near_syn::NearMethod, write_docs, NearImpl, NearSerde};
use std::ops::Deref;
use syn::{
    Attribute, Fields, ImplItem, ImplItemMethod, Item, ItemEnum, ItemImpl, ItemStruct,
    PathArguments, ReturnType, Type,
};

/// Represents a pass to several Rust files to generate TypeScript bindings.
pub struct TS<T> {
    /// Represents the name of the contract to export.
    pub name: String,
    /// interfaces
    pub interfaces: Vec<String>,
    /// view
    pub view_methods: Vec<String>,
    /// change
    pub change_methods: Vec<String>,
    /// Output buffer where to store the generated TypeScript bindings.
    pub buf: T,
}

macro_rules! ln {
    ($this:ident, $($arg:tt)*) => ({
        writeln!($this.buf, $($arg)*).unwrap()
    })
}

impl<T: std::io::Write> TS<T> {
    /// Creates a new `TS` instance.
    ///
    /// ```
    /// let mut ts = near_syn::ts::TS::new(Vec::new());
    /// assert_eq!(String::from_utf8_lossy(&ts.buf), "");
    /// ```
    pub fn new(buf: T) -> Self {
        Self {
            name: String::new(),
            interfaces: Vec::new(),
            view_methods: Vec::new(),
            change_methods: Vec::new(),
            buf,
        }
    }

    /// Exports common Near types.
    ///
    /// ```
    /// let mut ts = near_syn::ts::TS::new(Vec::new());
    /// ts.ts_prelude(" 2021".to_string(), "bin");
    /// assert_eq!(String::from_utf8_lossy(&ts.buf), format!(
    /// r#"// TypeScript bindings generated with bin v{} {} 2021
    ///
    /// // Exports common NEAR Rust SDK types
    /// export type U64 = string;
    /// export type I64 = string;
    /// export type U128 = string;
    /// export type I128 = string;
    /// export type AccountId = string;
    /// export type ValidAccountId = string;
    ///
    /// "#,
    ///   env!("CARGO_PKG_VERSION"),
    ///   env!("CARGO_PKG_REPOSITORY"),
    ///   ));
    /// ```
    pub fn ts_prelude(&mut self, now: String, bin_name: &str) {
        ln!(
            self,
            "// TypeScript bindings generated with {} v{} {}{}\n",
            bin_name,
            env!("CARGO_PKG_VERSION"),
            env!("CARGO_PKG_REPOSITORY"),
            now
        );

        ln!(self, "// Exports common NEAR Rust SDK types");
        ln!(self, "export type U64 = string;");
        ln!(self, "export type I64 = string;");
        ln!(self, "export type U128 = string;");
        ln!(self, "export type I128 = string;");
        ln!(self, "export type AccountId = string;");
        ln!(self, "export type ValidAccountId = string;");
        ln!(self, "");
    }

    /// Emits additional extensions for the main type implemented by the contract.
    /// This is used when the contract implements one or more `trait`s.
    /// The `name` and `interfaces` fields must be set in order to emit these additional extensions.
    ///
    /// ## Examples
    ///
    /// ```
    /// let mut ts = near_syn::ts::TS::new(Vec::new());
    /// ts.name = "Contract".to_string();
    /// ts.interfaces.push("NftCore".to_string());
    /// ts.interfaces.push("NftEnum".to_string());
    /// ts.ts_extend_traits();
    /// assert_eq!(String::from_utf8_lossy(&ts.buf), "export interface Contract extends NftCore, NftEnum {}\n\n");
    /// ```
    pub fn ts_extend_traits(&mut self) {
        if !self.name.is_empty() && !self.interfaces.is_empty() {
            ln!(
                self,
                "export interface {} extends {} {{}}\n",
                self.name,
                self.interfaces.join(", ")
            );
        }
    }

    /// Exports the methods object required by `near-api-js` to be able
    /// to find contract methods.
    ///
    /// ```
    /// let mut ts = near_syn::ts::TS::new(Vec::new());
    /// ts.name = "Contract".to_string();
    /// ts.view_methods.push("get".to_string());
    /// ts.change_methods.push("set".to_string());
    /// ts.change_methods.push("insert".to_string());
    /// ts.ts_contract_methods();
    /// assert_eq!(String::from_utf8_lossy(&ts.buf),
    /// r#"export const ContractMethods = {
    ///     viewMethods: [
    ///         "get",
    ///     ],
    ///     changeMethods: [
    ///         "set",
    ///         "insert",
    ///     ],
    /// };
    /// "#);
    /// ```
    ///
    /// Both `viewMethods` and `changeMethods` fields must be present in the
    /// resulting object.
    /// This is required by `near-api-js`.
    /// For example,
    ///
    /// ```
    /// let mut ts = near_syn::ts::TS::new(Vec::new());
    /// ts.name = "Contract".to_string();
    /// ts.view_methods.push("get".to_string());
    /// ts.ts_contract_methods();
    /// assert_eq!(String::from_utf8_lossy(&ts.buf),
    /// r#"export const ContractMethods = {
    ///     viewMethods: [
    ///         "get",
    ///     ],
    ///     changeMethods: [
    ///     ],
    /// };
    /// "#);
    /// ```
    pub fn ts_contract_methods(&mut self) {
        fn fmt(methods: &Vec<String>) -> String {
            methods
                .iter()
                .map(|m| format!("        {:?},\n", m))
                .collect::<Vec<String>>()
                .join("")
        }

        ln!(self, "export const {}Methods = {{", self.name);
        ln!(
            self,
            "    viewMethods: [\n{}    ],",
            fmt(&self.view_methods)
        );
        ln!(
            self,
            "    changeMethods: [\n{}    ],",
            fmt(&self.change_methods)
        );
        ln!(self, "}};");
    }

    /// Translates a collection of Rust items to TypeScript.
    /// It currently translates `type`, `struct`, `enum` and `impl` items to TypeScript.
    /// It traverses recursively `mod` definitions with braced content.
    /// The inner `mod`' items are flatten into a single TypeScript module.
    /// If an item in `items` is not one of the mentioned above, it is ignored.
    ///
    /// Notice how `mod` definitions are flattened:
    ///
    /// ```
    /// let mut ts = near_syn::ts::TS::new(Vec::new());
    /// let ast: syn::File = syn::parse2(quote::quote! {
    ///         /// Doc-comments are translated.
    ///         type T = u64;
    ///         mod inner_mod {
    ///             /// Doc-comments are translated.
    ///             type S = u64;
    ///         }
    ///     }).unwrap();
    /// ts.ts_items(&ast.items);
    /// assert_eq!(String::from_utf8_lossy(&ts.buf),
    /// r#"/**
    ///  *  Doc-comments are translated.
    ///  */
    /// export type T = number;
    ///
    /// /**
    ///  *  Doc-comments are translated.
    ///  */
    /// export type S = number;
    ///
    /// "#);
    /// ```
    ///
    /// ```
    /// let mut ts = near_syn::ts::TS::new(Vec::new());
    /// let ast: syn::File = syn::parse2(quote::quote! {
    ///         #[near_bindgen]
    ///         impl Contract {
    ///             pub fn get(&self) -> u32 { 42 }
    ///         }
    ///
    ///         #[near_bindgen]
    ///         impl NftCore for Contract {
    ///             fn f(&self) -> u32 { 42 }
    ///         }
    ///
    ///         #[near_bindgen]
    ///         impl NftEnum for Contract {
    ///             fn g(&self) -> u32 { 42 }
    ///         }
    ///
    ///     }).unwrap();
    /// ts.ts_items(&ast.items);
    /// ts.ts_extend_traits();
    /// assert_eq!(String::from_utf8_lossy(&ts.buf),
    /// r#"/**
    ///  */
    /// export interface Contract {
    ///     /**
    ///      */
    ///     get(): Promise<number>;
    ///
    /// }
    ///
    /// /**
    ///  */
    /// export interface NftCore {
    ///     /**
    ///      */
    ///     f(): Promise<number>;
    ///
    /// }
    ///
    /// /**
    ///  */
    /// export interface NftEnum {
    ///     /**
    ///      */
    ///     g(): Promise<number>;
    ///
    /// }
    ///
    /// export interface Contract extends NftCore, NftEnum {}
    ///
    /// "#);
    /// ```
    pub fn ts_items(&mut self, items: &Vec<Item>) {
        for item in items {
            match item {
                Item::Type(item_type) => self.ts_type(&item_type),
                Item::Struct(item_struct) => self.ts_struct(&item_struct),
                Item::Enum(item_enum) => self.ts_enum(&item_enum),
                Item::Impl(item_impl) => self.ts_impl(&item_impl),
                Item::Mod(item_mod) => {
                    if let Some((_, mod_items)) = &item_mod.content {
                        self.ts_items(mod_items);
                    }
                }
                _ => {}
            }
        }
    }

    /// Translates a type alias to another type alias in TypeScript.
    ///
    /// ```
    /// let mut ts = near_syn::ts::TS::new(Vec::new());
    /// ts.ts_type(&syn::parse2(quote::quote! {
    ///         /// Doc-comments are translated.
    ///         type T = u64;
    ///     }).unwrap());
    /// assert_eq!(String::from_utf8_lossy(&ts.buf),
    /// r#"/**
    ///  *  Doc-comments are translated.
    ///  */
    /// export type T = number;
    ///
    /// "#);
    /// ```
    ///
    /// If doc-comments are omitted,
    /// TypeScript empty doc-comments will be emitted.
    ///
    /// ```
    /// let mut ts = near_syn::ts::TS::new(Vec::new());
    /// ts.ts_type(&syn::parse2(quote::quote! {
    ///         type T = u64;
    ///     }).unwrap());
    /// assert_eq!(String::from_utf8_lossy(&ts.buf),
    /// r#"/**
    ///  */
    /// export type T = number;
    ///
    /// "#);
    /// ```
    pub fn ts_type(&mut self, item_type: &syn::ItemType) {
        self.ts_doc(&item_type.attrs, "");
        ln!(
            self,
            "export type {} = {};",
            item_type.ident,
            ts_type(&item_type.ty)
        );
        ln!(self, "");
    }

    /// Generates the corresponding TypeScript bindings for the given `struct`.
    /// Doc-comments embedded in the Rust source file are included in the bindings.
    /// The `struct` must derive `Serialize` from `serde` in order to
    /// generate its corresponding TypeScript bindings.
    ///
    /// ```
    /// let mut ts = near_syn::ts::TS::new(Vec::new());
    /// ts.ts_struct(&syn::parse2(quote::quote! {
    ///         /// Doc-comments are also translated.
    ///         #[derive(Serialize)]
    ///         struct A {
    ///             /// Doc-comments here are translated as well.
    ///             field: u32,
    ///         }
    ///     }).unwrap());
    /// assert_eq!(String::from_utf8_lossy(&ts.buf),
    /// r#"/**
    ///  *  Doc-comments are also translated.
    ///  */
    /// export type A = {
    ///     /**
    ///      *  Doc-comments here are translated as well.
    ///      */
    ///     field: number;
    ///
    /// }
    ///
    /// "#);
    /// ```
    ///
    /// Single-compoenent tuple-structs are converted to TypeScript type synonym.
    ///
    /// ```
    /// let mut ts = near_syn::ts::TS::new(Vec::new());
    /// ts.ts_struct(&syn::parse2(quote::quote! {
    ///         /// Tuple struct with one component.
    ///         #[derive(Serialize)]
    ///         struct T(String);
    ///     }).unwrap());
    /// assert_eq!(String::from_utf8_lossy(&ts.buf),
    /// r#"/**
    ///  *  Tuple struct with one component.
    ///  */
    /// export type T = string;
    ///
    /// "#);
    /// ```
    ///
    /// On the other hand,
    /// tuple-structs with more than one component,
    /// are converted to TypeScript proper tuples.
    ///
    /// ```
    /// let mut ts = near_syn::ts::TS::new(Vec::new());
    /// ts.ts_struct(&syn::parse2(quote::quote! {
    ///         /// Tuple struct with one component.
    ///         #[derive(Serialize)]
    ///         struct T(String, u32);
    ///     }).unwrap());
    /// assert_eq!(String::from_utf8_lossy(&ts.buf),
    /// r#"/**
    ///  *  Tuple struct with one component.
    ///  */
    /// export type T = [string, number];
    ///
    /// "#);
    /// ```
    ///
    /// If derive `Serialize` is not found, given `struct` is omitted.
    ///
    /// ```
    /// let mut ts = near_syn::ts::TS::new(Vec::new());
    /// ts.ts_struct(&syn::parse2(quote::quote! {
    ///         struct A { }
    ///     }).unwrap());
    /// assert_eq!(String::from_utf8_lossy(&ts.buf), "");
    /// ```
    pub fn ts_struct(&mut self, item_struct: &ItemStruct) {
        if !item_struct.is_serde() {
            return;
        }

        self.ts_doc(&item_struct.attrs, "");
        match &item_struct.fields {
            Fields::Named(fields) => {
                ln!(self, "export type {} = {{", item_struct.ident);
                for field in &fields.named {
                    let field_name = field.ident.as_ref().unwrap();
                    let ty = ts_type(&field.ty);
                    self.ts_doc(&field.attrs, "    ");
                    ln!(self, "    {}: {};\n", field_name, ty);
                }
                ln!(self, "}}");
                ln!(self, "");
            }
            Fields::Unnamed(fields) => {
                let mut tys = Vec::new();
                for field in &fields.unnamed {
                    let ty = ts_type(&field.ty);
                    tys.push(ty);
                }
                ln!(
                    self,
                    "export type {} = {};\n",
                    item_struct.ident,
                    if tys.len() == 1 {
                        tys.get(0).unwrap().clone()
                    } else {
                        format!("[{}]", tys.join(", "))
                    }
                );
            }
            Fields::Unit => panic!("unit struct no supported"),
        }
    }

    /// Translates an enum to a TypeScript `enum` or `type` according to the
    /// Rust definition.
    /// The Rust `enum` must derive `Serialize` from `serde` in order
    /// to be translated.
    ///
    /// For instance, a plain Rust `enum` will be translated to an `enum`.
    ///
    /// ```
    /// let mut ts = near_syn::ts::TS::new(Vec::new());
    /// ts.ts_enum(&syn::parse2(quote::quote! {
    ///         /// Doc-comments are translated.
    ///         #[derive(Serialize)]
    ///         enum E {
    ///             /// Doc-comments here are translated as well.
    ///             V1,
    ///         }
    ///     }).unwrap());
    /// assert_eq!(String::from_utf8_lossy(&ts.buf),
    /// r#"/**
    ///  *  Doc-comments are translated.
    ///  */
    /// export enum E {
    ///     /**
    ///      *  Doc-comments here are translated as well.
    ///      */
    ///     V1,
    ///
    /// }
    ///
    /// "#);
    /// ```
    pub fn ts_enum(&mut self, item_enum: &ItemEnum) {
        if !item_enum.is_serde() {
            return;
        }

        self.ts_doc(&item_enum.attrs, "");
        ln!(self, "export enum {} {{", item_enum.ident);
        for variant in &item_enum.variants {
            self.ts_doc(&variant.attrs, "    ");
            ln!(self, "    {},\n", variant.ident);
        }
        ln!(self, "}}\n");
    }

    /// Translates an `impl` section to a TypeScript `interface.`
    ///
    /// A `struct` can have multiple `impl` sections with no `trait` to declare additional methods.
    /// These `impl`s are emitted with the name of the contract,
    /// as TypeScript merges these definitions.
    ///
    /// ```
    /// let mut ts = near_syn::ts::TS::new(Vec::new());
    /// ts.ts_impl(&syn::parse2(quote::quote! {
    ///         /// Doc-comments are translated.
    ///         #[near_bindgen]
    ///         impl Contract {
    ///             /// Doc-comments here are translated as well.
    ///             pub fn get(&self) -> u32 { 42 }
    ///         }
    ///     }).unwrap());
    /// assert_eq!(String::from_utf8_lossy(&ts.buf),
    /// r#"/**
    ///  *  Doc-comments are translated.
    ///  */
    /// export interface Contract {
    ///     /**
    ///      *  Doc-comments here are translated as well.
    ///      */
    ///     get(): Promise<number>;
    ///
    /// }
    ///
    /// "#);
    /// ```
    pub fn ts_impl(&mut self, item_impl: &ItemImpl) {
        if !item_impl.is_bindgen() || !item_impl.has_exported_methods() {
            return;
        }

        self.ts_doc(&item_impl.attrs, "");
        if let Some((_excl, trait_path, _for)) = &item_impl.trait_ {
            let trait_name = join_path(trait_path);
            self.interfaces.push(trait_name.clone());
            ln!(self, "export interface {} {{", trait_name);
        } else {
            if let syn::Type::Path(type_path) = &*item_impl.self_ty {
                self.name = join_path(&type_path.path);
                ln!(self, "export interface {} {{", self.name);
            } else {
                panic!("name not found")
            }
        }

        {
            for item in item_impl.items.iter() {
                if let ImplItem::Method(method) = item {
                    if method.is_exported(item_impl) {
                        if !method.is_init() {
                            if method.is_mut() {
                                &mut self.change_methods
                            } else {
                                &mut self.view_methods
                            }
                            .push(method.sig.ident.to_string());
                        }
                        self.ts_doc(&method.attrs, "    ");
                        ln!(self, "    {}\n", ts_sig(&method));
                    }
                }
            }
        }

        ln!(self, "}}\n");
    }

    fn ts_doc(&mut self, attrs: &Vec<Attribute>, indent: &str) {
        ln!(self, "{}/**", indent);
        write_docs(&mut self.buf, attrs, |l| format!("{} * {}", indent, l));
        ln!(self, "{} */", indent);
    }
}

/// Return the TypeScript equivalent type of the Rust type represented by `ty`.
/// Rust primitives types and `String` are included.
///
/// ```
/// use syn::parse_str;
/// use near_syn::ts::ts_type;
///
/// assert_eq!(ts_type(&parse_str("bool").unwrap()), "boolean");
/// assert_eq!(ts_type(&parse_str("i8").unwrap()), "number");
/// assert_eq!(ts_type(&parse_str("u8").unwrap()), "number");
/// assert_eq!(ts_type(&parse_str("i16").unwrap()), "number");
/// assert_eq!(ts_type(&parse_str("u16").unwrap()), "number");
/// assert_eq!(ts_type(&parse_str("i32").unwrap()), "number");
/// assert_eq!(ts_type(&parse_str("u32").unwrap()), "number");
/// assert_eq!(ts_type(&parse_str("String").unwrap()), "string");
/// ```
///
/// Rust standard and collections types, *e.g.*, `Option`, `Vec` and `HashMap`,
/// are included in the translation.
///
/// ```
/// # use syn::parse_str;
/// # use near_syn::ts::ts_type;
/// assert_eq!(ts_type(&parse_str("Option<U64>").unwrap()), "U64|null");
/// assert_eq!(ts_type(&parse_str("Option<String>").unwrap()), "string|null");
/// assert_eq!(ts_type(&parse_str("Vec<ValidAccountId>").unwrap()), "ValidAccountId[]");
/// assert_eq!(ts_type(&parse_str("HashSet<ValidAccountId>").unwrap()), "ValidAccountId[]");
/// assert_eq!(ts_type(&parse_str("BTreeSet<ValidAccountId>").unwrap()), "ValidAccountId[]");
/// assert_eq!(ts_type(&parse_str("HashMap<AccountId, U128>").unwrap()), "Record<AccountId, U128>");
/// assert_eq!(ts_type(&parse_str("BTreeMap<AccountId, U128>").unwrap()), "Record<AccountId, U128>");
/// ```
///
/// Rust nested types are converted to TypeScript as well.
///
/// ```
/// # use syn::parse_str;
/// # use near_syn::ts::ts_type;
/// assert_eq!(ts_type(&parse_str("HashMap<AccountId, Vec<U128>>").unwrap()), "Record<AccountId, U128[]>");
/// assert_eq!(ts_type(&parse_str("Vec<Option<U128>>").unwrap()), "(U128|null)[]");
/// assert_eq!(ts_type(&parse_str("Option<Vec<U128>>").unwrap()), "U128[]|null");
/// assert_eq!(ts_type(&parse_str("Option<Option<U64>>").unwrap()), "U64|null|null");
/// assert_eq!(ts_type(&parse_str("Vec<Vec<U64>>").unwrap()), "U64[][]");
/// assert_eq!(ts_type(&parse_str("(U64)").unwrap()), "U64");
/// assert_eq!(ts_type(&parse_str("(U64, String, Vec<u32>)").unwrap()), "[U64, string, number[]]");
///
/// assert_eq!(ts_type(&parse_str("()").unwrap()), "void");
/// // assert_eq!(ts_type(&parse_str("std::vec::Vec<U64>").unwrap()), "U64[]");
/// ```
///
/// ## Panics
///
/// Panics when standard library generics types are used incorrectly.
/// For example `Option` or `HashMap<U64>`.
/// This situation can only happen on Rust source files that were **not** type-checked by `rustc`.
pub fn ts_type(ty: &Type) -> String {
    #[derive(PartialEq, PartialOrd)]
    enum Assoc {
        Single,
        Vec,
        Or,
    }
    fn single(ts: &str) -> (String, Assoc) {
        (ts.to_string(), Assoc::Single)
    }
    fn use_paren(ta: (String, Assoc), assoc: Assoc) -> String {
        if ta.1 > assoc {
            format!("({})", ta.0)
        } else {
            ta.0
        }
    }
    fn gen_args<'a>(p: &'a syn::TypePath, nargs: usize, name: &str) -> Vec<&'a Type> {
        if let PathArguments::AngleBracketed(args) = &p.path.segments[0].arguments {
            if args.args.len() != nargs {
                panic!(
                    "{} expects {} generic(s) argument(s), found {}",
                    name,
                    nargs,
                    args.args.len()
                );
            }
            let mut result = Vec::new();
            for arg in &args.args {
                if let syn::GenericArgument::Type(tk) = arg {
                    result.push(tk);
                } else {
                    panic!("No type provided for {}", name);
                }
            }
            result
        } else {
            panic!("{} used with no generic arguments", name);
        }
    }

    fn ts_type_assoc(ty: &Type) -> (String, Assoc) {
        match ty {
            Type::Path(p) => match crate::join_path(&p.path).as_str() {
                "bool" => single("boolean"),
                "u64" => single("number"),
                "i8" | "u8" | "i16" | "u16" | "i32" | "u32" => single("number"),
                "String" => single("string"),
                "Option" => {
                    let targs = gen_args(p, 1, "Option");
                    let ta = ts_type_assoc(&targs[0]);
                    (format!("{}|null", use_paren(ta, Assoc::Or)), Assoc::Or)
                }
                "Vec" | "HashSet" | "BTreeSet" => {
                    let targs = gen_args(p, 1, "Vec");
                    let ta = ts_type_assoc(&targs[0]);
                    (format!("{}[]", use_paren(ta, Assoc::Vec)), Assoc::Vec)
                }
                "HashMap" | "BTreeMap" => {
                    let targs = gen_args(p, 2, "HashMap");
                    let (tks, _) = ts_type_assoc(&targs[0]);
                    let (tvs, _) = ts_type_assoc(&targs[1]);
                    (format!("Record<{}, {}>", tks, tvs), Assoc::Single)
                }
                s => single(s),
            },
            Type::Paren(paren) => ts_type_assoc(paren.elem.as_ref()),
            Type::Tuple(tuple) => {
                if tuple.elems.is_empty() {
                    ("void".into(), Assoc::Single)
                } else {
                    let mut tys = Vec::new();
                    for elem_type in &tuple.elems {
                        let (t, _) = ts_type_assoc(&elem_type);
                        tys.push(t);
                    }
                    (format!("[{}]", tys.join(", ")), Assoc::Single)
                }
            }
            _ => panic!("type not supported"),
        }
    }
    ts_type_assoc(ty).0
}

/// Returns the signature of the given Rust `method`.
/// The resulting TypeScript binding is a valid method definition expected by the NEAR RPC.
/// Thus, the following conversion are applied:
/// - Function arguments are packed into a single TypeScript object argument
/// - Return type is wrapped into a `Promise`
/// - Types are converted using `ts_type`
///
/// ## Examples
///
/// ```
/// use syn::parse_str;
/// use near_syn::ts::ts_sig;
///
/// assert_eq!(ts_sig(&parse_str("fn a() {}").unwrap()), "a(): Promise<void>;");
/// assert_eq!(ts_sig(&parse_str("fn b(x: U128) {}").unwrap()), "b(args: { x: U128 }): Promise<void>;");
/// assert_eq!(ts_sig(&parse_str("fn c(x: U128, y: String) -> Vec<Token> {}").unwrap()), "c(args: { x: U128, y: string }): Promise<Token[]>;");
/// assert_eq!(ts_sig(&parse_str("fn d(x: U128, y: String, z: Option<U64>) -> Vec<Token> {}").unwrap()), "d(args: { x: U128, y: string, z: U64|null }): Promise<Token[]>;");
/// assert_eq!(ts_sig(&parse_str("fn e(x: U128) -> () {}").unwrap()), "e(args: { x: U128 }): Promise<void>;");
/// assert_eq!(ts_sig(&parse_str("fn f(paren: (String)) {}").unwrap()), "f(args: { paren: string }): Promise<void>;");
/// assert_eq!(ts_sig(&parse_str("fn get(&self) -> u32 {}").unwrap()), "get(): Promise<number>;");
/// assert_eq!(ts_sig(&parse_str("fn set(&mut self) {}").unwrap()), "set(gas?: any): Promise<void>;");
/// assert_eq!(ts_sig(&parse_str("fn set_args(&mut self, x: u32) {}").unwrap()), "set_args(args: { x: number }, gas?: any): Promise<void>;");
/// assert_eq!(ts_sig(&parse_str("fn a() -> Promise {}").unwrap()), "a(): Promise<void>;");
/// ```
pub fn ts_sig(method: &ImplItemMethod) -> String {
    let mut args = Vec::new();
    for arg in method.sig.inputs.iter() {
        match arg {
            syn::FnArg::Typed(pat_type) => {
                if let syn::Pat::Ident(pat_ident) = pat_type.pat.deref() {
                    let type_name = ts_type(&pat_type.ty);
                    let arg_ident = &pat_ident.ident;
                    args.push(format!("{}: {}", arg_ident, type_name));
                }
            }
            _ => {}
        }
    }

    if method.is_init() {
        format!("{}: {{ {} }};", method.sig.ident, args.join(", "),)
    } else {
        let ret_type = match &method.sig.output {
            ReturnType::Default => "void".into(),
            ReturnType::Type(_, typ) => {
                let ty = ts_type(typ.deref());
                if ty == "Promise" {
                    "void".to_string()
                } else {
                    ty
                }
            }
        };

        let mut args_decl = Vec::new();
        if args.len() > 0 {
            args_decl.push(format!("args: {{ {} }}", args.join(", ")));
        };
        if method.is_mut() {
            args_decl.push("gas?: any".into());
        }
        if method.is_payable() {
            args_decl.push("amount?: any".into());
        }

        format!(
            "{}({}): Promise<{}>;",
            method.sig.ident,
            args_decl.join(", "),
            ret_type
        )
    }
}

#[cfg(test)]
mod tests {

    use crate::ts::ts_type;

    #[test]
    #[should_panic(expected = "Option used with no generic arg")]
    fn ts_type_on_option_with_no_args_should_panic() {
        ts_type(&syn::parse_str("Option").unwrap());
    }

    #[test]
    #[should_panic(expected = "Option expects 1 generic(s) argument(s), found 2")]
    fn ts_type_on_option_with_more_than_one_arg_should_panic() {
        ts_type(&syn::parse_str("Option<String, U128>").unwrap());
    }

    #[test]
    #[should_panic(expected = "Vec used with no generic arg")]
    fn ts_type_on_vec_with_no_args_should_panic() {
        ts_type(&syn::parse_str("Vec").unwrap());
    }

    #[test]
    #[should_panic(expected = "Vec expects 1 generic(s) argument(s), found 3")]
    fn ts_type_on_vec_with_more_than_one_arg_should_panic() {
        ts_type(&syn::parse_str("Vec<String, U128, u32>").unwrap());
    }

    #[test]
    #[should_panic(expected = "HashMap used with no generic arguments")]
    fn ts_type_on_hashmap_with_no_args_should_panic() {
        ts_type(&syn::parse_str("HashMap").unwrap());
    }

    #[test]
    #[should_panic(expected = "HashMap expects 2 generic(s) argument(s), found 1")]
    fn ts_type_on_hashmap_with_less_than_two_args_should_panic() {
        ts_type(&syn::parse_str("HashMap<U64>").unwrap());
    }
}