Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
use anyhow::Result;
use std::{
    collections::HashMap,
    fs::{self, read_dir, File, OpenOptions},
    hash::Hash,
    io::{BufRead, BufReader, Seek, SeekFrom, Write},
    path::Path,
    str::FromStr,
};

use crate::{strs, times};

/// 创建文件,文件存在会打开,往文件追加内容
pub fn open_file(file_path: &str) -> File {
    create_pdir(file_path);
    OpenOptions::new()
        .read(true) // 可读
        .write(true) // 可写
        .append(true) // 追加内容
        .create(true) // 新建,若文件存在则打开这个文件
        .open(file_path)
        .unwrap()
}

/// 创建父文件夹
pub fn create_pdir(file_path: &str) {
    //创建父目录
    let path = Path::new(file_path);
    let prefix = path.parent().unwrap();
    if !prefix.exists() {
        std::fs::create_dir_all(prefix).unwrap();
    }
}
/// 创建新文件,文件存在会清空内容
pub fn create_file(file_path: &str) -> Result<File, std::io::Error> {
    //创建父目录
    create_pdir(file_path);
    File::create(file_path)
}

/// 判断文件是否存在
pub fn exists(path: &str) -> bool {
    Path::new(path).exists()
}
/// 判断是否是文件
pub fn is_file(path: &str) -> bool {
    Path::new(path).is_file()
}
/// 判断是否是文件夹
pub fn is_dir(path: &str) -> bool {
    Path::new(path).is_dir()
}

/// 创建文件夹
pub fn create_dir_all(dir_path: &str) {
    if !exists(dir_path) {
        std::fs::create_dir_all(dir_path).unwrap();
    }
}

/// 列出文件夹名称
pub fn list_dir_name(dir_path: &str) -> Vec<String> {
    let mut ret = vec![];
    if exists(dir_path) {
        for ele in read_dir(dir_path).unwrap() {
            let ele = ele.unwrap();
            if ele.metadata().unwrap().is_dir() {
                ret.push(ele.file_name().to_str().unwrap().to_string())
            }
        }
    }
    ret
}

/// 列出文件名称
pub fn list_file_name(dir_path: &str) -> Vec<String> {
    let mut ret = Vec::new();
    if exists(dir_path) {
        for ele in read_dir(dir_path).unwrap() {
            let ele = ele.unwrap();
            if ele.metadata().unwrap().is_file() {
                ret.push(ele.file_name().to_str().unwrap().to_string())
            }
        }
    }
    ret
}

/// 列出目录下所有文件
pub fn list_all_file(dir_path: &str, filter: fn(path: &str) -> bool) -> Vec<String> {
    let mut ret = Vec::new();
    if exists(dir_path) {
        match read_dir(dir_path) {
            Ok(entrys) => {
                for ele in entrys {
                    let ele = ele.unwrap();
                    let metadata = ele.metadata().unwrap();
                    let path = ele.path().display().to_string();
                    let path = path.replace("\\", "/");
                    if metadata.is_dir() {
                        let mut files = list_all_file(path.as_str(), filter);
                        ret.append(&mut files);
                    }
                    if metadata.is_file() {
                        if filter(&path) {
                            ret.push(path)
                        }
                    }
                }
            }
            Err(err) => {
                eprintln!("{err}")
            }
        }
    }
    ret
}
/// 根据指定行数分割文件
pub fn splite_file_by_lines(path: &str, out_path: &str, splite_lines: i32) -> Vec<String> {
    let (_, name, ext) = file_path_attr(path);
    let mut new_files = vec![];
    match File::open(path) {
        Ok(input) => {
            let buffered = BufReader::new(input);
            let mut lines = 0;
            let mut str = String::new();
            for line in buffered.lines() {
                if let Ok(s) = line {
                    str.push_str(&s);
                    str.push_str("\n");
                    lines += 1;
                    if lines > 0 && lines % splite_lines == 0 {
                        let new_file_name =
                            format!("{out_path}/{name}_{}_{lines}.{ext}", lines - splite_lines);
                        create_file(&new_file_name)
                            .unwrap()
                            .write_all(str.as_bytes())
                            .unwrap();
                        str.clear();
                        new_files.push(new_file_name);
                    }
                }
            }
            if !str.is_empty() {
                let new_file_name =
                    format!("{out_path}/{name}_{}_{lines}.{ext}", lines - splite_lines);
                create_file(&new_file_name)
                    .unwrap()
                    .write_all(str.as_bytes())
                    .unwrap();
                new_files.push(new_file_name);
            }
        }
        Err(err) => {
            println!("{err}");
        }
    }
    new_files
}

