dns/
dns.rs

1use casper_contract_schema::{
2    Access, Argument, ContractSchema, CustomType, Entrypoint, EnumVariant, Event, NamedCLType,
3    StructMember, TypeName,
4};
5
6type IPv4 = [u8; 4];
7type IPv6 = [u8; 16];
8
9#[allow(dead_code)]
10enum IP {
11    Unknown,                                               // no data,
12    IPv4(IPv4),                                            // single unnamed element,
13    IPv4WithDescription(IPv4, String),                     // multiple unnamed elements,
14    IPv6 { ip: IPv6 },                                     // single named element,
15    IPv6WithDescription { ip: IPv6, description: String }, // multiple named elements,
16}
17
18fn enum_example_schema() -> ContractSchema {
19    ContractSchema {
20        casper_contract_schema_version: 1,
21        toolchain: String::from("rustc 1.73.0 (cc66ad468 2023-10-03)"),
22        contract_name: String::from("DNS"),
23        contract_version: String::from("0.1.3"),
24        authors: vec![],
25        repository: None,
26        homepage: None,
27        errors: vec![],
28        types: vec![
29            CustomType::Struct {
30                name: TypeName::new("IP::IPv6"),
31                description: None,
32                members: vec![StructMember::new("ip", "", NamedCLType::ByteArray(16))],
33            },
34            CustomType::Struct {
35                name: TypeName::new("IP::IPv6WithDescription"),
36                description: None,
37                members: vec![
38                    StructMember::new("ip", "", NamedCLType::ByteArray(16)),
39                    StructMember::new("description", "", NamedCLType::String),
40                ],
41            },
42            CustomType::Enum {
43                name: TypeName::new("IP"),
44                description: Some(String::from("IP address")),
45                variants: vec![
46                    EnumVariant {
47                        name: String::from("Unknown"),
48                        description: None,
49                        discriminant: 0,
50                        ty: NamedCLType::Unit.into(),
51                    },
52                    EnumVariant {
53                        name: String::from("IPv4"),
54                        description: Some(String::from("IPv4 address")),
55                        discriminant: 1,
56                        ty: NamedCLType::ByteArray(4).into(),
57                    },
58                    EnumVariant {
59                        name: String::from("IPv4WithDescription"),
60                        description: Some(String::from("IPv4 address with description")),
61                        discriminant: 2,
62                        ty: NamedCLType::Tuple2([
63                            Box::new(NamedCLType::ByteArray(4)),
64                            Box::new(NamedCLType::String),
65                        ])
66                        .into(),
67                    },
68                    EnumVariant {
69                        name: String::from("IPv6"),
70                        description: Some(String::from("IPv6 address")),
71                        discriminant: 3,
72                        ty: NamedCLType::Custom("IP::IPv6".to_owned()).into(),
73                    },
74                    EnumVariant {
75                        name: String::from("IPv6WithDescription"),
76                        description: Some(String::from("IPv6 address with description")),
77                        discriminant: 4,
78                        ty: NamedCLType::Custom("IP::IPv6WithDescription".to_owned()).into(),
79                    },
80                ],
81            },
82            CustomType::Struct {
83                name: TypeName::new("DNSRecord"),
84                description: Some(String::from("DNS record")),
85                members: vec![
86                    StructMember::new("name", "Domain name", NamedCLType::String),
87                    StructMember::new("ip", "", NamedCLType::Custom("IP".to_owned())),
88                ],
89            },
90        ],
91        entry_points: vec![
92            Entrypoint {
93                name: String::from("add_record"),
94                description: None,
95                is_mutable: true,
96                arguments: vec![
97                    Argument::new("name", "", NamedCLType::String),
98                    Argument::new("ip", "", NamedCLType::Custom("IP".to_owned())),
99                ],
100                return_ty: NamedCLType::Unit.into(),
101                is_contract_context: true,
102                access: Access::Public,
103            },
104            Entrypoint {
105                name: String::from("remove_record"),
106                description: Some(String::from("Remove a DNS record")),
107                is_mutable: true,
108                arguments: vec![
109                    Argument::new("name", "", NamedCLType::String),
110                    Argument::new("ip", "", NamedCLType::Custom("IP".to_owned())),
111                ],
112                return_ty: NamedCLType::Unit.into(),
113                is_contract_context: true,
114                access: Access::Groups(vec![String::from("admin"), String::from("moderator")]),
115            },
116        ],
117        events: vec![
118            Event::new("event_RecordAdded", "DNSRecord"),
119            Event::new("event_RecordRemoved", "DNSRecord"),
120        ],
121        call: None,
122    }
123}
124
125pub fn main() {
126    let schema = enum_example_schema();
127    let pretty_json = serde_json::to_string_pretty(&schema).unwrap();
128    println!("{}", pretty_json);
129}