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
//! This crate provide core types of ethbind generation system
//!
//!
//!

use std::{
    collections::HashMap,
    fs::{self, read_to_string},
    path::Path,
    str::FromStr,
};

use ethbind_json::{
    AbiField, Array, ArrayM, Constructor, Error, Event, FixedMN, Function, HardhatArtifact,
    IntegerM, Type,
};
use serde::{Deserialize, Serialize};
use thiserror::Error;

/// Typed **bind** error
#[derive(Debug, Error)]
pub enum BindError {
    /// This error is returned by [`RuntimeBinder`] to indicate
    /// that a runtime type binding for the contract type was not found.
    #[error("Runtime binder didn't found mapping runtime type for {0}")]
    UnknownType(String),
}

/// ABI data structure that can be generated into arbitrary programming language supported by `Ethbind`.
///
/// `Ethbind` provides `Generatable` implementations for types of crate [`ethbind-json`](https://docs.rs/ethbind-json)
pub trait Generatable {
    /// Generate abi data to `Target` programming language code.
    fn generate<C: Context>(&self, context: &mut C) -> anyhow::Result<()>;
}

/// `Ethbind` code generation system `Context` instance
pub trait Context {
    /// Target programming language runtime/strongly typed binder
    type Runtime: RuntimeBinder;

    /// `Target` programming language code generator
    type Language: Generator;

    /// Get context binding programming language [`Generator`] and [`runtime binder`](RuntimeBinder)
    fn get_mut(&mut self) -> (&mut Self::Language, &mut Self::Runtime);

    fn finalize(self) -> (Self::Language, Self::Runtime);
}

/// Binder for mapping contract type system to `target` programming language runtime type system.
pub trait RuntimeBinder {
    /// Convert contract [`abi type`](Type) to `runtime` type string
    ///
    /// If the parameter `type` is tuple or array of tuple returns [`None`]
    fn to_runtime_type(&mut self, r#type: &Type) -> anyhow::Result<Option<&str>>;

    /// Get runtime type by metadata `name`, If not found the implementation must return [`Err(BindError::UnknownType)`]
    fn get(&mut self, name: &str) -> anyhow::Result<&str>;
}

/// Programming language code generator supported by `Ethbind`.
///
/// The implementation must support multi-round generation process.
pub trait Generator {
    /// [`Generatable`] or `Executor` call this fn to start a new contract generation round.
    fn begin<R: RuntimeBinder>(&mut self, runtime_binder: &mut R, name: &str)
        -> anyhow::Result<()>;

    /// Close current generation contract round.
    fn end<R: RuntimeBinder>(&mut self, runtime_binder: &mut R, name: &str) -> anyhow::Result<()>;

    /// Generate contract method ,call this fn after call [`begin`](Generator::begin) at least once.
    fn generate_fn<R: RuntimeBinder>(
        &mut self,
        runtime_binder: &mut R,
        r#fn: &Function,
    ) -> anyhow::Result<()>;

    /// Generate contract deploy method ,call this fn after call [`begin`](Generator::begin) at least once.
    fn generate_deploy<R: RuntimeBinder>(
        &mut self,
        runtime_binder: &mut R,
        contructor: &Constructor,
        deploy_bytes: &str,
    ) -> anyhow::Result<()>;

    /// Generate event handle interface ,call this fn after call [`begin`](Generator::begin) at least once.
    fn generate_event<R: RuntimeBinder>(
        &mut self,
        runtime_binder: &mut R,
        event: &Event,
    ) -> anyhow::Result<()>;

    /// Generate error handle interface ,call this fn after call [`begin`](Generator::begin) at least once.
    fn generate_error<R: RuntimeBinder>(
        &mut self,
        runtime_binder: &mut R,
        error: &Error,
    ) -> anyhow::Result<()>;

    /// Close generator and return generated contract codes.
    fn finalize<R: RuntimeBinder>(self, runtime_binder: &mut R) -> anyhow::Result<Vec<Contract>>;
}

/// Generated contract files archive
pub struct Contract {
    pub files: Vec<File>,
}

/// Generated codes and file name
pub struct File {
    pub name: String,
    pub data: String,
}

pub trait SaveTo {
    fn save_to<P: AsRef<Path>>(&self, output_dir: P) -> anyhow::Result<()>;
}

impl SaveTo for Contract {
    /// Write generated contract codes to file system.
    fn save_to<P: AsRef<Path>>(&self, output_dir: P) -> anyhow::Result<()> {
        if !output_dir.as_ref().exists() {
            fs::create_dir_all(&output_dir)?;
        }

        for file in &self.files {
            let path = output_dir.as_ref().join(&file.name);

            fs::write(path, &file.data)?;
        }

        Ok(())
    }
}

impl SaveTo for Vec<Contract> {
    fn save_to<P: AsRef<Path>>(&self, output_dir: P) -> anyhow::Result<()> {
        for c in self {
            c.save_to(&output_dir)?;
        }

        Ok(())
    }
}

/// A [`RuntimeBinder`] implementation which load runtime types mapping metadata from json.
#[derive(Debug, Serialize, Deserialize)]
pub struct JsonRuntimeBinder {
    #[serde(flatten)]
    runtime_types: HashMap<String, String>,