pub fn read_line<T>(path: &str, deal_line: fn(line: String) -> Option<T>) -> Vec<T> {
    let mut vec = Vec::new();
    match File::open(path) {
        Ok(input) => {
            let buffered = BufReader::new(input);
            for line in buffered.lines() {
                if let Ok(s) = line {
                    let value = deal_line(s);
                    match value {
                        Some(v) => {
                            vec.push(v);
                        }
                        None => {}
                    }
                }
            }
        }
        Err(err) => {
            println!("{err}");
        }
    }
    vec
}

pub fn read_line_map<K, V, T>(
    path: &str,
    deal_line: fn(line: String) -> Option<T>,
    kvf: fn(t: T) -> (K, V),
) -> HashMap<K, V>
where
    K: Eq + Hash,
{
    let mut vec: HashMap<K, V> = HashMap::new();
    match File::open(path) {
        Ok(input) => {
            let buffered = BufReader::new(input);
            for line in buffered.lines() {
                if let Ok(s) = line {
                    let value = deal_line(s);
                    match value {
                        Some(v) => {
                            let (k, v) = kvf(v);
                            vec.insert(k, v);
                        }
                        None => {}
                    }
                }
            }
        }
        Err(err) => {
            println!("{err}");
        }
    }
    vec
}

/// 文件大小
pub fn file_size(path: &str) -> u64 {
    if exists(path) {
        let meta = fs::symlink_metadata(path).unwrap();
        meta.len()
    } else {
        0
    }
}

/// 读取文件最后一行
pub fn read_last_line(path: &str, buf_size: u64) -> Option<String> {
    match File::open(path) {
        Ok(mut input) => {
            let file_size = file_size(path);
            if file_size > buf_size {
                let start_idx = file_size - buf_size;
                input.seek(SeekFrom::Start(start_idx)).unwrap();
            }
            let bf = BufReader::new(input);
            match bf.lines().last() {
                Some(line) => match line {
                    Ok(l) => {
                        return Some(l);
                    }
                    Err(err) => {
                        println!("seek 失败 {err}");
                        None
                    }
                },
                None => None,
            }
        }
        Err(_) => {
            // eprintln!("打开文件{path}失败,{err}");
            None
        }
    }
}

/// 获取文件行数
pub fn line_size(path: &str) -> usize {
    if let Ok(f) = File::open(path) {
        let file_size = file_size(path);
        println!("size={file_size}");
        if file_size > 0 {
            let buffered = BufReader::new(f);
            return buffered.lines().count();
        }
    }
    0
}

// 加载目录文件
pub async fn load_async<T>(path: &str, filter: fn(path: &str) -> bool) -> Vec<T>
where
    T: FromStr + Default + Send + 'static,
{
    let files;
    if is_dir(path) {
        files = list_all_file(path, filter);
    } else {
        files = vec![path.to_string()]
    }

    let mut hds = vec![];
    for ele in files {
        hds.push(tokio::spawn(async move {
            let items: Vec<T> = load_file(&ele);
            items
        }));
    }
    let mut all = Vec::new();
    for ele in hds {
        match ele.await {
            Ok(mut items) => {
                all.append(&mut items);
            }
            Err(err) => {
                println!("{err}")
            }
        }
    }
    all
}
// 加载目录文件
pub fn load<T>(path: &str, filter: fn(path: &str) -> bool) -> Vec<T>
where
    T: FromStr + Default,
{
    let files;
    if is_dir(path) {
        files = list_all_file(path, filter);
    } else {
        files = vec![path.to_string()]
    }
    let mut all = Vec::new();
    for ele in files {
        let mut items = load_file(&ele);
        all.append(&mut items);
    }
    all
}

