-
Notifications
You must be signed in to change notification settings - Fork 265
Client call with named params #541
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
tomusdrw
merged 26 commits into
paritytech:master
from
willemolding:client-call-with-named-params
Mar 23, 2020
Merged
Changes from 24 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
4bf380d
add support for json objects in TypedClient.call_method
willemolding c65efae
adds named params switch to #[rpc] method annotation
willemolding 187078a
add generating of map style params in macro
willemolding 167612f
prevent creating a server when using the named_params switch
willemolding 4cfa00b
add test for attempt to derive with named_params on server. refactors…
willemolding f158a7f
adds test that correct params object is generated
willemolding b0fefbc
apply cargo fmt
willemolding 2e266c7
adds ParamStyle enum and conversions. Parses this from meta
willemolding 7baf3be
updates existing tests
willemolding 097276c
adds client side support for raw params
willemolding 66488b2
adds compiler error on invalid params value
willemolding 95a945c
add client tests
willemolding 2d20978
adds global switch to trait attribute
willemolding 510048f
add logic to override code generation with default from top level
willemolding a6850ae
adds error on changing trait default when generating server with inco…
willemolding d750f40
adds tests for preventing server generation with named params
willemolding 36bab4e
Update derive/src/rpc_attr.rs
willemolding 4721ce2
Update derive/src/rpc_trait.rs
willemolding e7abc09
remove unwrap and throw useful compile time error
willemolding 38bb7f0
fixes error, notifies that server also supports raw params
willemolding 0d4bb3c
add placeholder for future support for named params server side
willemolding 82257ea
remove raw_params from examples and replace with params = raw
willemolding 1933993
run cargo fmt
willemolding e7e0cd5
add deprecation message placeholder
willemolding 2c69f44
bump and lock quote version
willemolding 15a5b8b
rollback quote to 1.0.1
willemolding File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,37 +1,71 @@ | ||
| use proc_macro::TokenStream; | ||
| use std::str::FromStr; | ||
|
|
||
| use crate::params_style::ParamStyle; | ||
| use crate::rpc_attr::path_eq_str; | ||
|
|
||
| const CLIENT_META_WORD: &str = "client"; | ||
| const SERVER_META_WORD: &str = "server"; | ||
| const PARAMS_META_KEY: &str = "params"; | ||
|
|
||
| #[derive(Debug)] | ||
| pub struct DeriveOptions { | ||
| pub enable_client: bool, | ||
| pub enable_server: bool, | ||
| pub params_style: ParamStyle, | ||
| } | ||
|
|
||
| impl DeriveOptions { | ||
| pub fn new(enable_client: bool, enable_server: bool) -> Self { | ||
| pub fn new(enable_client: bool, enable_server: bool, params_style: ParamStyle) -> Self { | ||
| DeriveOptions { | ||
| enable_client, | ||
| enable_server, | ||
| params_style, | ||
| } | ||
| } | ||
|
|
||
| pub fn try_from(tokens: TokenStream) -> Result<Self, syn::Error> { | ||
| if tokens.is_empty() { | ||
| return Ok(Self::new(true, true)); | ||
| } | ||
| let ident: syn::Ident = syn::parse::<syn::Ident>(tokens)?; | ||
| let options = { | ||
| let ident = ident.to_string(); | ||
| if ident == "client" { | ||
| Some(Self::new(true, false)) | ||
| } else if ident == "server" { | ||
| Some(Self::new(false, true)) | ||
| } else { | ||
| None | ||
| pub fn try_from(args: syn::AttributeArgs) -> Result<Self, syn::Error> { | ||
| let mut options = DeriveOptions::new(false, false, ParamStyle::default()); | ||
| for arg in args { | ||
| if let syn::NestedMeta::Meta(meta) = arg { | ||
| match meta { | ||
| syn::Meta::Path(ref p) => { | ||
| match p | ||
| .get_ident() | ||
| .ok_or(syn::Error::new_spanned( | ||
| p, | ||
| format!("Expecting identifier `{}` or `{}`", CLIENT_META_WORD, SERVER_META_WORD), | ||
| ))? | ||
| .to_string() | ||
| .as_ref() | ||
| { | ||
| CLIENT_META_WORD => options.enable_client = true, | ||
| SERVER_META_WORD => options.enable_server = true, | ||
| _ => {} | ||
| }; | ||
| } | ||
| syn::Meta::NameValue(nv) => { | ||
| if path_eq_str(&nv.path, PARAMS_META_KEY) { | ||
| if let syn::Lit::Str(ref lit) = nv.lit { | ||
| options.params_style = ParamStyle::from_str(&lit.value()) | ||
| .map_err(|e| syn::Error::new_spanned(nv.clone(), e))?; | ||
| } | ||
| } else { | ||
| return Err(syn::Error::new_spanned(nv, "Unexpected RPC attribute key")); | ||
| } | ||
| } | ||
| _ => return Err(syn::Error::new_spanned(meta, "Unexpected use of RPC attribute macro")), | ||
| } | ||
| } | ||
| }; | ||
| match options { | ||
| Some(options) => Ok(options), | ||
| None => Err(syn::Error::new(ident.span(), "Unknown attribute.")), | ||
| } | ||
| if !options.enable_client && !options.enable_server { | ||
| // if nothing provided default to both | ||
| options.enable_client = true; | ||
| options.enable_server = true; | ||
| } | ||
| if options.enable_server && options.params_style == ParamStyle::Named { | ||
| // This is not allowed at this time | ||
| panic!("Server code generation only supports `params = \"positional\"` (default) or `params = \"raw\" at this time.") | ||
| } | ||
| Ok(options) | ||
| } | ||
| } |
This file contains hidden or 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,34 @@ | ||
| use std::str::FromStr; | ||
|
|
||
| const POSITIONAL: &str = "positional"; | ||
| const NAMED: &str = "named"; | ||
| const RAW: &str = "raw"; | ||
|
|
||
| #[derive(Clone, Debug, PartialEq)] | ||
| pub enum ParamStyle { | ||
| Positional, | ||
| Named, | ||
| Raw, | ||
| } | ||
|
|
||
| impl Default for ParamStyle { | ||
| fn default() -> Self { | ||
| Self::Positional | ||
| } | ||
| } | ||
|
|
||
| impl FromStr for ParamStyle { | ||
| type Err = String; | ||
|
|
||
| fn from_str(s: &str) -> Result<Self, String> { | ||
| match s { | ||
| POSITIONAL => Ok(Self::Positional), | ||
| NAMED => Ok(Self::Named), | ||
| RAW => Ok(Self::Raw), | ||
| _ => Err(format!( | ||
| "Invalid value for params key. Must be one of [{}, {}, {}]", | ||
| POSITIONAL, NAMED, RAW | ||
| )), | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.