    #[serde(skip)]
    components: HashMap<String, String>,
}

impl FromStr for JsonRuntimeBinder {
    type Err = serde_json::Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        serde_json::from_str(s)
    }
}

impl TryFrom<&str> for JsonRuntimeBinder {
    type Error = serde_json::Error;

    fn try_from(value: &str) -> Result<Self, Self::Error> {
        serde_json::from_str(value.as_ref())
    }
}

impl TryFrom<String> for JsonRuntimeBinder {
    type Error = serde_json::Error;

    fn try_from(value: String) -> Result<Self, Self::Error> {
        serde_json::from_str(value.as_ref())
    }
}

impl TryFrom<&[u8]> for JsonRuntimeBinder {
    type Error = serde_json::Error;

    fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
        serde_json::from_slice(value)
    }
}

impl JsonRuntimeBinder {
    /// Create `JsonRuntimeBinder` from [`Reader`](std::io::Read)
    pub fn from_reader<R>(reader: R) -> anyhow::Result<Self>
    where
        R: std::io::Read,
    {
        Ok(serde_json::from_reader(reader)?)
    }

    /// Try load `JsonRuntimeBinder` from json file with `path` parameter
    pub fn load<P: AsRef<Path>>(path: P) -> anyhow::Result<Self> {
        Ok(fs::read_to_string(path)?.try_into()?)
    }

    /// Try map contract abi basic types's `type_name` to [`runtime type`](JsonRuntimeType), if not found returns `Err(EthBindError::UnknownType)`
    fn search_basic_type<T: AsRef<str>>(&self, type_name: T) -> anyhow::Result<&str> {
        self.runtime_types
            .get(type_name.as_ref())
            .map(|c| c.as_str())
            .ok_or(BindError::UnknownType(type_name.as_ref().to_owned()).into())
    }

    fn to_array_m(&mut self, element: &ArrayM) -> anyhow::Result<Option<&str>> {
        let tag = element.to_string();

        // Returns exists runtime type
        if self.components.contains_key(&tag) {
            return Ok(self.components.get(&tag).map(|c| c.as_str()));
        }

        let el_type = { self.to_runtime_type(&element.element)?.map(|c| c.clone()) };

        if el_type.is_none() {
            return Ok(None);
        }

        let el_type = el_type.unwrap().to_owned();

        let runtime_type = self.search_basic_type("array_m")?;

        let m = element.m;

        let declare_type = runtime_type
            .replace("$el", &el_type)
            .replace("$m", &m.to_string());

        self.components.insert(tag.clone(), declare_type);

        Ok(self.components.get(&tag).map(|c| c.as_str()))
    }

    fn to_bytes_m(&mut self, m: usize) -> anyhow::Result<&str> {
        let tag = format!("array{}", m);

        // Returns exists runtime type
        if self.components.contains_key(&tag) {
            return Ok(self.components.get(&tag).unwrap());
        }

        let runtime_type = self.search_basic_type("bytes_m")?;

        let declare_type = runtime_type.replace("$m", &m.to_string());

        self.components.insert(tag.clone(), declare_type);

        Ok(self.components.get(&tag).unwrap())
    }

    fn to_array(&mut self, element: &Array) -> anyhow::Result<Option<&str>> {
        let tag = element.to_string();

        // Returns exists runtime type
        if self.components.contains_key(&tag) {
            return Ok(self.components.get(&tag).map(|c| c.as_str()));
        }

        let el_type = { self.to_runtime_type(&element.element)?.map(|c| c.clone()) };

        if el_type.is_none() {
            return Ok(None);
        }

        let el_type = el_type.unwrap().to_owned();

        let runtime_type = { self.search_basic_type("array")? };

        let declare_type = runtime_type.replace("$el", &el_type);

        self.components.insert(tag.clone(), declare_type);

        Ok(self.components.get(&tag).map(|c| c.as_str()))
    }