// 加载目录文件
pub fn load_map<K, V, T>(
    path: &str,
    filter: fn(path: &str) -> bool,
    kvf: fn(t: T) -> (K, V),
) -> HashMap<K, V>
where
    T: FromStr + Default,
    K: std::hash::Hash + std::cmp::Eq,
{
    let files;
    if is_dir(path) {
        files = list_all_file(path, filter);
    } else {
        files = vec![path.to_string()];
    }
    let mut t = (HashMap::new(), kvf);
    for ele in files {
        load_file_by_line(&ele, &mut t, |l, m| match T::from_str(&l) {
            Ok(t) => {
                let (k, v) = m.1(t);
                m.0.insert(k, v);
            }
            Err(_) => {}
        });
    }
    t.0
}

// 加载文件
pub fn load_file<T>(path: &str) -> Vec<T>
where
    T: FromStr + Default,
{
    read_line(path, |s| match T::from_str(&s) {
        Ok(t) => Some(t),
        Err(_) => {
            println!("line {s} pase err");
            None
        }
    })
}

// 加载文件为map
pub fn load_file_map<T, K, V>(path: &str, kvf: fn(t: T) -> (K, V)) -> HashMap<K, V>
where
    T: FromStr + Default,
    K: Hash + Eq,
{
    read_line_map(
        path,
        |s| match T::from_str(&s) {
            Ok(t) => Some(t),
            Err(_) => {
                println!("line {s} pase err");
                None
            }
        },
        kvf,
    )
}
/// 获取文件目录,文件名,拓展名
pub fn file_path_attr(file_path: &str) -> (&str, &str, &str) {
    let path = Path::new(file_path);
    let dir = path.parent().unwrap().to_str().unwrap();
    let name = path.file_stem().unwrap().to_str().unwrap();
    let ext = path.extension().unwrap().to_str().unwrap();
    (dir, name, ext)
}

// 按行加载文件
pub fn load_file_by_line<T>(path: &str, t: &mut T, line_fn: fn(line: String, t: &mut T)) {
    match File::open(path) {
        Ok(input) => {
            let buffered = BufReader::new(input);
            for line in buffered.lines() {
                if let Ok(line) = line {
                    line_fn(line, t);
                }
            }
        }
        Err(err) => {
            println!("{err}");
        }
    }
}

// 加载目录文件
pub fn load_by_line<T>(
    //加载目录
    path: &str,
    //过滤器
    filter: fn(path: &str) -> bool,
    //数据
    t: &mut T,
    //处理行数据方法
    line_fn: fn(line: String, t: &mut T),
) {
    let files;
    if is_dir(path) {
        files = list_all_file(path, filter);
    } else {
        files = vec![path.to_string()];
    }
    for ele in files {
        load_file_by_line(&ele, t, line_fn);
    }
}

/**

按天分割文件

# Examples

```
use caisin::files::splite_file_by_date;
let file_path="test";//文件目录
let out_path="out";//输出目录
let sufix=".data";//新生成文件结尾
let filter=|s:&str|s.ends_with(".data");
let spl="\x01"; //行字段分割
let time_idx=3; //时间字段位置
let limit_size=100;//字符串达到指定长度则写入一次文件,防止内存占用过高
// let fmt="yyyy-mm-dd hh:mm:ss";
let fmt="ts";
splite_file_by_date(file_path, out_path, sufix, filter, spl, time_idx, limit_size, fmt);
```
*/
pub fn splite_file_by_date(
    file_path: &str,
    out_path: &str,
    sufix: &str,
    filter: fn(&str) -> bool,
    spl: &str,
    time_idx: usize,
    limit_size: usize,
    fmt: &str,
) {
    let mut f_paths = vec![];
    if is_file(file_path) {
        f_paths.push(file_path.to_string());
    } else {
        f_paths = list_all_file(file_path, filter);
    }
    let mut f_map: HashMap<String, String> = HashMap::new();
    for f_path in f_paths {
        match File::open(f_path) {
            Ok(input) => {
                let buffered = BufReader::new(input);
                for line in buffered.lines() {
                    if let Ok(mut line) = line {
                        let items: Vec<&str> = line.split(spl).collect();
                        if items.len() < time_idx + 1 {
                            eprintln!(
                                "{line} len is {} time_idx:{time_idx} out of index",
                                items.len()
                            );
                            continue;
                        }
                        let time_item = items[time_idx];
                        let date_path = match fmt {
                            "ts" => {
                                let t = times::unix_str_2_date_time(time_item);
                                times::get_date_path(&t)
                            }
                            _ => {
                                let t = times::parse_date_time(time_item);
                                t.format(times::YMD_PATH).to_string()
                            }
                        };
                        line.push('\n');
                        if f_map.contains_key(&date_path) {
                            let content = f_map.get_mut(&date_path).unwrap();
                            //如果有限制写入缓存大小则先写入,防止文件过多过大,导致内存溢出
                            if limit_size > 0 {
                                if content.len() > limit_size {
                                    write_bytes(
                                        &format!("{out_path}/{date_path}{sufix}"),
                                        content.as_bytes(),
                                    );
                                    content.clear();
                                }
                            }
                            content.push_str(&line);
                        } else {
                            f_map.insert(date_path, line);
                        }
                    }
                }
            }
            Err(err) => {
                println!("{err}");
            }
        }
    }
    //余下的写入文件
    for (date_path, content) in f_map {
        write_bytes(
            &format!("{out_path}/{date_path}{sufix}"),
            content.as_bytes(),
        );
    }
}

