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
use crate::rustfmt;
use fuels_types::errors::Error;
use proc_macro2::TokenStream;
use std::{fs::File, io::Write, path::Path};
pub struct ContractBindings {
pub tokens: TokenStream,
pub rustfmt: bool,
}
impl ContractBindings {
pub fn write<W>(&self, mut w: W) -> Result<(), Error>
where
W: Write,
{
let source = {
let raw = self.tokens.to_string();
if self.rustfmt {
rustfmt::format(&raw).unwrap_or(raw)
} else {
raw
}
};
w.write_all(source.as_bytes()).unwrap();
Ok(())
}
pub fn write_to_file<P>(&self, path: P) -> Result<(), Error>
where
P: AsRef<Path>,
{
let file = File::create(path).unwrap();
self.write(file)
}
pub fn into_tokens(self) -> TokenStream {
self.tokens
}
}