    fn to_fixed_m_n(&mut self, fixed: &FixedMN) -> anyhow::Result<&str> {
        let tag = fixed.to_string();

        // Returns exists runtime type
        if self.components.contains_key(&tag) {
            return Ok(self.components.get(&tag).unwrap());
        }

        let runtime_type = self.search_basic_type("fixed_m_n")?;

        let declare_type = runtime_type
            .replace("$m", &fixed.m.to_string())
            .replace("$n", &fixed.n.to_string());

        self.components.insert(tag.clone(), declare_type);

        Ok(self.components.get(&tag).unwrap())
    }

    fn to_integer_m(&mut self, integer_m: &IntegerM) -> anyhow::Result<&str> {
        let tag = integer_m.to_string();

        // Returns exists runtime type
        if self.components.contains_key(&tag) {
            return Ok(self.components.get(&tag).unwrap());
        }

        let runtime_type = if integer_m.signed {
            self.search_basic_type("int_m")?
        } else {
            self.search_basic_type("uint_m")?
        };

        let declare_type = runtime_type.replace("$m", &integer_m.m.to_string());

        self.components.insert(tag.clone(), declare_type);

        Ok(self.components.get(&tag).unwrap())
    }
}

impl RuntimeBinder for JsonRuntimeBinder {
    fn to_runtime_type(&mut self, r#type: &Type) -> anyhow::Result<Option<&str>> {
        match r#type {
            Type::Simple(element) if element.is_tuple() => Ok(None),
            Type::Simple(element) => self.search_basic_type(element.to_string()).map(|c| Some(c)),
            Type::ArrayM(element) => self.to_array_m(&element),
            Type::Array(element) => self.to_array(&element),
            Type::BytesM(element) => self.to_bytes_m(element.m).map(|c| Some(c)),
            Type::FixedMN(element) => self.to_fixed_m_n(element).map(|c| Some(c)),
            Type::IntegerM(element) => self.to_integer_m(element).map(|c| Some(c)),
        }
    }

    fn get(&mut self, name: &str) -> anyhow::Result<&str> {
        Ok(self
            .runtime_types
            .get(name)
            .ok_or(BindError::UnknownType(name.to_string()))?)
    }
}

/// `Ethbind` executor
pub struct Executor<L, R> {
    generator: L,
    runtime_binder: R,
}

impl<L, R> Context for Executor<L, R>
where
    R: RuntimeBinder,
    L: Generator,
{
    type Language = L;
    type Runtime = R;

    fn get_mut(&mut self) -> (&mut Self::Language, &mut Self::Runtime) {
        (&mut self.generator, &mut self.runtime_binder)
    }

    fn finalize(self) -> (Self::Language, Self::Runtime) {
        (self.generator, self.runtime_binder)
    }
}

impl<L, R> From<(L, R)> for Executor<L, R>
where
    R: RuntimeBinder,
    L: Generator,
{
    fn from(value: (L, R)) -> Self {
        Executor {
            generator: value.0,
            runtime_binder: value.1,
        }
    }
}

pub struct BindingBuilder<C: Context> {
    context: C,
    builders: Vec<Box<dyn Fn(&mut C) -> anyhow::Result<()>>>,
}

impl<C: Context + Default> Default for BindingBuilder<C> {
    fn default() -> Self {
        Self {
            context: Default::default(),
            builders: Default::default(),
        }
    }
}

impl<C: Context> BindingBuilder<C> {
    /// Create new binder builder with providing [`generator`](Generator)
    pub fn new<C1: Into<C>>(context: C1) -> Self {
        Self {
            context: context.into(),
            builders: Default::default(),
        }
    }

