odra_casper_codegen/
schema.rs

1use odra_types::{
2    contract_def::{Argument, Entrypoint, Event},
3    CLType
4};
5use serde::{Deserialize, Serialize};
6
7pub fn gen_schema(
8    contract_ident: &str,
9    contract_entrypoints: &[Entrypoint],
10    events: &[Event]
11) -> String {
12    let schema = Schema {
13        name: contract_ident.to_owned(),
14        entrypoints: contract_entrypoints.iter().map(Into::into).collect(),
15        events: events.iter().map(Into::into).collect()
16    };
17    serde_json::to_string_pretty(&schema).expect("Failed to serialize schema")
18}
19
20#[derive(Debug, Clone, Serialize, Deserialize)]
21struct Schema {
22    name: String,
23    entrypoints: Vec<EntrypointDef>,
24    events: Vec<EventDef>
25}
26
27#[derive(Debug, Clone, Serialize, Deserialize)]
28struct EntrypointDef {
29    name: String,
30    is_mutable: bool,
31    args: Vec<ElemDef>,
32    return_ty: CLType
33}
34
35#[derive(Debug, Clone, Serialize, Deserialize)]
36struct EventDef {
37    name: String,
38    fields: Vec<ElemDef>
39}
40
41#[derive(Debug, Clone, Serialize, Deserialize)]
42struct ElemDef {
43    name: String,
44    ty: CLType
45}
46
47impl From<&Entrypoint> for EntrypointDef {
48    fn from(ep: &Entrypoint) -> Self {
49        EntrypointDef {
50            name: ep.ident.clone(),
51            is_mutable: ep.is_mut,
52            args: ep.args.iter().map(Into::into).collect(),
53            return_ty: ep.ret.clone()
54        }
55    }
56}
57
58impl From<&Argument> for ElemDef {
59    fn from(arg: &Argument) -> Self {
60        ElemDef {
61            name: arg.ident.clone(),
62            ty: arg.ty.clone()
63        }
64    }
65}
66
67impl From<&Event> for EventDef {
68    fn from(event: &Event) -> Self {
69        EventDef {
70            name: event.ident.clone(),
71            fields: event.args.iter().map(Into::into).collect()
72        }
73    }
74}