11//! java-spaghetti.toml configuration file structures and parsing APIs.
22
3- use std:: collections:: HashSet ;
43use std:: path:: { Path , PathBuf } ;
54use std:: { fs, io} ;
65
@@ -10,9 +9,6 @@ fn default_proxy_path_prefix() -> String {
109 "java_spaghetti/proxy" . to_string ( )
1110}
1211
13- fn default_empty ( ) -> String {
14- String :: new ( )
15- }
1612fn default_slash ( ) -> String {
1713 String :: from ( "/" )
1814}
@@ -73,16 +69,6 @@ pub struct DocPattern {
7369 /// | jni_prefix = "" | field_url_pattern = "https://developer.android.com/reference/{CLASS}.html#{FIELD}"
7470 pub field_url_pattern : Option < String > ,
7571
76- /// What java class(es) to match against. This takes the form of a simple prefix to a JNI path with no wildcards.
77- ///
78- /// | To Match: | Use a JNI Prefix: |
79- /// | ------------------------- | ------------------------------------- |
80- /// | * | jni_prefix = ""
81- /// | java.lang.* | jni_prefix = "java/lang/"
82- /// | name.spaces.OuterClass.* | jni_prefix = "name/spaces/OuterClass$"
83- #[ serde( default = "default_empty" ) ]
84- pub jni_prefix : String ,
85-
8672 /// What to use in the {CLASS} portion of URLs to separate namespaces. Defaults to "/".
8773 #[ serde( default = "default_slash" ) ]
8874 pub class_namespace_separator : String ,
@@ -104,6 +90,35 @@ pub struct DocPattern {
10490 pub argument_seperator : String ,
10591}
10692
93+ #[ derive( Debug , Clone , Deserialize ) ]
94+ pub struct Rule {
95+ /// What java class(es) to match against. This takes the form of a simple prefix to a JNI path with no wildcards.
96+ ///
97+ /// | To Match: | Use a JNI Prefix: |
98+ /// | ------------------------- | ------------------------------------- |
99+ /// | * | jni_prefix = ""
100+ /// | java.lang.* | jni_prefix = "java/lang/"
101+ /// | name.spaces.OuterClass.* | jni_prefix = "name/spaces/OuterClass$"
102+ #[ serde( default ) ]
103+ pub prefix : String ,
104+
105+ #[ serde( default ) ]
106+ pub include : Option < bool > ,
107+
108+ #[ serde( default ) ]
109+ pub proxy : Option < bool > ,
110+
111+ #[ serde( default ) ]
112+ pub doc_pattern : Option < DocPattern > ,
113+ }
114+
115+ #[ derive( Debug , Clone ) ]
116+ pub struct ClassConfig < ' a > {
117+ pub include : bool ,
118+ pub proxy : bool ,
119+ pub doc_pattern : Option < & ' a DocPattern > ,
120+ }
121+
107122#[ derive( Debug , Clone , Deserialize ) ]
108123pub struct Config {
109124 #[ serde( default = "default_proxy_path_prefix" ) ]
@@ -112,18 +127,11 @@ pub struct Config {
112127 pub ( crate ) input : Vec < PathBuf > ,
113128 pub ( crate ) output : PathBuf ,
114129
115- #[ serde( default ) ]
116- pub ( crate ) doc_patterns : Vec < DocPattern > ,
117130 #[ serde( default ) ]
118131 pub ( crate ) logging_verbose : bool ,
119132
120- #[ serde( rename = "include" ) ]
121133 #[ serde( default ) ]
122- pub ( crate ) include_classes : HashSet < String > ,
123-
124- #[ serde( rename = "include_proxy" ) ]
125- #[ serde( default = "HashSet::new" ) ]
126- pub ( crate ) include_proxies : HashSet < String > ,
134+ pub ( crate ) rules : Vec < Rule > ,
127135}
128136
129137impl Config {
@@ -140,8 +148,13 @@ impl Config {
140148 pub fn read_str ( buffer : & str , dir : & Path ) -> io:: Result < Self > {
141149 let mut config: Config = toml:: from_str ( buffer) . map_err ( |e| io:: Error :: new ( io:: ErrorKind :: InvalidData , e) ) ?;
142150
143- if config. include_classes . is_empty ( ) {
144- config. include_classes . insert ( "*" . to_string ( ) ) ;
151+ if config. rules . is_empty ( ) {
152+ config. rules . push ( Rule {
153+ prefix : "" . to_string ( ) ,
154+ include : Some ( true ) ,
155+ doc_pattern : None ,
156+ proxy : None ,
157+ } )
145158 }
146159
147160 config. output = resolve_file ( & config. output , dir) ;
@@ -181,6 +194,30 @@ impl Config {
181194 }
182195 }
183196 }
197+
198+ pub fn resolve_class ( & self , class : & str ) -> ClassConfig < ' _ > {
199+ let mut res = ClassConfig {
200+ include : false ,
201+ proxy : false ,
202+ doc_pattern : None ,
203+ } ;
204+
205+ for r in & self . rules {
206+ if class. starts_with ( & r. prefix ) {
207+ if let Some ( include) = r. include {
208+ res. include = include;
209+ }
210+ if let Some ( proxy) = r. proxy {
211+ res. proxy = proxy;
212+ }
213+ if let Some ( doc_pattern) = & r. doc_pattern {
214+ res. doc_pattern = Some ( doc_pattern) ;
215+ }
216+ }
217+ }
218+
219+ res
220+ }
184221}
185222
186223fn resolve_file ( path : & Path , dir : & Path ) -> PathBuf {
0 commit comments