/// 将文件转为指定大小块
pub fn block_file(
    file_path: &str,
    filter: fn(&str) -> bool,
    out_path: &str,
    sufix: &str,
    block_site: usize,
) {
    let mut f_paths = vec![];
    if is_file(file_path) {
        f_paths.push(file_path.to_string());
    } else {
        f_paths = list_all_file(file_path, filter);
    }
    let mut str = String::new();
    let mut idx = 0;
    for f_path in f_paths {
        match File::open(f_path) {
            Ok(input) => {
                let buffered = BufReader::new(input);
                for line in buffered.lines() {
                    if let Ok(line) = line {
                        if str.len() > block_site {
                            write_bytes(&format!("{out_path}/{idx:0>4}{sufix}"), str.as_bytes());
                            str.clear();
                            idx += 1;
                        }
                        str.push_str(&line);
                        str.push('\n');
                    }
                }
            }
            Err(err) => {
                eprintln!("{err}")
            }
        }
    }
    if !str.is_empty() {
        write_bytes(&format!("{out_path}/{idx:0>4}{sufix}"), str.as_bytes());
    }
}

fn write_bytes(file_path: &str, bytes: &[u8]) {
    open_file(file_path).write_all(bytes).unwrap()
}

/// 根据字段索引分割文件
pub fn splite_file_by_field_idx(
    file_path: &str,
    out_path: &str,
    sufix: &str,
    filter: fn(&str) -> bool,
    spl: &str,
    split_idx: usize,
    keep_idxs: &str,
    limit_size: usize,
) {
    let mut f_paths = vec![];
    if is_file(file_path) {
        f_paths.push(file_path.to_string());
    } else {
        f_paths = list_all_file(file_path, filter);
    }
    let mut f_map: HashMap<String, String> = HashMap::new();
    let keep_idxs: Vec<usize> = strs::str_2_vec(keep_idxs, ",");
    for f_path in f_paths {
        match File::open(f_path) {
            Ok(input) => {
                let buffered = BufReader::new(input);
                for line in buffered.lines() {
                    if let Ok(line) = line {
                        let items: Vec<&str> = line.split(spl).collect();
                        if items.len() < split_idx + 1 {
                            continue;
                        }
                        let item = items[split_idx];
                        if f_map.contains_key(item) {
                            let content = f_map.get_mut(item).unwrap();
                            if limit_size > 0 {
                                if content.len() > limit_size {
                                    write_bytes(
                                        &format!("{out_path}/{item}{sufix}"),
                                        content.as_bytes(),
                                    );
                                    content.clear();
                                }
                            }
                            if keep_idxs.is_empty() {
                                content.push_str(&line);
                            } else {
                                let mut keep_items = vec![];
                                for idx in &keep_idxs {
                                    keep_items.push(items[*idx]);
                                }
                                content.push_str(&keep_items.join(spl));
                            }
                            content.push('\n');
                        } else {
                            f_map.insert(item.to_string(), line);
                        }
                    }
                }
            }
            Err(err) => {
                println!("{err}")
            }
        }
    }
    for (k, v) in f_map {
        if !v.is_empty() {
            write_bytes(&format!("{out_path}/{k}{sufix}"), v.as_bytes());
        }
    }
}

#[test]
fn test_f() {
    let str = "wx1_111";
    let c = "_";
    let (pre, suf, ..) = match str.find(c) {
        Some(idx) => (&str[..idx], &str[idx + c.len()..], true),
        None => (str, "", false),
    };
    println!("{pre},{suf}")
}