vyper_rs/
macros.rs

1//! The Macro module contains powerful macros used to enhance the developer experience.
2
3/// The `vyper!` macro is used to construct the Vyper type without the boilerplate. If there is
4/// more than one pair of literals passed into the macro, the macro will return a Vypers.
5///
6/// Input: any length sequence of expressions that evaluate to a Path.
7///
8/// ```rust
9///  use vyper_rs::vyper::*;
10///  use vyper_rs::*;
11///  use std::path::{PathBuf, Path};
12///  use vyper_rs::vyper_errors::VyperErrors;
13///  fn try_me() -> Result<(), VyperErrors> {
14///     let _: Vyper = vyper!("./multisig.vy");
15///     let _: Vypers = vyper!("./multisig.vy", "./multisig.vy");
16///     Ok(())
17///  }
18///  ```
19#[macro_export]
20macro_rules! vyper {
21
22    ($p1: expr) => {
23       Vyper::new(Path::new($p1))
24    };
25    ($($p1: expr),+) => {
26        {
27            let mut contracts: Vec<PathBuf> = vec![];
28            $(
29                let v = PathBuf::from($p1);
30                contracts.push(v);
31            )+
32            Vypers::new(contracts)
33        }
34    };
35}
36
37/// The `compile!` macro is used to compile one more more Vyper contracts.
38///
39/// Input: any length sequence of expressions that evaluate to a Path.
40///
41/// Keywords: venv.
42///
43/// venv - compile contract using an instance of the Vyper compiler inside a venv.
44///
45/// ```rust
46///  use vyper_rs::venv::*;
47///  use vyper_rs::vyper::*;
48///  use vyper_rs::*;
49///  use std::path::{Path, PathBuf};
50///  use vyper_rs::vyper_errors::VyperErrors;
51///  async fn try_me() -> Result<(), VyperErrors> {
52///     let _: Vyper = compile!(venv "./multisig.vy");
53///     let _: Vyper = compile!("./multisig.vy");
54///     let _: Vypers = compile!(venv "./multisig.vy", "./multisig.vy");
55///     let _: Vypers = compile!("./multisig.vy", "./multisig.vy");
56///     Ok(())
57///  }
58///  ```
59#[macro_export]
60macro_rules! compile {
61    // classic, simple
62    ($p1: expr) => {
63        {
64            let mut vy: Vyper = vyper!($p1);
65            vy.compile()?;
66            vy
67        }
68    };
69    // a twist on the classic: compile inside a venv with keyword venv
70    (venv $p1: expr) => {
71        {
72            let mut contract = Venv::default()
73                .init()?
74                .ivyper_venv(None)?
75                .vyper(Path::new("../../multisig.vy"));
76           contract.compile()?;
77           contract
78        }
79    };
80    // compile many
81    ($($p1: expr),+) => {
82        {
83            let mut contracts: Vec<Vyper> = vec![];
84            $(
85                let v = vyper!($p1);
86                contracts.push(v);
87            )+
88            let mut cs: Vypers = Vypers::from(contracts);
89            cs.compile_many().await?;
90            cs
91        }
92    };
93    // compile many in venv
94    (venv $($p1: expr),+) => {
95        {
96            let mut paths: Vec<PathBuf> = vec![];
97            $(
98                let v = PathBuf::from($p1);
99                paths.push(v);
100            )+
101            let mut contracts = Venv::default().init()?.ivyper_venv(None)?.vypers(paths);
102            contracts.compile_many().await?;
103            contracts
104        }
105    };
106}
107
108/// The `abi!` macro is used to compile one more more Vyper contracts and get or generate the ABI.
109///
110/// Input: any length sequence of expressions that evaluate to a Path.
111///
112/// Keywords: paris, venv, get.
113///
114/// venv - compile contract using an instance of the Vyper compiler inside a venv.
115///
116/// ```rust
117///  use vyper_rs::venv::*;
118///  use vyper_rs::vyper::*;
119///  use vyper_rs::*;
120///  use vyper_rs::vyper_errors::VyperErrors;
121///  use std::path::{PathBuf, Path};
122///  use serde_json::Value;
123/// async fn try_me() -> Result<(), VyperErrors> {
124///     let _: Value = abi!("./multisig.vy");
125///     let _: Value = abi!(venv "./multisig.vy");
126///     let _: Vec<Value> = abi!("./multisig.vy", "./multisig.vy");   
127///     let _: Vec<Value> = abi!(venv "./multisig.vy", "./multisig.vy");   
128///     Ok(())
129/// }
130/// ```
131#[macro_export]
132macro_rules! abi {
133    // OG matcher
134    // return the ABI as json instead of creating a file
135    ($p1: expr) => {
136        {
137            let c: Vyper = compile!($p1);
138            c.get_abi()?
139        }
140    };
141    // return the ABI as json instead of creating a file
142    (venv $p1: expr) => {
143        {
144            let mut c: Vyper = compile!(venv $p1);
145            c.get_abi()?
146        }
147    };
148    // return many ABIs as json
149    ($($p1: expr),+) => {
150        {
151            let mut paths: Vec<PathBuf> = vec![];
152            $(
153                let v = PathBuf::from($p1);
154                paths.push(v);
155            )+
156            let cs: Vypers = Vypers::new(paths);
157            cs.get_abi_many().await?
158        }
159    };
160    // venv version of many
161    (venv $($p1: expr),+) => {
162        {
163            let mut p: Vec<PathBuf> = vec![];
164            $(
165                let v = PathBuf::from($p1);
166                p.push(v);
167            )+
168            let mut contracts = Venv::default().init()?.ivyper_venv(None)?.vypers(p);
169            contracts.get_abi_many().await?
170        }
171    };
172}
173/// The `venv!` macro creates a virtual environment with the latest version of the vyper compiler installed.
174/// Optionally, you can pass the desired version of the Vyper compiler you want to install, i.e
175/// "0.3.10", as a &str.
176///```rust
177///
178/// use vyper_rs::venv::*;
179/// use vyper_rs::*;
180/// use vyper_rs::vyper_errors::VyperErrors;
181///
182/// fn try_me() -> Result<(), VyperErrors> {
183///     let _:  Venv<Ready> = venv!();
184///     let _: Venv<Ready> = venv!("0.3.10");
185///     Ok(())
186/// }
187///
188///```
189#[macro_export]
190macro_rules! venv {
191    () => {{
192        Venv::default().init()?.ivyper_venv(None)?
193    }};
194    ($ver: literal) => {{
195        let version: &str = $ver;
196        Venv::default().init()?.ivyper_venv(Some(version))?
197    }};
198}