Skip to content

Commit 2de5df6

Browse files
committed
Move all magic strings to constants
1 parent 4f4f43c commit 2de5df6

File tree

3 files changed

+37
-32
lines changed

3 files changed

+37
-32
lines changed

derive/src/rpc_attr.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,22 @@ struct RpcAttributeVisitor {
2323
aliases: Vec<String>,
2424
}
2525

26+
const RPC_ATTR_NAME: &'static str = "rpc";
27+
const RPC_NAME_KEY: &'static str = "name";
28+
const RPC_ALIASES_KEY: &'static str = "aliases";
29+
const PUB_SUB_META_WORD: &'static str = "pubsub";
30+
const METADATA_META_WORD: &'static str = "meta";
31+
const SUBSCRIBE_META_WORD: &'static str = "subscribe";
32+
const UNSUBSCRIBE_META_WORD: &'static str = "unsubscribe";
33+
2634
impl RpcTraitAttribute {
2735
pub fn try_from_trait_attribute(args: &syn::AttributeArgs) -> Result<RpcTraitAttribute, String> {
2836
let mut visitor = RpcAttributeVisitor::default();
2937
for nested_meta in args {
3038
visitor.visit_nested_meta(nested_meta);
3139
}
3240

33-
if visitor.meta_words.contains(&"pubsub".into()) {
41+
if visitor.meta_words.contains(&PUB_SUB_META_WORD.into()) {
3442
if let Some(name) = visitor.name {
3543
Ok(RpcTraitAttribute::PubSubTrait { name })
3644
} else {
@@ -52,9 +60,9 @@ impl RpcMethodAttribute {
5260
Ok(Some(RpcMethodAttribute {
5361
attr: attr.clone(),
5462
aliases: visitor.aliases,
55-
has_metadata: visitor.meta_words.contains(&"meta".into()),
56-
is_subscribe: visitor.meta_words.contains(&"subscribe".into()),
57-
is_unsubscribe: visitor.meta_words.contains(&"unsubscribe".into()),
63+
has_metadata: visitor.meta_words.contains(&METADATA_META_WORD.into()),
64+
is_subscribe: visitor.meta_words.contains(&SUBSCRIBE_META_WORD.into()),
65+
is_unsubscribe: visitor.meta_words.contains(&UNSUBSCRIBE_META_WORD.into()),
5866
name,
5967
}))
6068
},
@@ -68,7 +76,7 @@ impl<'a> Visit<'a> for RpcAttributeVisitor {
6876
fn visit_attribute(&mut self, attr: &syn::Attribute) {
6977
match attr.parse_meta() {
7078
Ok(ref meta) => {
71-
if meta.name() == "rpc" {
79+
if meta.name() == RPC_ATTR_NAME {
7280
self.attr = Some(attr.clone());
7381
visit::visit_meta(self, meta);
7482
}
@@ -84,15 +92,15 @@ impl<'a> Visit<'a> for RpcAttributeVisitor {
8492
visit::visit_meta(self, meta);
8593
}
8694
fn visit_meta_name_value(&mut self, name_value: &syn::MetaNameValue) {
87-
if name_value.ident == "name" {
95+
if name_value.ident == RPC_NAME_KEY {
8896
if let syn::Lit::Str(ref str) = name_value.lit {
8997
self.name = Some(str.value())
9098
}
9199
}
92100
visit::visit_meta_name_value(self, name_value);
93101
}
94102
fn visit_meta_list(&mut self, meta_list: &syn::MetaList) {
95-
if meta_list.ident == "alias" {
103+
if meta_list.ident == RPC_ALIASES_KEY {
96104
self.aliases = meta_list.nested
97105
.iter()
98106
.filter_map(|nm| {

derive/src/rpc_trait.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ use syn::{
66
use crate::rpc_attr::{RpcTraitAttribute, RpcMethodAttribute};
77
use crate::to_delegate::{RpcMethod, ToDelegateMethod};
88

9+
const METADATA_TYPE: &'static str = "Metadata";
10+
911
const MISSING_SUBSCRIBE_METHOD_ERR: &'static str =
1012
"Can't find subscribe method, expected a method annotated with `subscribe` \
1113
e.g. `#[rpc(subscribe, name = \"hello_subscribe\")]`";
@@ -14,6 +16,8 @@ const MISSING_UNSUBSCRIBE_METHOD_ERR: &'static str =
1416
"Can't find unsubscribe method, expected a method annotated with `unsubscribe` \
1517
e.g. `#[rpc(unsubscribe, name = \"hello_unsubscribe\")]`";
1618

19+
const RPC_MOD_NAME_PREFIX: &'static str = "rpc_impl_{}";
20+
1721
type Result<T> = std::result::Result<T, String>;
1822

1923
struct RpcTrait {
@@ -42,7 +46,7 @@ impl<'a> Fold for RpcTrait {
4246
}
4347

4448
fn fold_trait_item_type(&mut self, ty: syn::TraitItemType) -> syn::TraitItemType {
45-
if ty.ident.to_string() == "Metadata" {
49+
if ty.ident == METADATA_TYPE {
4650
self.has_metadata = true;
4751
let mut ty = ty.clone();
4852
match self.attr {
@@ -80,7 +84,7 @@ fn generate_rpc_item_trait(attr_args: &syn::AttributeArgs, item_trait: &syn::Ite
8084
if !visitor.methods.is_empty() {
8185
Ok(ToDelegateMethod::Standard(visitor.methods))
8286
} else {
83-
Err("No rpc annotated trait items found".to_string())
87+
Err("No rpc annotated trait items found".into())
8488
}
8589
},
8690
RpcTraitAttribute::PubSubTrait { name } => {
@@ -102,8 +106,8 @@ fn generate_rpc_item_trait(attr_args: &syn::AttributeArgs, item_trait: &syn::Ite
102106
unsubscribe: unsub.clone()
103107
})
104108
},
105-
(Some(_), None) => Err(MISSING_UNSUBSCRIBE_METHOD_ERR.to_string()),
106-
(None, Some(_)) => Err(MISSING_SUBSCRIBE_METHOD_ERR.to_string()),
109+
(Some(_), None) => Err(MISSING_UNSUBSCRIBE_METHOD_ERR.into()),
110+
(None, Some(_)) => Err(MISSING_SUBSCRIBE_METHOD_ERR.into()),
107111
(None, None) => Err(format!("\n{}\n{}", MISSING_SUBSCRIBE_METHOD_ERR, MISSING_UNSUBSCRIBE_METHOD_ERR)),
108112
}
109113
}
@@ -122,14 +126,14 @@ fn generate_rpc_item_trait(attr_args: &syn::AttributeArgs, item_trait: &syn::Ite
122126

123127
fn rpc_wrapper_mod_name(rpc_trait: &syn::ItemTrait) -> syn::Ident {
124128
let name = rpc_trait.ident.clone();
125-
let mod_name = format!("rpc_impl_{}", name.to_string());
129+
let mod_name = format!("{}_{}", RPC_MOD_NAME_PREFIX, name.to_string());
126130
syn::Ident::new(&mod_name, proc_macro2::Span::call_site())
127131
}
128132

129133
pub fn rpc_impl(args: syn::AttributeArgs, input: syn::Item) -> Result<proc_macro2::TokenStream> {
130134
let rpc_trait = match input {
131135
syn::Item::Trait(item_trait) => item_trait,
132-
_ => return Err("rpc_api trait only works with trait declarations".to_owned())
136+
_ => return Err("rpc_api trait only works with trait declarations".into())
133137
};
134138

135139
let rpc_trait = generate_rpc_item_trait(&args, &rpc_trait)?;

derive/src/to_delegate.rs

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,17 @@ pub enum ToDelegateMethod {
1616
}
1717
}
1818

19+
const SUBCRIBER_TYPE_IDENT: &'static str = "Subscriber";
20+
const METADATA_CLOSURE_ARG: &'static str = "meta";
21+
const SUBSCRIBER_CLOSURE_ARG: &'static str = "subscriber";
22+
1923
impl ToDelegateMethod {
2024
pub fn generate_trait_item_method(
2125
&self,
2226
trait_item: &syn::ItemTrait,
2327
has_metadata: bool,
2428
) -> syn::TraitItemMethod {
25-
let (io_delegate_type,to_delegate_body) =
29+
let (io_delegate_type, to_delegate_body) =
2630
match self {
2731
ToDelegateMethod::Standard(methods) => {
2832
let delegate_type = quote!(_jsonrpc_core::IoDelegate);
@@ -56,25 +60,19 @@ impl ToDelegateMethod {
5660
let method: syn::TraitItemMethod =
5761
if has_metadata {
5862
parse_quote! {
59-
fn to_delegate(self)
60-
-> #io_delegate_type<Self, Self::Metadata>
61-
{
63+
fn to_delegate(self) -> #io_delegate_type<Self, Self::Metadata> {
6264
#to_delegate_body
6365
}
6466
}
6567
} else {
6668
parse_quote! {
67-
fn to_delegate<M: _jsonrpc_core::Metadata>(self)
68-
-> #io_delegate_type<Self, M>
69-
{
69+
fn to_delegate<M: _jsonrpc_core::Metadata>(self) -> #io_delegate_type<Self, M> {
7070
#to_delegate_body
7171
}
7272
}
7373
};
7474

75-
let predicates =
76-
generate_where_clause_serialization_predicates(&trait_item);
77-
75+
let predicates = generate_where_clause_serialization_predicates(&trait_item);
7876
let mut method = method.clone();
7977
method.sig.decl.generics
8078
.make_where_clause()
@@ -85,14 +83,12 @@ impl ToDelegateMethod {
8583

8684
fn delegate_rpc_method(method: &RpcMethod) -> proc_macro2::TokenStream {
8785
let rpc_name = &method.name();
88-
8986
let add_method =
9087
if method.has_metadata() {
9188
ident("add_method_with_meta")
9289
} else {
9390
ident("add_method")
9491
};
95-
9692
let closure = method.generate_delegate_closure(false);
9793
let add_aliases = method.generate_add_aliases();
9894

@@ -144,10 +140,7 @@ pub struct RpcMethod {
144140
}
145141

146142
impl RpcMethod {
147-
pub fn new(
148-
attr: RpcMethodAttribute,
149-
trait_item: syn::TraitItemMethod
150-
) -> RpcMethod {
143+
pub fn new(attr: RpcMethodAttribute, trait_item: syn::TraitItemMethod) -> RpcMethod {
151144
RpcMethod { attr, trait_item }
152145
}
153146

@@ -251,7 +244,7 @@ impl RpcMethod {
251244
if *ty == parse_quote!(Self::Metadata) { Some(ty.clone()) } else { None });
252245
let subscriber_arg = param_types.iter().nth(1).and_then(|ty| {
253246
if let syn::Type::Path(path) = ty {
254-
if path.path.segments.iter().any(|s| s.ident == "Subscriber") {
247+
if path.path.segments.iter().any(|s| s.ident == SUBCRIBER_TYPE_IDENT) {
255248
Some(ty.clone())
256249
} else {
257250
None
@@ -263,10 +256,10 @@ impl RpcMethod {
263256

264257
let mut special_args = Vec::new();
265258
if let Some(ref meta) = meta_arg {
266-
special_args.push((ident("meta"), meta.clone()));
259+
special_args.push((ident(METADATA_CLOSURE_ARG), meta.clone()));
267260
}
268261
if let Some(ref subscriber) = subscriber_arg {
269-
special_args.push((ident("subscriber"), subscriber.clone()));
262+
special_args.push((ident(SUBSCRIBER_CLOSURE_ARG), subscriber.clone()));
270263
}
271264
special_args
272265
}

0 commit comments

Comments
 (0)