-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Autogenerate
__env
field in a module struct
* update macros * update examples * update templates
- Loading branch information
Showing
17 changed files
with
156 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
use crate::ir::StructIR; | ||
use crate::utils; | ||
|
||
#[derive(syn_derive::ToTokens)] | ||
pub struct ModuleDefItem { | ||
item_struct: syn::ItemStruct | ||
} | ||
|
||
impl TryFrom<&'_ StructIR> for ModuleDefItem { | ||
type Error = syn::Error; | ||
|
||
fn try_from(ir: &'_ StructIR) -> Result<Self, Self::Error> { | ||
let mut item_struct = ir.self_code().clone(); | ||
let env_field: syn::Field = utils::misc::field( | ||
&utils::ident::underscored_env(), | ||
&utils::ty::rc_contract_env() | ||
); | ||
|
||
let fields = item_struct | ||
.fields | ||
.into_iter() | ||
.chain(vec![env_field]) | ||
.collect::<syn::punctuated::Punctuated<_, _>>(); | ||
|
||
item_struct.fields = syn::Fields::Named(syn::FieldsNamed { | ||
brace_token: Default::default(), | ||
named: fields | ||
}); | ||
|
||
Ok(Self { item_struct }) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crate::test_utils::{assert_eq, mock_module_definition}; | ||
|
||
#[test] | ||
fn test_module_def_item() { | ||
let ir = mock_module_definition(); | ||
let def = ModuleDefItem::try_from(&ir).unwrap(); | ||
let expected = quote::quote! { | ||
pub struct CounterPack { | ||
counter0: ModuleWrapper<Counter>, | ||
counter1: ModuleWrapper<Counter>, | ||
counter2: ModuleWrapper<Counter>, | ||
counters: Variable<u32>, | ||
counters_map: Mapping<u8, Counter>, | ||
__env: Rc<odra::ContractEnv> | ||
} | ||
}; | ||
|
||
assert_eq(def, expected); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,19 @@ | ||
pub fn address() -> syn::ExprField { | ||
syn::parse_quote!(self.address) | ||
member(super::ident::address()) | ||
} | ||
|
||
pub fn attached_value() -> syn::ExprField { | ||
syn::parse_quote!(self.attached_value) | ||
member(super::ident::attached_value()) | ||
} | ||
|
||
pub fn env() -> syn::ExprField { | ||
syn::parse_quote!(self.env) | ||
member(super::ident::env()) | ||
} | ||
|
||
pub fn underscored_env() -> syn::ExprField { | ||
member(super::ident::underscored_env()) | ||
} | ||
|
||
fn member(ident: syn::Ident) -> syn::ExprField { | ||
syn::parse_quote!(self.#ident) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
pub fn field(ident: &syn::Ident, ty: &syn::Type) -> syn::Field { | ||
syn::Field { | ||
attrs: vec![], | ||
vis: super::syn::visibility_private(), | ||
mutability: syn::FieldMutability::None, | ||
ident: Some(ident.clone()), | ||
colon_token: Some(Default::default()), | ||
ty: ty.clone() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters