fuels_rs/code_gen/
bindings.rs1use crate::errors::Error;
2use crate::rustfmt;
3use proc_macro2::TokenStream;
4use std::{fs::File, io::Write, path::Path};
5
6pub struct ContractBindings {
9 pub tokens: TokenStream,
11 pub rustfmt: bool,
13}
14
15impl ContractBindings {
16 pub fn write<W>(&self, mut w: W) -> Result<(), Error>
18 where
19 W: Write,
20 {
21 let source = {
22 let raw = self.tokens.to_string();
23
24 if self.rustfmt {
25 rustfmt::format(&raw).unwrap_or(raw)
26 } else {
27 raw
28 }
29 };
30
31 w.write_all(source.as_bytes()).unwrap();
32 Ok(())
33 }
34
35 pub fn write_to_file<P>(&self, path: P) -> Result<(), Error>
37 where
38 P: AsRef<Path>,
39 {
40 let file = File::create(path).unwrap();
41 self.write(file)
42 }
43
44 pub fn into_tokens(self) -> TokenStream {
47 self.tokens
48 }
49}