@@ -21,17 +21,152 @@ mod shared {
21
21
}
22
22
}
23
23
24
+ #[ cfg( any( feature = "walkdir" , feature = "fs-walkdir-parallel" ) ) ]
25
+ mod walkdir_precompose {
26
+ use std:: borrow:: Cow ;
27
+ use std:: ffi:: OsStr ;
28
+ use std:: path:: Path ;
29
+
30
+ pub struct DirEntry < T > {
31
+ inner : T ,
32
+ precompose_unicode : bool ,
33
+ }
34
+
35
+ pub trait DirEntryApi {
36
+ fn path ( & self ) -> Cow < ' _ , Path > ;
37
+ fn file_name ( & self ) -> Cow < ' _ , OsStr > ;
38
+ fn file_type ( & self ) -> std:: fs:: FileType ;
39
+ }
40
+
41
+ impl < T : DirEntryApi > DirEntry < T > {
42
+ /// Obtain the full path of this entry, possibly with precomposed unicode if enabled.
43
+ ///
44
+ /// Note that decomposing filesystem like those made by Apple accept both precomposed and
45
+ /// decomposed names, and consider them equal.
46
+ pub fn path ( & self ) -> Cow < ' _ , Path > {
47
+ let path = self . inner . path ( ) ;
48
+ if self . precompose_unicode {
49
+ gix_utils:: str:: precompose_path ( path)
50
+ } else {
51
+ path
52
+ }
53
+ }
54
+
55
+ /// Obtain filen name of this entry, possibly with precomposed unicode if enabled.
56
+ pub fn file_name ( & self ) -> Cow < ' _ , OsStr > {
57
+ let name = self . inner . file_name ( ) ;
58
+ if self . precompose_unicode {
59
+ gix_utils:: str:: precompose_os_string ( name)
60
+ } else {
61
+ name
62
+ }
63
+ }
64
+
65
+ /// Return the file type for the file that this entry points to.
66
+ ///
67
+ /// If `follow_links` was `true`, this is the file type of the item the link points to.
68
+ pub fn file_type ( & self ) -> std:: fs:: FileType {
69
+ self . inner . file_type ( )
70
+ }
71
+ }
72
+
73
+ /// A platform over entries in a directory, which may or may not precompose unicode after retrieving
74
+ /// paths from the file system.
75
+ pub struct WalkDir < T > {
76
+ pub ( crate ) inner : Option < T > ,
77
+ pub ( crate ) precompose_unicode : bool ,
78
+ }
79
+
80
+ pub struct WalkDirIter < T , I , E >
81
+ where
82
+ T : Iterator < Item = Result < I , E > > ,
83
+ I : DirEntryApi ,
84
+ {
85
+ pub ( crate ) inner : T ,
86
+ pub ( crate ) precompose_unicode : bool ,
87
+ }
88
+
89
+ impl < T , I , E > Iterator for WalkDirIter < T , I , E >
90
+ where
91
+ T : Iterator < Item = Result < I , E > > ,
92
+ I : DirEntryApi ,
93
+ {
94
+ type Item = Result < DirEntry < I > , E > ;
95
+
96
+ fn next ( & mut self ) -> Option < Self :: Item > {
97
+ self . inner . next ( ) . map ( |res| {
98
+ res. map ( |entry| DirEntry {
99
+ inner : entry,
100
+ precompose_unicode : self . precompose_unicode ,
101
+ } )
102
+ } )
103
+ }
104
+ }
105
+ }
106
+
24
107
///
25
108
#[ cfg( feature = "fs-walkdir-parallel" ) ]
26
109
pub mod walkdir {
110
+ use std:: borrow:: Cow ;
111
+ use std:: ffi:: OsStr ;
112
+ use std:: fs:: FileType ;
27
113
use std:: path:: Path ;
28
114
29
- pub use jwalk:: { DirEntry as DirEntryGeneric , DirEntryIter as DirEntryIterGeneric , Error , WalkDir } ;
115
+ use jwalk:: WalkDir as WalkDirImpl ;
116
+ pub use jwalk:: { DirEntry as DirEntryGeneric , DirEntryIter as DirEntryIterGeneric , Error } ;
30
117
31
118
pub use super :: shared:: Parallelism ;
32
119
33
- /// An alias for an uncustomized directory entry to match the one of the non-parallel version offered by `walkdir`.
34
- pub type DirEntry = DirEntryGeneric < ( ( ) , ( ) ) > ;
120
+ type DirEntryImpl = DirEntryGeneric < ( ( ) , ( ) ) > ;
121
+
122
+ /// A directory entry returned by [DirEntryIter].
123
+ pub type DirEntry = super :: walkdir_precompose:: DirEntry < DirEntryImpl > ;
124
+ /// A platform to create a [DirEntryIter] from.
125
+ pub type WalkDir = super :: walkdir_precompose:: WalkDir < WalkDirImpl > ;
126
+
127
+ impl super :: walkdir_precompose:: DirEntryApi for DirEntryImpl {
128
+ fn path ( & self ) -> Cow < ' _ , Path > {
129
+ self . path ( ) . into ( )
130
+ }
131
+
132
+ fn file_name ( & self ) -> Cow < ' _ , OsStr > {
133
+ self . file_name ( ) . into ( )
134
+ }
135
+
136
+ fn file_type ( & self ) -> FileType {
137
+ self . file_type ( )
138
+ }
139
+ }
140
+
141
+ impl IntoIterator for WalkDir {
142
+ type Item = Result < DirEntry , jwalk:: Error > ;
143
+ type IntoIter = DirEntryIter ;
144
+
145
+ fn into_iter ( self ) -> Self :: IntoIter {
146
+ DirEntryIter {
147
+ inner : self . inner . expect ( "always set (builder fix)" ) . into_iter ( ) ,
148
+ precompose_unicode : self . precompose_unicode ,
149
+ }
150
+ }
151
+ }
152
+
153
+ impl WalkDir {
154
+ /// Set the minimum component depth of paths of entries.
155
+ pub fn min_depth ( mut self , min : usize ) -> Self {
156
+ self . inner = Some ( self . inner . take ( ) . expect ( "always set" ) . min_depth ( min) ) ;
157
+ self
158
+ }
159
+ /// Set the maximum component depth of paths of entries.
160
+ pub fn max_depth ( mut self , max : usize ) -> Self {
161
+ self . inner = Some ( self . inner . take ( ) . expect ( "always set" ) . max_depth ( max) ) ;
162
+ self
163
+ }
164
+ /// Follow symbolic links.
165
+ pub fn follow_links ( mut self , toggle : bool ) -> Self {
166
+ self . inner = Some ( self . inner . take ( ) . expect ( "always set" ) . follow_links ( toggle) ) ;
167
+ self
168
+ }
169
+ }
35
170
36
171
impl From < Parallelism > for jwalk:: Parallelism {
37
172
fn from ( v : Parallelism ) -> Self {
@@ -58,43 +193,122 @@ pub mod walkdir {
58
193
}
59
194
60
195
/// Instantiate a new directory iterator which will not skip hidden files, with the given level of `parallelism`.
61
- pub fn walkdir_new ( root : & Path , parallelism : Parallelism ) -> WalkDir {
62
- WalkDir :: new ( root) . skip_hidden ( false ) . parallelism ( parallelism. into ( ) )
196
+ ///
197
+ /// Use `precompose_unicode` to represent the `core.precomposeUnicode` configuration option.
198
+ pub fn walkdir_new ( root : & Path , parallelism : Parallelism , precompose_unicode : bool ) -> WalkDir {
199
+ WalkDir {
200
+ inner : WalkDirImpl :: new ( root)
201
+ . skip_hidden ( false )
202
+ . parallelism ( parallelism. into ( ) )
203
+ . into ( ) ,
204
+ precompose_unicode,
205
+ }
63
206
}
64
207
65
208
/// Instantiate a new directory iterator which will not skip hidden files and is sorted
66
- pub fn walkdir_sorted_new ( root : & Path , parallelism : Parallelism ) -> WalkDir {
67
- WalkDir :: new ( root)
68
- . skip_hidden ( false )
69
- . sort ( true )
70
- . parallelism ( parallelism. into ( ) )
209
+ ///
210
+ /// Use `precompose_unicode` to represent the `core.precomposeUnicode` configuration option.
211
+ pub fn walkdir_sorted_new ( root : & Path , parallelism : Parallelism , precompose_unicode : bool ) -> WalkDir {
212
+ WalkDir {
213
+ inner : WalkDirImpl :: new ( root)
214
+ . skip_hidden ( false )
215
+ . sort ( true )
216
+ . parallelism ( parallelism. into ( ) )
217
+ . into ( ) ,
218
+ precompose_unicode,
219
+ }
71
220
}
72
221
222
+ type DirEntryIterImpl = DirEntryIterGeneric < ( ( ) , ( ) ) > ;
223
+
73
224
/// The Iterator yielding directory items
74
- pub type DirEntryIter = DirEntryIterGeneric < ( ( ) , ( ) ) > ;
225
+ pub type DirEntryIter = super :: walkdir_precompose :: WalkDirIter < DirEntryIterImpl , DirEntryImpl , jwalk :: Error > ;
75
226
}
76
227
77
- #[ cfg( all( feature = "walkdir" , not( feature = "fs-walkdir-parallel" ) ) ) ]
78
228
///
229
+ #[ cfg( all( feature = "walkdir" , not( feature = "fs-walkdir-parallel" ) ) ) ]
79
230
pub mod walkdir {
231
+ use std:: borrow:: Cow ;
232
+ use std:: ffi:: OsStr ;
233
+ use std:: fs:: FileType ;
80
234
use std:: path:: Path ;
81
235
82
- pub use walkdir:: { DirEntry , Error , WalkDir } ;
236
+ pub use walkdir:: Error ;
237
+ use walkdir:: { DirEntry as DirEntryImpl , WalkDir as WalkDirImpl } ;
238
+
239
+ /// A directory entry returned by [DirEntryIter].
240
+ pub type DirEntry = super :: walkdir_precompose:: DirEntry < DirEntryImpl > ;
241
+ /// A platform to create a [DirEntryIter] from.
242
+ pub type WalkDir = super :: walkdir_precompose:: WalkDir < WalkDirImpl > ;
83
243
84
244
pub use super :: shared:: Parallelism ;
85
245
246
+ impl super :: walkdir_precompose:: DirEntryApi for DirEntryImpl {
247
+ fn path ( & self ) -> Cow < ' _ , Path > {
248
+ self . path ( ) . into ( )
249
+ }
250
+
251
+ fn file_name ( & self ) -> Cow < ' _ , OsStr > {
252
+ self . file_name ( ) . into ( )
253
+ }
254
+
255
+ fn file_type ( & self ) -> FileType {
256
+ self . file_type ( )
257
+ }
258
+ }
259
+
260
+ impl IntoIterator for WalkDir {
261
+ type Item = Result < DirEntry , walkdir:: Error > ;
262
+ type IntoIter = DirEntryIter ;
263
+
264
+ fn into_iter ( self ) -> Self :: IntoIter {
265
+ DirEntryIter {
266
+ inner : self . inner . expect ( "always set (builder fix)" ) . into_iter ( ) ,
267
+ precompose_unicode : self . precompose_unicode ,
268
+ }
269
+ }
270
+ }
271
+
272
+ impl WalkDir {
273
+ /// Set the minimum component depth of paths of entries.
274
+ pub fn min_depth ( mut self , min : usize ) -> Self {
275
+ self . inner = Some ( self . inner . take ( ) . expect ( "always set" ) . min_depth ( min) ) ;
276
+ self
277
+ }
278
+ /// Set the maximum component depth of paths of entries.
279
+ pub fn max_depth ( mut self , max : usize ) -> Self {
280
+ self . inner = Some ( self . inner . take ( ) . expect ( "always set" ) . max_depth ( max) ) ;
281
+ self
282
+ }
283
+ /// Follow symbolic links.
284
+ pub fn follow_links ( mut self , toggle : bool ) -> Self {
285
+ self . inner = Some ( self . inner . take ( ) . expect ( "always set" ) . follow_links ( toggle) ) ;
286
+ self
287
+ }
288
+ }
289
+
86
290
/// Instantiate a new directory iterator which will not skip hidden files, with the given level of `parallelism`.
87
- pub fn walkdir_new ( root : & Path , _: Parallelism ) -> WalkDir {
88
- WalkDir :: new ( root)
291
+ ///
292
+ /// Use `precompose_unicode` to represent the `core.precomposeUnicode` configuration option.
293
+ pub fn walkdir_new ( root : & Path , _: Parallelism , precompose_unicode : bool ) -> WalkDir {
294
+ WalkDir {
295
+ inner : WalkDirImpl :: new ( root) . into ( ) ,
296
+ precompose_unicode,
297
+ }
89
298
}
90
299
91
300
/// Instantiate a new directory iterator which will not skip hidden files and is sorted, with the given level of `parallelism`.
92
- pub fn walkdir_sorted_new ( root : & Path , _: Parallelism ) -> WalkDir {
93
- WalkDir :: new ( root) . sort_by_file_name ( )
301
+ ///
302
+ /// Use `precompose_unicode` to represent the `core.precomposeUnicode` configuration option.
303
+ pub fn walkdir_sorted_new ( root : & Path , _: Parallelism , precompose_unicode : bool ) -> WalkDir {
304
+ WalkDir {
305
+ inner : WalkDirImpl :: new ( root) . sort_by_file_name ( ) . into ( ) ,
306
+ precompose_unicode,
307
+ }
94
308
}
95
309
96
310
/// The Iterator yielding directory items
97
- pub type DirEntryIter = walkdir:: IntoIter ;
311
+ pub type DirEntryIter = super :: walkdir_precompose :: WalkDirIter < walkdir:: IntoIter , DirEntryImpl , walkdir :: Error > ;
98
312
}
99
313
100
314
#[ cfg( any( feature = "walkdir" , feature = "fs-walkdir-parallel" ) ) ]
0 commit comments