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
#![deny(missing_docs, unsafe_code)]
mod contract;
mod source;
mod util;
pub use crate::source::Source;
pub use crate::util::parse_address;
use anyhow::Result;
pub use ethcontract_common::Address;
use proc_macro2::TokenStream;
use std::collections::HashMap;
use std::fs::File;
use std::io::Write;
use std::path::Path;
pub(crate) struct Args {
artifact_source: Source,
runtime_crate_name: String,
contract_name_override: Option<String>,
deployments: HashMap<u32, Address>,
}
impl Args {
pub fn new(source: Source) -> Self {
Args {
artifact_source: source,
runtime_crate_name: "ethcontract".to_owned(),
contract_name_override: None,
deployments: HashMap::new(),
}
}
}
pub struct Builder {
args: Args,
}
impl Builder {
pub fn new<P>(artifact_path: P) -> Self
where
P: AsRef<Path>,
{
Builder::with_source(Source::local(artifact_path))
}
pub fn from_source_url<S>(source_url: S) -> Result<Self>
where
S: AsRef<str>,
{
let source = Source::parse(source_url)?;
Ok(Builder::with_source(source))
}
pub fn with_source(source: Source) -> Self {
Builder {
args: Args::new(source),
}
}
pub fn with_runtime_crate_name<S>(mut self, name: S) -> Self
where
S: Into<String>,
{
self.args.runtime_crate_name = name.into();
self
}
pub fn with_contract_name_override<S>(mut self, name: Option<S>) -> Self
where
S: Into<String>,
{
self.args.contract_name_override = name.map(S::into);
self
}
pub fn add_deployment(mut self, network_id: u32, address: Address) -> Self {
self.args.deployments.insert(network_id, address);
self
}
pub fn add_deployment_str<S>(self, network_id: u32, address: S) -> Self
where
S: AsRef<str>,
{
self.add_deployment(
network_id,
parse_address(address).expect("failed to parse address"),
)
}
pub fn generate(self) -> Result<ContractBindings> {
let tokens = contract::expand(self.args)?;
Ok(ContractBindings { tokens })
}
}
pub struct ContractBindings {
tokens: TokenStream,
}
impl ContractBindings {
pub fn write<W>(&self, mut w: W) -> Result<()>
where
W: Write,
{
write!(w, "{}", self.tokens)?;
Ok(())
}
pub fn write_to_file<P>(&self, path: P) -> Result<()>
where
P: AsRef<Path>,
{
let file = File::create(path)?;
self.write(file)
}
pub fn into_tokens(self) -> TokenStream {
self.tokens
}
}