    /// Generate binding codes with contract/abi data
    pub fn bind<S: AsRef<str> + 'static, CN: AsRef<str>>(
        mut self,
        contract_name: CN,
        contract: S,
    ) -> Self {
        let contract_name = contract_name.as_ref().to_string();

        self.builders.push(Box::new(move |c| {
            let fields: Vec<AbiField> = serde_json::from_str(contract.as_ref())?;

            {
                let (generator, runtime_binder) = c.get_mut();

                generator.begin(runtime_binder, &contract_name)?;
            }

            fields.generate(c)?;

            Ok(())
        }));

        self
    }

    /// Generate binding codes with hardhat artifact data
    pub fn bind_hardhat<S: AsRef<str> + 'static>(mut self, contract: S) -> Self {
        self.builders.push(Box::new(move |c| {
            let fields: HardhatArtifact = serde_json::from_str(contract.as_ref())?;

            {
                let (generator, runtime_binder) = c.get_mut();

                generator.begin(runtime_binder, &fields.contract_name)?;
            }

            fields.generate(c)?;

            Ok(())
        }));

        self
    }

    /// Generate binding codes with contract/abi file path
    pub fn bind_file<P: AsRef<Path> + 'static, CN: AsRef<str>>(
        mut self,
        contract_name: CN,
        path: P,
    ) -> Self {
        let contract_name = contract_name.as_ref().to_string();

        self.builders.push(Box::new(move |c| {
            let contract = read_to_string(&path)?;

            let fields: Vec<AbiField> = serde_json::from_str(contract.as_ref())?;

            {
                let (generator, runtime_binder) = c.get_mut();

                generator.begin(runtime_binder, &contract_name)?;
            }

            fields.generate(c)?;

            Ok(())
        }));

        self
    }

    /// Generate binding codes with hardhat artifact file path
    pub fn bind_hardhat_file<P: AsRef<Path> + 'static>(mut self, path: P) -> Self {
        self.builders.push(Box::new(move |c| {
            let contract = read_to_string(&path)?;

            let fields: HardhatArtifact = serde_json::from_str(contract.as_ref())?;

            {
                let (generator, runtime_binder) = c.get_mut();

                generator.begin(runtime_binder, &fields.contract_name)?;
            }

            fields.generate(c)?;

            Ok(())
        }));

        self
    }

    /// Retrieve [`result`](Generator) and consume binding builder instance.
    pub fn finalize(mut self) -> anyhow::Result<Vec<Contract>> {
        for builder in self.builders {
            builder(&mut self.context)?;
        }

        let (generator, mut runtime_binder) = self.context.finalize();

        Ok(generator.finalize(&mut runtime_binder)?)
    }
}

impl Generatable for HardhatArtifact {
    fn generate<C: Context>(&self, context: &mut C) -> anyhow::Result<()> {
        self.abi.generate(context)?;

        for abi in &self.abi {
            match abi {
                AbiField::Constructor(contructor) => {
                    // Generate deploy fn
                    let (generator, runtime_binder) = context.get_mut();

                    generator.generate_deploy(runtime_binder, contructor, &self.bytecode)?;
                }
                _ => {}
            }
        }

        Ok(())
    }
}

impl Generatable for Vec<AbiField> {
    fn generate<C: Context>(&self, context: &mut C) -> anyhow::Result<()> {
        let (generator, runtime_binder) = context.get_mut();

        for abi in self {
            match abi {
                AbiField::Function(function) => generator.generate_fn(runtime_binder, function)?,
                AbiField::Event(event) => generator.generate_event(runtime_binder, event)?,
                AbiField::Error(error) => generator.generate_error(runtime_binder, error)?,
                _ => {
                    // Skip generate codes for constructor/receive/fallback.
                    // - Call `Generator::generate_deploy` for [`HardhatArtifact`]'s trait `Generate` to generate the constructor's binding code.
                    // - It seems that end users do not directly call receive/fallback api, so we skip their generation step.
                }
            }
        }

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use ethbind_json::Type;

    use crate::{JsonRuntimeBinder, RuntimeBinder};

    #[test]
    fn test_json_runtime_binder() {
        let mut runtime_binder: JsonRuntimeBinder =
            include_str!("../../rust/macros/tests/mapping.json")
                .parse()
                .expect("Load abi");

        let t: Type = "uint256".parse().expect("Parse type string");

        let runtime_type = runtime_binder
            .to_runtime_type(&t)
            .expect("Get runtime type")
            .expect("Is not tuple type");

        assert_eq!(runtime_type, "mock::Int<false,256>");

        let t: Type = "fixed128x8".parse().expect("Parse type string");

        let runtime_type = runtime_binder
            .to_runtime_type(&t)
            .expect("Get runtime type")
            .expect("Is not tuple type");

        assert_eq!(runtime_type, "mock::Fixed<true,128,8>");

        let t: Type = "uint256[20]".parse().expect("Parse type string");

        let runtime_type = runtime_binder
            .to_runtime_type(&t)
            .expect("Get runtime type")
            .expect("Is not tuple type");

        assert_eq!(runtime_type, "[mock::Int<false,256>;20]");

        let t: Type = "uint256[]".parse().expect("Parse type string");

        let runtime_type = runtime_binder
            .to_runtime_type(&t)
            .expect("Get runtime type")
            .expect("Is not tuple type");

        assert_eq!(runtime_type, "Vec<mock::Int<false,256>>");

        let t: Type = "bytes24".parse().expect("Parse type string");

        let runtime_type = runtime_binder
            .to_runtime_type(&t)
            .expect("Get runtime type")
            .expect("Is not tuple type");

        assert_eq!(runtime_type, "[u8;24]");

        let t: Type = "address".parse().expect("Parse type string");

        let runtime_type = runtime_binder
            .to_runtime_type(&t)
            .expect("Get runtime type")
            .expect("Is not tuple type");

        assert_eq!(runtime_type, "mock::Address");
    }
}