@@ -4,9 +4,10 @@ use assert2::assert;
44use colored:: Colorize ;
55use config_file2:: StoreConfigFile ;
66use log:: { debug, info, warn} ;
7- use path_absolutize:: Absolutize ;
7+ use path_absolutize:: Absolutize as _ ;
88use pathdiff:: diff_paths;
99use serde:: { Deserialize , Serialize } ;
10+ use walkdir:: WalkDir ;
1011
1112use crate :: {
1213 crypt:: { COMPRESSED_EXTENSION , ENCRYPTED_EXTENSION } ,
@@ -41,12 +42,15 @@ impl Default for Config {
4142}
4243
4344impl Config {
44- pub fn new ( path : impl Into < PathBuf > ) -> Self {
45- let path = path. into ( ) ;
45+ pub fn new ( path : impl AsRef < Path > ) -> Self {
4646 Self :: default ( ) . with_repo_path ( path)
4747 }
48- pub fn with_repo_path ( mut self , path : impl Into < PathBuf > ) -> Self {
49- self . repo_path = path. into ( ) ;
48+ pub fn with_repo_path ( mut self , path : impl AsRef < Path > ) -> Self {
49+ self . repo_path = path
50+ . as_ref ( )
51+ . absolutize ( )
52+ . expect ( "path absolutize failed" )
53+ . to_path_buf ( ) ;
5054 self
5155 }
5256 /// The absolute path of the config file.
@@ -56,11 +60,9 @@ impl Config {
5660 /// Add one path to crypt list
5761 ///
5862 /// path: relative path to a file or dir.
59- pub fn add_one_file_to_crypt_list ( & mut self , path : & str ) {
63+ pub fn add_one_file_to_crypt_list ( & mut self , path : impl AsRef < Path > ) {
6064 // path is the relative path to the current dir (or absolute path)
61- let path = Path :: new ( path)
62- . absolutize ( )
63- . expect ( "path absolutize failed" ) ;
65+ let path = path. as_ref ( ) . absolutize ( ) . expect ( "path absolutize failed" ) ;
6466
6567 debug ! ( "add_one_file_to_crypt_list: {}" , path. display( ) ) ;
6668 assert ! (
@@ -96,37 +98,35 @@ impl Config {
9698 "Add to crypt list: {}" ,
9799 format!( "{}" , path_relative_to_repo. display( ) ) . green( )
98100 ) ;
99- self . crypt_list . push (
100- path_relative_to_repo. to_string_lossy ( ) . replace ( '\\' , "/" )
101- + if path. is_dir ( ) { "/**" } else { "" } ,
102- ) ;
101+ self . crypt_list
102+ . push ( path_relative_to_repo. to_string_lossy ( ) . replace ( '\\' , "/" ) ) ;
103103
104104 // check extension
105105 if path. is_dir ( ) {
106- if let Ok ( glob_result) = glob:: glob ( path. join ( "**" ) . to_string_lossy ( ) . as_ref ( ) ) {
107- glob_result
108- . filter_map ( std:: result:: Result :: ok)
109- . for_each ( |p| {
110- if let Some ( ext) = p. extension ( ) . and_then ( |ext| ext. to_str ( ) )
111- && [ COMPRESSED_EXTENSION , ENCRYPTED_EXTENSION ] . contains ( & ext)
112- {
113- warn ! (
114- "{}" ,
115- format!(
116- "adding dir that contains file with no-good extension: {}" ,
117- p. display( )
118- )
119- . yellow( )
120- ) ;
121- }
122- } ) ;
106+ for entry in WalkDir :: new ( path)
107+ . into_iter ( )
108+ . filter_map ( std:: result:: Result :: ok)
109+ {
110+ if let Some ( ext) = entry. path ( ) . extension ( ) . and_then ( |ext| ext. to_str ( ) )
111+ && [ COMPRESSED_EXTENSION , ENCRYPTED_EXTENSION ] . contains ( & ext)
112+ {
113+ warn ! (
114+ "{}" ,
115+ format!(
116+ "adding dir that contains file with no-good extension: {}" ,
117+ entry. path( ) . display( )
118+ )
119+ . yellow( )
120+ ) ;
121+ }
123122 }
124123 }
125124 }
126- pub fn add_to_crypt_list ( & mut self , paths : & [ & str ] ) -> anyhow:: Result < ( ) > {
125+
126+ pub fn add_to_crypt_list ( & mut self , paths : & [ impl AsRef < Path > ] ) -> anyhow:: Result < ( ) > {
127127 paths
128128 . iter ( )
129- . for_each ( |x| self . add_one_file_to_crypt_list ( x) ) ;
129+ . for_each ( |x| self . add_one_file_to_crypt_list ( x. as_ref ( ) ) ) ;
130130 self . store ( CONFIG_FILE_NAME ) . map_err ( |e| anyhow:: anyhow!( e) )
131131 }
132132}
@@ -170,11 +170,14 @@ mod tests {
170170 let path_to_add = temp_dir. join ( "testdir" ) ;
171171 fs:: create_dir ( & path_to_add) ?;
172172 config. add_one_file_to_crypt_list ( path_to_add. as_os_str ( ) . to_string_lossy ( ) . as_ref ( ) ) ;
173- println ! ( "{:?}" , config. crypt_list. first( ) ) ;
173+ println ! ( "{:?}" , config. crypt_list. first( ) . unwrap ( ) ) ;
174174 assert ! (
175- config. crypt_list[ 0 ] . ends_with( "/**" ) ,
176- "needs to be dir pattern: `{}`" ,
177- config. crypt_list[ 0 ]
175+ config
176+ . repo_path
177+ . join( config. crypt_list. first( ) . unwrap( ) )
178+ . is_dir( ) ,
179+ "needs to be dir: {}" ,
180+ config. crypt_list. first( ) . unwrap( )
178181 ) ;
179182 Ok ( ( ) )
180183 }
0 commit comments