@@ -58,7 +58,7 @@ pub struct SortOptions {
58
58
///
59
59
/// ```rust
60
60
/// use lstr::sort::{sort_entries, SortOptions, SortType};
61
- ///
61
+ ///
62
62
/// let mut entries = vec![/* ... */];
63
63
/// let options = SortOptions {
64
64
/// sort_type: SortType::Name,
@@ -84,7 +84,7 @@ fn compare_entries(a: &DirEntry, b: &DirEntry, options: &SortOptions) -> Orderin
84
84
let b_is_dir = b. file_type ( ) . map ( |ft| ft. is_dir ( ) ) . unwrap_or ( false ) ;
85
85
let a_is_dotfile = is_dotfile ( a) ;
86
86
let b_is_dotfile = is_dotfile ( b) ;
87
-
87
+
88
88
// Handle dotfiles-first and directories-first sorting
89
89
// Order: dotfolders → folders → dotfiles → files
90
90
if options. dotfiles_first {
@@ -94,7 +94,7 @@ fn compare_entries(a: &DirEntry, b: &DirEntry, options: &SortOptions) -> Orderin
94
94
( false , true , false , true ) | // Both regular folders
95
95
( true , false , true , false ) | // Both dotfiles
96
96
( false , false , false , false ) => { } , // Both regular files
97
-
97
+
98
98
// Different categories - apply priority order
99
99
( true , true , _, _) => return Ordering :: Less , // a is dotfolder (highest priority)
100
100
( _, _, true , true ) => return Ordering :: Greater , // b is dotfolder
@@ -111,7 +111,7 @@ fn compare_entries(a: &DirEntry, b: &DirEntry, options: &SortOptions) -> Orderin
111
111
_ => { } // Both are dirs or both are files, continue
112
112
}
113
113
}
114
-
114
+
115
115
// Apply the primary sorting strategy
116
116
match options. sort_type {
117
117
SortType :: Name => compare_by_name ( a, b, options) ,
@@ -125,7 +125,7 @@ fn compare_entries(a: &DirEntry, b: &DirEntry, options: &SortOptions) -> Orderin
125
125
fn compare_by_name ( a : & DirEntry , b : & DirEntry , options : & SortOptions ) -> Ordering {
126
126
let name_a = a. file_name ( ) ;
127
127
let name_b = b. file_name ( ) ;
128
-
128
+
129
129
if options. natural_sort {
130
130
compare_natural ( name_a, name_b)
131
131
} else if options. case_sensitive {
@@ -147,10 +147,10 @@ fn compare_by_size(a: &DirEntry, b: &DirEntry) -> Ordering {
147
147
fn compare_by_modified ( a : & DirEntry , b : & DirEntry ) -> Ordering {
148
148
let modified_a = a. metadata ( ) . ok ( ) . and_then ( |m| m. modified ( ) . ok ( ) ) ;
149
149
let modified_b = b. metadata ( ) . ok ( ) . and_then ( |m| m. modified ( ) . ok ( ) ) ;
150
-
150
+
151
151
match ( modified_a, modified_b) {
152
152
( Some ( a_time) , Some ( b_time) ) => a_time. cmp ( & b_time) ,
153
- ( Some ( _) , None ) => Ordering :: Less , // Files with known time sort first
153
+ ( Some ( _) , None ) => Ordering :: Less , // Files with known time sort first
154
154
( None , Some ( _) ) => Ordering :: Greater ,
155
155
( None , None ) => Ordering :: Equal ,
156
156
}
@@ -160,13 +160,13 @@ fn compare_by_modified(a: &DirEntry, b: &DirEntry) -> Ordering {
160
160
fn compare_by_extension ( a : & DirEntry , b : & DirEntry , options : & SortOptions ) -> Ordering {
161
161
let ext_a = get_extension ( a. file_name ( ) ) ;
162
162
let ext_b = get_extension ( b. file_name ( ) ) ;
163
-
163
+
164
164
let ext_cmp = if options. case_sensitive {
165
165
ext_a. cmp ( & ext_b)
166
166
} else {
167
167
compare_case_insensitive_str ( & ext_a, & ext_b)
168
168
} ;
169
-
169
+
170
170
// If extensions are equal, fall back to name comparison
171
171
if ext_cmp == Ordering :: Equal {
172
172
compare_by_name ( a, b, options)
@@ -180,7 +180,7 @@ fn compare_natural(a: &OsStr, b: &OsStr) -> Ordering {
180
180
// Convert to strings for natural comparison
181
181
let str_a = a. to_string_lossy ( ) ;
182
182
let str_b = b. to_string_lossy ( ) ;
183
-
183
+
184
184
// Use the natord crate for natural ordering
185
185
natord:: compare ( & str_a, & str_b)
186
186
}
@@ -196,12 +196,12 @@ fn compare_case_insensitive(a: &OsStr, b: &OsStr) -> Ordering {
196
196
fn compare_default_order ( a : & OsStr , b : & OsStr ) -> Ordering {
197
197
let str_a = a. to_string_lossy ( ) ;
198
198
let str_b = b. to_string_lossy ( ) ;
199
-
199
+
200
200
// Compare character by character using the specified priority
201
201
for ( char_a, char_b) in str_a. chars ( ) . zip ( str_b. chars ( ) ) {
202
202
let order_a = char_sort_priority ( char_a) ;
203
203
let order_b = char_sort_priority ( char_b) ;
204
-
204
+
205
205
match order_a. cmp ( & order_b) {
206
206
Ordering :: Equal => {
207
207
// Same priority category, compare within category
@@ -213,7 +213,7 @@ fn compare_default_order(a: &OsStr, b: &OsStr) -> Ordering {
213
213
other => return other,
214
214
}
215
215
}
216
-
216
+
217
217
// If all compared characters are equal, compare by length
218
218
str_a. len ( ) . cmp ( & str_b. len ( ) )
219
219
}
@@ -233,10 +233,7 @@ fn char_sort_priority(c: char) -> u8 {
233
233
234
234
/// Checks if a directory entry is a dotfile/dotfolder (starts with '.').
235
235
fn is_dotfile ( entry : & DirEntry ) -> bool {
236
- entry
237
- . file_name ( )
238
- . to_string_lossy ( )
239
- . starts_with ( '.' )
236
+ entry. file_name ( ) . to_string_lossy ( ) . starts_with ( '.' )
240
237
}
241
238
242
239
/// Performs case-insensitive comparison on regular strings.
@@ -271,7 +268,7 @@ mod tests {
271
268
// Test case-insensitive comparison
272
269
let name_a = OsStr :: new ( "Apple" ) ;
273
270
let name_b = OsStr :: new ( "banana" ) ;
274
-
271
+
275
272
let result = compare_case_insensitive ( name_a, name_b) ;
276
273
assert_eq ! ( result, Ordering :: Less ) ; // "apple" < "banana"
277
274
}
@@ -280,7 +277,7 @@ mod tests {
280
277
fn test_case_sensitive_name_sorting ( ) {
281
278
let name_a = OsStr :: new ( "Apple" ) ;
282
279
let name_b = OsStr :: new ( "banana" ) ;
283
-
280
+
284
281
let result = name_a. cmp ( name_b) ;
285
282
assert_eq ! ( result, Ordering :: Less ) ; // "Apple" < "banana" in ASCII
286
283
}
@@ -289,22 +286,22 @@ mod tests {
289
286
fn test_natural_sorting ( ) {
290
287
let name_a = OsStr :: new ( "file1.txt" ) ;
291
288
let name_b = OsStr :: new ( "file10.txt" ) ;
292
-
289
+
293
290
let result = compare_natural ( name_a, name_b) ;
294
291
assert_eq ! ( result, Ordering :: Less ) ; // file1 < file10 naturally
295
-
292
+
296
293
// Test that regular lexicographic would give opposite result
297
294
let lexicographic = name_a. cmp ( name_b) ;
298
295
assert_eq ! ( lexicographic, Ordering :: Less ) ; // Actually "file1.txt" < "file10.txt" lexicographically too
299
-
296
+
300
297
// Better test: "file2.txt" vs "file10.txt"
301
298
let name_c = OsStr :: new ( "file2.txt" ) ;
302
299
let name_d = OsStr :: new ( "file10.txt" ) ;
303
-
300
+
304
301
let natural_result = compare_natural ( name_c, name_d) ;
305
302
let lexicographic_result = name_c. cmp ( name_d) ;
306
-
307
- assert_eq ! ( natural_result, Ordering :: Less ) ; // file2 < file10 naturally
303
+
304
+ assert_eq ! ( natural_result, Ordering :: Less ) ; // file2 < file10 naturally
308
305
assert_eq ! ( lexicographic_result, Ordering :: Greater ) ; // "file2.txt" > "file10.txt" lexicographically
309
306
}
310
307
@@ -328,14 +325,14 @@ mod tests {
328
325
}
329
326
330
327
#[ test]
331
- fn test_reverse_sorting ( ) {
328
+ fn test_reverse_sorting ( ) {
332
329
let name_a = OsStr :: new ( "apple" ) ;
333
330
let name_b = OsStr :: new ( "banana" ) ;
334
-
331
+
335
332
// Normal comparison: apple < banana
336
333
let normal = compare_case_insensitive ( name_a, name_b) ;
337
334
assert_eq ! ( normal, Ordering :: Less ) ;
338
-
335
+
339
336
// With reverse option, the final result should be flipped
340
337
// (This would be handled by the sort_entries function)
341
338
}
@@ -346,7 +343,7 @@ mod tests {
346
343
assert_eq ! ( compare_default_order( OsStr :: new( "1file" ) , OsStr :: new( "Afile" ) ) , Ordering :: Less ) ;
347
344
assert_eq ! ( compare_default_order( OsStr :: new( "Afile" ) , OsStr :: new( "afile" ) ) , Ordering :: Less ) ;
348
345
assert_eq ! ( compare_default_order( OsStr :: new( "afile" ) , OsStr :: new( "zfile" ) ) , Ordering :: Less ) ;
349
-
346
+
350
347
// Test within same category
351
348
assert_eq ! ( compare_default_order( OsStr :: new( "1file" ) , OsStr :: new( "2file" ) ) , Ordering :: Less ) ;
352
349
assert_eq ! ( compare_default_order( OsStr :: new( "Afile" ) , OsStr :: new( "Bfile" ) ) , Ordering :: Less ) ;
@@ -374,4 +371,4 @@ mod tests {
374
371
assert ! ( !OsStr :: new( "visible.txt" ) . to_string_lossy( ) . starts_with( '.' ) ) ;
375
372
assert ! ( !OsStr :: new( "normal" ) . to_string_lossy( ) . starts_with( '.' ) ) ;
376
373
}
377
- }
374
+ }
0 commit comments