@@ -2,21 +2,25 @@ use crates::Crate;
2
2
use prelude:: * ;
3
3
use std:: path:: Path ;
4
4
use toml:: value:: Table ;
5
- use toml:: { self , Value } ;
5
+ use toml:: { self , value :: Array , Value } ;
6
6
7
7
pub ( super ) struct TomlFrobber < ' a > {
8
8
krate : & ' a Crate ,
9
9
table : Table ,
10
+ dir : & ' a Path ,
10
11
}
11
12
12
13
impl < ' a > TomlFrobber < ' a > {
13
- pub ( super ) fn new ( krate : & ' a Crate , cargo_toml : & Path ) -> Fallible < Self > {
14
+ pub ( super ) fn new ( krate : & ' a Crate , cargo_toml : & ' a Path ) -> Fallible < Self > {
14
15
let toml_content = :: std:: fs:: read_to_string ( cargo_toml)
15
16
. with_context ( |_| format ! ( "missing Cargo.toml from {}" , krate) ) ?;
17
+
16
18
let table: Table = toml:: from_str ( & toml_content)
17
19
. with_context ( |_| format ! ( "unable to parse {}" , cargo_toml. display( ) , ) ) ?;
18
20
19
- Ok ( TomlFrobber { krate, table } )
21
+ let dir = cargo_toml. parent ( ) . unwrap ( ) ;
22
+
23
+ Ok ( TomlFrobber { krate, table, dir } )
20
24
}
21
25
22
26
#[ cfg( test) ]
@@ -27,13 +31,48 @@ impl<'a> TomlFrobber<'a> {
27
31
pub ( super ) fn frob ( & mut self ) {
28
32
info ! ( "started frobbing {}" , self . krate) ;
29
33
34
+ self . remove_missing_items ( "example" ) ;
35
+ self . remove_missing_items ( "test" ) ;
30
36
self . remove_workspaces ( ) ;
31
37
self . remove_unwanted_cargo_features ( ) ;
32
38
self . remove_dependencies ( ) ;
33
39
34
40
info ! ( "finished frobbing {}" , self . krate) ;
35
41
}
36
42
43
+ fn test_existance ( dir : & Path , value : & Array , folder : & str ) -> Array {
44
+ value
45
+ . iter ( )
46
+ . filter_map ( |t| t. as_table ( ) )
47
+ . map ( |table| {
48
+ let name = table. get ( "name" ) . unwrap ( ) . to_string ( ) ;
49
+ let path = table. get ( "path" ) . map_or_else (
50
+ || dir. join ( folder) . join ( name + ".rs" ) ,
51
+ |path| dir. join ( path. as_str ( ) . unwrap ( ) ) ,
52
+ ) ;
53
+ ( table, path)
54
+ } )
55
+ . filter ( |( _table, path) | path. exists ( ) )
56
+ . filter_map ( |( table, _path) | Value :: try_from ( table) . ok ( ) )
57
+ . collect ( )
58
+ }
59
+
60
+ fn remove_missing_items ( & mut self , category : & str ) {
61
+ let folder = & ( String :: from ( category) + "s" ) ;
62
+
63
+ println ! ( "tables {:?}: {:?}" , category, self . table. get( category) ) ;
64
+
65
+ let _krate = self . krate . to_string ( ) ;
66
+ let dir = self . dir ;
67
+
68
+ if let Some ( array) = self . table . get_mut ( category) {
69
+ * ( array) =
70
+ toml:: Value :: Array ( Self :: test_existance ( dir, array. as_array ( ) . unwrap ( ) , folder) ) ;
71
+ }
72
+
73
+ println ! ( "tables example: {:?}" , self . table. get( "example" ) ) ;
74
+ }
75
+
37
76
fn remove_workspaces ( & mut self ) {
38
77
let krate = self . krate . to_string ( ) ;
39
78
0 commit comments