-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#[drink::contract_bundle_provider] macro (#73)
- Loading branch information
1 parent
817f215
commit cb0beb1
Showing
14 changed files
with
267 additions
and
88 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,85 @@ | ||
use std::{collections::HashMap, path::PathBuf}; | ||
|
||
use convert_case::{Case, Casing}; | ||
use proc_macro2::{Ident, Span, TokenStream as TokenStream2}; | ||
use quote::quote; | ||
use syn::ItemEnum; | ||
|
||
pub struct BundleProviderGenerator { | ||
root_contract_name: Option<String>, | ||
bundles: HashMap<String, PathBuf>, | ||
} | ||
|
||
impl BundleProviderGenerator { | ||
pub fn new<I: Iterator<Item = (String, PathBuf)>>( | ||
bundles: I, | ||
root_contract_name: Option<String>, | ||
) -> Self { | ||
let root_contract_name = root_contract_name.map(|name| name.to_case(Case::Pascal)); | ||
let bundles = HashMap::from_iter(bundles.map(|(name, path)| { | ||
let name = name.to_case(Case::Pascal); | ||
(name, path) | ||
})); | ||
|
||
if let Some(root_contract_name) = &root_contract_name { | ||
assert!( | ||
bundles.contains_key(root_contract_name), | ||
"Root contract must be part of the bundles" | ||
); | ||
} | ||
|
||
Self { | ||
root_contract_name, | ||
bundles, | ||
} | ||
} | ||
|
||
pub fn generate_bundle_provision(&self, enum_item: ItemEnum) -> TokenStream2 { | ||
let enum_name = &enum_item.ident; | ||
let enum_vis = &enum_item.vis; | ||
let enum_attrs = &enum_item.attrs; | ||
|
||
let local = match &self.root_contract_name { | ||
None => quote! {}, | ||
Some(root_name) => { | ||
let local_bundle = self.bundles[root_name].to_str().expect("Invalid path"); | ||
quote! { | ||
pub fn local() -> ::drink::DrinkResult<::drink::ContractBundle> { | ||
::drink::ContractBundle::load(#local_bundle) | ||
} | ||
} | ||
} | ||
}; | ||
|
||
let (contract_names, matches): (Vec<_>, Vec<_>) = self | ||
.bundles | ||
.keys() | ||
.map(|name| { | ||
let name_ident = Ident::new(name, Span::call_site()); | ||
let path = self.bundles[name].to_str().expect("Invalid path"); | ||
let matcher = quote! { | ||
#enum_name::#name_ident => ::drink::ContractBundle::load(#path), | ||
}; | ||
(name_ident, matcher) | ||
}) | ||
.unzip(); | ||
|
||
quote! { | ||
#(#enum_attrs)* | ||
#[derive(Copy, Clone, PartialEq, Eq, Debug)] | ||
#enum_vis enum #enum_name { | ||
#(#contract_names,)* | ||
} | ||
|
||
impl #enum_name { | ||
#local | ||
|
||
pub fn bundle(self) -> ::drink::DrinkResult<::drink::ContractBundle> { | ||
match self { | ||
#(#matches)* | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
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
Oops, something went wrong.