@@ -4,12 +4,10 @@ use std::collections::HashSet;
4
4
use proc_macro2;
5
5
use syn:: { self , Token , parenthesized} ;
6
6
use syn:: parse:: { Parse , ParseStream } ;
7
- use syn:: punctuated:: Punctuated ;
8
7
9
8
use crate :: error:: { DiagnosticError , Result } ;
10
9
11
10
pub struct Config {
12
- pub cache_type : syn:: Path ,
13
11
pub ignore_args : HashSet < syn:: Ident > ,
14
12
pub use_tls : bool ,
15
13
}
@@ -18,16 +16,13 @@ struct IgnoreArgsAttrib {
18
16
ignore_args : HashSet < syn:: Ident > ,
19
17
}
20
18
21
- struct CacheTypeAttrib {
22
- type_path : syn:: Path ,
23
- }
24
-
25
19
enum ConfigAttrib {
26
- CacheType ( CacheTypeAttrib ) ,
27
20
IgnoreArgs ( IgnoreArgsAttrib ) ,
28
21
UseTls ,
29
22
}
30
23
24
+ const CONFIG_ATTRIBUTE_NAME : & ' static str = "cache_cfg" ;
25
+
31
26
impl Config {
32
27
// Parse any additional attributes present after `lru_cache` and return a configuration object
33
28
// created from their contents. Additionally, return any attributes that were not handled here.
@@ -38,7 +33,7 @@ impl Config {
38
33
for attrib in attribs {
39
34
let segs = & attrib. path . segments ;
40
35
if segs. len ( ) > 0 {
41
- if segs[ 0 ] . ident . to_string ( ) == "lru_config" {
36
+ if segs[ 0 ] . ident == CONFIG_ATTRIBUTE_NAME {
42
37
let tokens = attrib. tts . clone ( ) ;
43
38
let parsed = syn:: parse2 :: < ConfigAttrib > ( tokens) ;
44
39
match parsed {
@@ -60,7 +55,6 @@ impl Config {
60
55
61
56
for parsed_attrib in parsed_attributes {
62
57
match parsed_attrib {
63
- ConfigAttrib :: CacheType ( val) => config. cache_type = val. type_path ,
64
58
ConfigAttrib :: IgnoreArgs ( val) => config. ignore_args = val. ignore_args ,
65
59
ConfigAttrib :: UseTls => config. use_tls = true ,
66
60
}
@@ -73,43 +67,19 @@ impl Config {
73
67
impl Default for Config {
74
68
fn default ( ) -> Config {
75
69
Config {
76
- cache_type : make_path_from_segments ( & [ "lru_cache" , "LruCache" ] , true , proc_macro2:: Span :: call_site ( ) ) ,
77
70
ignore_args : HashSet :: new ( ) ,
78
71
use_tls : false ,
79
72
}
80
73
}
81
74
}
82
75
83
- fn make_path_from_segments ( segments : & [ & str ] , has_leading_colon : bool , span : proc_macro2:: Span ) -> syn:: Path {
84
- let leading_colon = if has_leading_colon {
85
- Some ( syn:: token:: Colon2 { spans : [ span; 2 ] } )
86
- } else {
87
- None
88
- } ;
89
- let mut segments_ast = Punctuated :: < syn:: PathSegment , Token ! [ :: ] > :: new ( ) ;
90
- for name in segments {
91
- let ident = syn:: Ident :: new ( name, span) ;
92
- let seg = syn:: PathSegment {
93
- ident,
94
- arguments : syn:: PathArguments :: None ,
95
- } ;
96
- segments_ast. push ( seg) ;
97
- }
98
-
99
- syn:: Path {
100
- leading_colon,
101
- segments : segments_ast,
102
- }
103
- }
104
-
105
76
impl Parse for ConfigAttrib {
106
77
fn parse ( input : ParseStream ) -> syn:: parse:: Result < Self > {
107
78
let content;
108
79
let _paren = parenthesized ! ( content in input) ;
109
80
let name = content. parse :: < syn:: Ident > ( ) ?;
110
81
111
82
match & name. to_string ( ) [ ..] {
112
- "cache_type" => Ok ( ConfigAttrib :: CacheType ( content. parse :: < CacheTypeAttrib > ( ) ?) ) ,
113
83
"ignore_args" => Ok ( ConfigAttrib :: IgnoreArgs ( content. parse :: < IgnoreArgsAttrib > ( ) ?) ) ,
114
84
"thread_local" => Ok ( ConfigAttrib :: UseTls ) ,
115
85
_ => Err ( syn:: parse:: Error :: new (
@@ -119,15 +89,6 @@ impl Parse for ConfigAttrib {
119
89
}
120
90
}
121
91
122
- impl Parse for CacheTypeAttrib {
123
- fn parse ( input : ParseStream ) -> syn:: parse:: Result < Self > {
124
- input. parse :: < Token ! [ =] > ( ) ?;
125
- Ok ( CacheTypeAttrib {
126
- type_path : input. parse :: < syn:: Path > ( ) ?
127
- } )
128
- }
129
- }
130
-
131
92
impl Parse for IgnoreArgsAttrib {
132
93
fn parse ( input : ParseStream ) -> syn:: parse:: Result < Self > {
133
94
input. parse :: < Token ! [ =] > ( ) ?;
0 commit comments