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

///all Traits
#[allow(warnings)]
pub mod traits {
    use std::{
        fmt::{Debug, Display},
        path::{Path, PathBuf},
        str::FromStr,
    };

    pub trait DynType: ToString {}
    impl<T: ToString> DynType for T {}
    /// this is a ToStringWrapper Struct for implementing Into to Box<dyn DynType>
    #[derive(Debug, Clone)]
    pub struct ToDynType<T: ToString>(pub T);
    impl<T: ToString> Into<Box<dyn DynType>> for ToDynType<T> {
        fn into(self) -> Box<dyn DynType> {
            Box::new(self.0.to_string())
        }
    }
    use std::ops::Deref;
    pub trait Join {
        fn join(&self, by: &str) -> String;
    }
    
    impl<T: ToString> Join for [T] {
        fn join(&self, by: &str) -> String {
            self.iter()
                .map(|item| item.to_string())
                .collect::<Vec<String>>()
                .join(by)
        }
    }
    pub trait Str {
        /// ## implment cut format for str and String
        /// ```rust
        ///fn main() {
        ///    use doe::Str;
        ///    use doe::targets;
        ///    let a = "this is a demo {}";
        ///    let c = a.cut(-1,-2,1);
        ///    let f = a.format(targets!("{}"=>"this is a demo"));
        ///    println!("{:?}",f);
        ///    println!("{:?}",c);
        ///}
        /// ```
        fn cut(&self, start: i32, end: i32, step: usize) -> String;
        /// ## implment format for str and String
        /// ```rust
        ///fn main() {
        ///    use doe::*;
        ///    "this is a {s},I like Trait Object {p}%"
        ///    .format(vec![(Box::new("{s}"),Box::new("demo")),(Box::new("{p}"),Box::new(100))]).println();//this is a demo,I like Trait Object 100%
        ///     use doe::targets;
        ///     "this is a {d},I like Trait Object {p}}%"
        ///     .format(targets!{"{d}"=>"demo","{p}"=>100})
        ///     .println(); //this is a demo,I like Trait Object 100%
        ///     
        ///     let a = "demo";
        ///     let b_a: Box<str> = a.to_box();
        ///     b_a.as_ref().dprintln();
        ///}
        /// ```
        fn format(&self, targets: Vec<(Box<dyn ToString>, Box<dyn ToString>)>) -> String;
        /// str String to Path
        fn to_path(&self) -> Box<Path>;
        /// str String to PathBuf
        fn to_pathbuf(&self) -> PathBuf;
        ///split string by delimiter into Vec<String>
        fn split_to_vec(&self,delimiter:&str) ->Vec<String>;
        ///string into Box<str>
        fn to_box(&self) ->Box<str>;
       
    }
    impl<S: AsRef<str>> Str for S {
        fn cut(&self, start: i32, end: i32, step: usize) -> String {
            let mut res = String::new();
            if start >= 0 && end >= 0 && start <= end {
                let start = start as usize;
                let end = end as usize;
                for (i, c) in self.as_ref().chars().enumerate().step_by(step) {
                    if i >= start && i <= end {
                        res.push(c);
                    }
                }
            } else if start < 0 && end < 0 {
                if start > end {
                    let start = self.as_ref().len() as i32 + end;
                    let end = self.as_ref().len() as i32 + start;
                    let start = start as usize;
                    let end = end as usize;
                    for (i, c) in self.as_ref().chars().enumerate().step_by(step) {
                        if i >= start && i <= end {
                            res.push(c);
                        }
                    }
                } else {
                    let start = self.as_ref().len() as i32 + start;
                    let end = self.as_ref().len() as i32 + end;
                    let start = start as usize;
                    let end = end as usize;
                    for (i, c) in self.as_ref().chars().enumerate().step_by(step) {
                        if i >= start && i <= end {
                            res.push(c);
                        }
                    }
                }
            } else if start > 0 && end < 0 {
                let start = start as usize;
                let end = self.as_ref().len() as i32 + end;
                let end = end as usize;
                for (i, c) in self.as_ref().chars().enumerate().step_by(step) {
                    if i >= start && i <= end {
                        res.push(c);
                    }
                }
            }
            res
        }
        fn format(&self, targets: Vec<(Box<dyn ToString>, Box<dyn ToString>)>) -> String {
            let mut s_mut = self.as_ref().to_string();
            for t in targets {
                s_mut = s_mut.replace(&t.0.deref().to_string(), &t.1.deref().to_string());
            }
            s_mut
        }
        fn to_path(&self) -> Box<Path> {
            Path::new(self.as_ref()).to_owned().into()
        }
        fn to_pathbuf(&self) -> PathBuf {
            PathBuf::from_str(self.as_ref()).unwrap()
        }
        fn split_to_vec(&self,delimiter:&str) ->Vec<String>{
            crate::split_to_vec!(self.as_ref(),delimiter)
        }
        fn to_box(&self) ->Box<str> {
            Box::from(self.as_ref())
        }
    }


    /// A trait for impl Debug
    pub trait DebugPrint {
        /// debug print
        /// ```rust
        /// use doe::DebugPrint;
        /// let get = |url:&str|url.to_string().dprint();
        /// ```
        fn dprint(&self);
        /// debug eprint
        /// ```rust
        /// use doe::DebugPrint;
        /// let get = |url:&str|url.to_string().deprint();
        /// ```
        fn deprint(&self);
        /// debug println
        /// ```rust
        /// use doe::DebugPrint;
        /// let get = |url:&str|url.to_string().dprintln();
        /// ```
        fn dprintln(&self);
        /// debug eprintln
        /// ```rust
        /// use doe::DebugPrint;
        /// let get = |url:&str|url.to_string().deprintln();
        /// ```
        fn deprintln(&self);
        /// ```rust
        /// fn main() {
        ///     use doe::DebugPrint;
        ///     #[derive(Debug)]
        ///     struct Demo{}
        ///     let d = Demo{};
        ///     d.dbg();
        /// }
        /// ```
        fn dbg(&self);
    }
    impl<T> DebugPrint for T
    where
        T: Debug,
    {
        fn dprint(&self) {
            print!("{:?}", self);
        }
        fn deprint(&self) {
            eprint!("{:?}", self);
        }

        fn dprintln(&self) {
            println!("{:?}", self);
        }
        fn deprintln(&self) {
            eprintln!("{:?}", self);
        }
        fn dbg(&self){
            dbg!(self);
        }
    }
    pub trait Print {
        /// ```rust
        /// use doe::Print;
        /// let get = |url:&str|url.to_string().print();
        /// ```
        fn print(&self);
        /// ```rust
        /// use doe::Print;
        /// let get = |url:&str|url.to_string().print();
        /// ```
        fn eprint(&self);
        /// ```rust
        /// use doe::Print;
        /// let get = |url:&str|url.to_string().println();
        /// ```
        fn println(&self);
        /// ```rust
        /// use doe::Print;
        /// let get = |url:&str|url.to_string().println();
        /// ```
        fn eprintln(&self);

    }
    impl<T> Print for T
    where
        T: Display,
    {
        fn print(&self) {
            print!("{}", self);
        }
        fn eprint(&self) {
            eprint!("{}", self);
        }

        fn println(&self) {
            println!("{}", self);
        }
        fn eprintln(&self) {
            eprintln!("{}", self);
        }
    }
}