test5/
lib.rs

1use std::{collections::BTreeSet, iter::FromIterator};
2use std::path::Path;
3
4pub mod m1 {
5    pub fn f1() -> i32 {
6        println!("edderocks1111111111111111");
7        111
8    }
9
10    pub fn f2() -> i32 {
11        println!("edderocks222222222222222222222222");
12        222
13    }
14}
15
16pub fn f3() -> i32 {
17    println!("fffffffffffffffffff333333333333333333333333");
18    333
19}
20
21
22
23pub trait MyTraitForStringSlice<'a> {
24    fn equals_to_one_of_2(self, a: &str, b: &str) -> bool;
25    fn equals_to_one_of_3(self, a: &str, b: &str, c: &str) -> bool;
26    fn get_regex_matched_group_n(self, regex: &str, n: usize) -> Option<String>;
27    fn name_increase_width_2(self) -> String;
28    fn to_path(self) -> &'a Path;
29    fn to_vec_char(self) -> Vec<char>;
30    fn to_vec_u8(self) -> Vec<u8>;
31    fn ends_with_vec(self, v: Vec<&str>) -> bool;
32    fn get_file_name(self) -> &'a str;
33    fn get_folder_name(self) -> &'a str;
34}
35
36impl<'a> MyTraitForStringSlice<'a> for &'a str {
37    fn equals_to_one_of_2(self, a: &str, b: &str) -> bool {
38        if self == a || self == b {
39            return true;
40        }
41        false
42    }
43
44    fn equals_to_one_of_3(self, a: &str, b: &str, c: &str) -> bool {
45        if self == a || self == b || self == c {
46            return true;
47        }
48        false
49    }
50
51    fn get_regex_matched_group_n(self, regex: &str, n: usize) -> Option<String> {
52        let re = regex::Regex::new(regex).unwrap();
53        let result = re.captures(self)?;
54        Some(result[n].to_string())
55    }
56
57    fn name_increase_width_2(self) -> String {
58        fn inc(s: &str) -> String {
59            let mut i = s.parse::<i32>().unwrap();
60            i = i + 1;
61            let width = s.chars().count();
62            format!("[{:0>w$}]", i, w = width)
63        }
64
65        fn get_default(w: usize) -> String {
66            format!("[{}]", "0".repeat(w))
67        }
68
69        let width: usize = 2;
70        let file = Path::new(self);
71        let re = regex::Regex::new(&format!(r"(?i)^(.+?)(\[({})\])?(\.\w+)?$", r"\d".repeat(width))).unwrap();
72        let result = re.captures(file.to_str().unwrap()).unwrap();
73
74        let mut t;
75        if result.get(2).is_none() {
76            t = format!("{}{}{}", &result.get(1).map_or("", |e| e.as_str()), get_default(width), &result.get(4).map_or("", |e| e.as_str()));
77        } else {
78            t = format!("{}{}{}", &result.get(1).map_or("", |e| e.as_str()), inc(&result.get(3).map(|e| e.as_str()).unwrap()), &result.get(4).map_or("", |e| e.as_str()));
79        }
80
81        while t.as_str().to_path().is_file() || t.as_str().to_path().is_dir() {
82            t = t.name_increase_width_2();
83        }
84        t
85    }
86
87    fn to_path(self) -> &'a Path {
88        Path::new(self)
89    }
90
91    fn to_vec_char(self) -> Vec<char> {
92        self.chars().collect::<Vec<_>>()
93    }
94
95    fn to_vec_u8(self) -> Vec<u8> {
96        self.as_bytes().to_vec()
97    }
98
99    fn ends_with_vec(self, v: Vec<&str>) -> bool {
100        for e in v {
101            if self.ends_with(e) {
102                return true;
103            }
104        }
105        false
106    }
107
108    fn get_file_name(self) -> &'a str {
109        let x = Path::new(self);
110        x.file_name().unwrap().to_str().unwrap()
111    }
112
113    fn get_folder_name(self) -> &'a str {
114        let x = Path::new(self);
115        x.parent().map_or("", |e| e.to_str().unwrap())
116    }
117}
118
119