1+ use config;
2+
13use infer_schema_internals:: * ;
24use std:: error:: Error ;
35use std:: fmt:: { self , Display , Formatter , Write } ;
46use std:: io:: { self , stdout} ;
7+ use serde:: de:: { self , MapAccess , Visitor } ;
8+ use serde:: { Deserialize , Deserializer } ;
59
610pub enum Filtering {
711 Whitelist ( Vec < TableName > ) ,
812 Blacklist ( Vec < TableName > ) ,
913 None ,
1014}
1115
16+ impl Default for Filtering {
17+ fn default ( ) -> Self {
18+ Filtering :: None
19+ }
20+ }
21+
1222impl Filtering {
1323 pub fn should_ignore_table ( & self , name : & TableName ) -> bool {
1424 use self :: Filtering :: * ;
@@ -23,31 +33,21 @@ impl Filtering {
2333
2434pub fn run_print_schema (
2535 database_url : & str ,
26- schema_name : Option < & str > ,
27- filtering : & Filtering ,
28- include_docs : bool ,
36+ config : & config:: PrintSchema ,
2937) -> Result < ( ) , Box < Error > > {
30- output_schema (
31- database_url,
32- schema_name,
33- filtering,
34- include_docs,
35- & mut stdout ( ) ,
36- )
38+ output_schema ( database_url, config, & mut stdout ( ) )
3739}
3840
3941pub fn output_schema < W : io:: Write > (
4042 database_url : & str ,
41- schema_name : Option < & str > ,
42- filtering : & Filtering ,
43- include_docs : bool ,
43+ config : & config:: PrintSchema ,
4444 out : & mut W ,
4545) -> Result < ( ) , Box < Error > > {
46- let table_names = load_table_names ( database_url, schema_name) ?
46+ let table_names = load_table_names ( database_url, config . schema_name ( ) ) ?
4747 . into_iter ( )
48- . filter ( |t| !filtering . should_ignore_table ( t) )
48+ . filter ( |t| !config . filter . should_ignore_table ( t) )
4949 . collect :: < Vec < _ > > ( ) ;
50- let foreign_keys = load_foreign_key_constraints ( database_url, schema_name) ?;
50+ let foreign_keys = load_foreign_key_constraints ( database_url, config . schema_name ( ) ) ?;
5151 let foreign_keys =
5252 remove_unsafe_foreign_keys_for_codegen ( database_url, & foreign_keys, & table_names) ;
5353 let table_data = table_names
@@ -57,10 +57,10 @@ pub fn output_schema<W: io::Write>(
5757 let definitions = TableDefinitions {
5858 tables : table_data,
5959 fk_constraints : foreign_keys,
60- include_docs,
60+ include_docs : config . with_docs ,
6161 } ;
6262
63- if let Some ( schema_name) = schema_name {
63+ if let Some ( schema_name) = config . schema_name ( ) {
6464 write ! ( out, "{}" , ModuleDefinition ( schema_name, definitions) ) ?;
6565 } else {
6666 write ! ( out, "{}" , definitions) ?;
@@ -255,3 +255,53 @@ impl<'a, 'b: 'a> Write for PadAdapter<'a, 'b> {
255255 Ok ( ( ) )
256256 }
257257}
258+
259+ impl < ' de > Deserialize < ' de > for Filtering {
260+ fn deserialize < D > ( deserializer : D ) -> Result < Self , D :: Error >
261+ where
262+ D : Deserializer < ' de > ,
263+ {
264+ struct FilteringVisitor ;
265+
266+ impl < ' de > Visitor < ' de > for FilteringVisitor {
267+ type Value = Filtering ;
268+
269+ fn expecting ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
270+ f. write_str ( "either a whitelist or a blacklist" )
271+ }
272+
273+ fn visit_map < V > ( self , mut map : V ) -> Result < Self :: Value , V :: Error >
274+ where
275+ V : MapAccess < ' de > ,
276+ {
277+ let mut whitelist = None ;
278+ let mut blacklist = None ;
279+ while let Some ( ( key, value) ) = map. next_entry ( ) ? {
280+ match key {
281+ "whitelist" => {
282+ if whitelist. is_some ( ) {
283+ return Err ( de:: Error :: duplicate_field ( "whitelist" ) ) ;
284+ }
285+ whitelist = Some ( value) ;
286+ }
287+ "blacklist" => {
288+ if blacklist. is_some ( ) {
289+ return Err ( de:: Error :: duplicate_field ( "blacklist" ) ) ;
290+ }
291+ blacklist = Some ( value) ;
292+ }
293+ _ => return Err ( de:: Error :: unknown_field ( key, & [ "whitelist" , "blacklist" ] ) ) ,
294+ }
295+ }
296+ match ( whitelist, blacklist) {
297+ ( Some ( _) , Some ( _) ) => Err ( de:: Error :: duplicate_field ( "blacklist" ) ) ,
298+ ( Some ( w) , None ) => Ok ( Filtering :: Whitelist ( w) ) ,
299+ ( None , Some ( b) ) => Ok ( Filtering :: Blacklist ( b) ) ,
300+ ( None , None ) => Ok ( Filtering :: None ) ,
301+ }
302+ }
303+ }
304+
305+ deserializer. deserialize_map ( FilteringVisitor )
306+ }
307+ }
0 commit comments