qust-ds 0.1.6

a part of crate qust
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
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817

use serde::{Deserialize, Serialize};
use std::{
    fmt::Debug, fs::{self, DirEntry, ReadDir}, hash::Hash, io::{self, Write}, path::Path, sync::Mutex, time::Instant
};
use crate::prelude::*;
use chrono::{Local, ParseError, Timelike};
/* #region dt */
pub trait Fromt<T> {
    fn fromt(data: &T) -> Self;
}

impl Fromt<dt> for dt {
    fn fromt(data: &dt) -> Self {
        *data
    }
}
impl Fromt<da> for dt {
    fn fromt(data: &da) -> Self {
        data.and_hms_opt(0, 0, 0).unwrap()
    }
}
impl Fromt<Year> for dt {
    fn fromt(data: &Year) -> Self {
        dt::fromt(&da::fromt(data))
    }
}
impl Fromt<&str> for dt {
    fn fromt(data: &&str) -> Self {
        dt::parse_from_str(data, "%Y-%m-%d %H:%M:%S%.f").unwrap()
    }
}

pub trait ToDt {
    fn to_dt(&self) -> dt;
}

impl<T> ToDt for T where dt: Fromt<T> {
    fn to_dt(&self) -> dt {
        dt::fromt(self)
    }
}
/* #endregion */

/* #region da */
impl Fromt<dt> for da {
    fn fromt(data: &dt) -> Self {
        data.date()
    }
}
impl Fromt<da> for da {
    fn fromt(data: &da) -> Self {
        *data
    }
}
impl Fromt<Year> for da {
    fn fromt(data: &Year) -> Self {
        da::from_ymd_opt(data.0 as i32, 1, 1).unwrap()
    }
}
impl Fromt<usize> for da {
    fn fromt(data: &usize) -> Self {
        let year = data / 10000;
        let month_date = data % 10000;
        let month = month_date / 100;
        let date = month_date % 100;
        da::from_ymd_opt(year as i32, month as u32, date as u32).unwrap()
    }
}
impl Fromt<String> for da {
    fn fromt(data: &String) -> Self {
        da::parse_from_str(data, "%Y-%m-%d").unwrap()
    }
}
impl Fromt<&str> for da {
    fn fromt(data: &&str) -> Self {
        let res = da::parse_from_str(data, "%Y-%m-%d");
        res.expect(data)
    }
}
pub trait ToDa {
    fn to_da(&self) -> da;
}

impl<T> ToDa for T where da: Fromt<T> {
    fn to_da(&self) -> da {
        da::fromt(self)
    }
}

/* #endregion */

/* #region tt */
impl Fromt<dt> for tt {
    fn fromt(data: &dt) -> Self {
        tt::from_hms_opt(data.hour(), data.minute(), data.second()).unwrap()
    }
}

impl Fromt<f64> for tt {
    fn fromt(data: &f64) -> Self {
        let usize_part = *data as u32;
        let float_part = ((data % usize_part as f64) * 1000.) as u32;
        let hour = usize_part / 10000;
        let minitue_second = usize_part % 10000;
        let minitue = minitue_second / 100;
        let second = minitue_second % 100;
        tt::from_hms_milli_opt(hour, minitue, second, float_part).unwrap()
    }
}
impl Fromt<usize> for tt {
    fn fromt(data: &usize) -> Self {
        tt::fromt(&(*data as f64))
    }
}
impl Fromt<&str> for tt {
    fn fromt(data: &&str) -> Self {
        tt::parse_from_str(data, "%H:%M:%S").unwrap()
    }
}

pub trait ToTt {
    fn to_tt(&self) -> tt;
}

impl<T> ToTt for T where tt: Fromt<T> {
    fn to_tt(&self) -> tt {
        tt::fromt(self)
    }
}

/* #endregion */

/* #region Year */
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Year(pub usize);

impl Fromt<usize> for Year {
    fn fromt(data: &usize) -> Self {
        Year(*data)
    }
}

pub trait ToYear {
    fn to_year(&self) -> Year;
}

impl<T> ToYear for T where Year: Fromt<T> {
    fn to_year(&self) -> Year {
        Year::fromt(self)
    }
}
/* #endregion */

/* #region ForCompare */
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum ForCompare<T> {
    After(T),
    Before(T),
    Between(std::ops::Range<T>),
    List(Vec<Box<ForCompare<T>>>),
}
impl<T: std::fmt::Debug> std::fmt::Display for ForCompare<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        <Self as std::fmt::Debug>::fmt(self, f)
    }
}

impl<T> ForCompare<T> 
where 
    T: PartialOrd,
{
    pub fn compare_time<N>(&self, other: &N) -> bool 
    where 
        T: Fromt<N>, 
    {
        self.compare_same(&T::fromt(other))
    }

    pub fn compare_same(&self, other: &T) -> bool {
        match self {
            ForCompare::After(x) => other >= x,
            ForCompare::Before(x) => other < x,
            ForCompare::Between(x) => x.contains(other),
            ForCompare::List(x) => {
                for i in x.iter() {
                    if i.compare_same(other) { 
                        return true
                    }
                }
                false
            }
        }
    }

    pub fn compare<N>(&self, other: N) -> bool
    where
        T: From<N>,
    {
        self.compare_same(&T::from(other))
    }
}
/* #endregion */

/* #region Convenient select */
pub trait TimeSelect: ToDt {
    fn before(&self) -> ForCompare<dt> {
        ForCompare::Before(self.to_dt())
    }

    fn after(&self) -> ForCompare<dt> {
        ForCompare::After(self.to_dt())
    }

    fn to<T: ToDt>(&self, other: T) -> ForCompare<dt> {
        ForCompare::Between(self.to_dt()..other.to_dt())
    }
}
impl<T: ToDt + Clone> TimeSelect for T {}

pub fn last_days(n: i64) -> ForCompare<dt> {
    let today = Local::now().date_naive();
    let start_date = today - chrono::Duration::days(n);
    start_date.to_da().after()
}
/* #endregion */

pub trait ToDa2 {
    fn to_da2(self) -> Result<da, ParseError>;
}

impl ToDa2 for &str {
    fn to_da2(self) -> Result<da, ParseError> {
        da::parse_from_str(self, "%Y%m%d")
    }
}

/* #region ProgressBar */
pub struct ProgressBar<T> {
    t: Instant,
    total: Vec<T>,
    last_i: Mutex<usize>,
    last_l: Mutex<usize>,
    thre: usize,
    count: usize,
    size: usize,
}

impl<T> ProgressBar<T> {
    pub fn inc(&self) {
        *self.last_i.lock().unwrap() += 1;
        let b = *self.last_i.lock().unwrap();
        let l = *self.last_l.lock().unwrap();
        let t = b.checked_sub(l);
        match t {
            Some(i) => {
                if i >= self.thre || (self.size >= self.thre && b == self.size) {
                    *self.last_l.lock().unwrap() = b;
                    self.print();
                }
            }
            None => {
                println!("b: {b} l: {l}");
            }
        }
    }
    pub fn print(&self) {
        println!("{:?} / {:?}, {:.0?}", self.last_i.lock().unwrap(), self.size, self.t.elapsed());
    }
}

impl<'a, T> Iterator for ProgressBar<&'a T> {
    type Item = &'a T;
    fn next(&mut self) -> Option<Self::Item> {
        let res = if self.count < self.size {
            self.inc();
            Some(self.total[self.count])
        } else {
            None
        };
        self.count += 1;
        res
    }
}

pub trait ToProgressBar {
    type Output<'a> where Self: 'a;
    fn to_progressbar(&self) -> ProgressBar<Self::Output<'_>>;
}

impl<T> ToProgressBar for [T] {
    type Output<'a> = &'a T where Self: 'a;
    fn to_progressbar(&self) -> ProgressBar<Self::Output<'_>> {
        ProgressBar {
            t: Instant::now(),
            size: self.len(),
            total: self.iter().collect_vec(),
            last_i: Mutex::new(0),
            last_l: Mutex::new(0),
            thre: 1000,
            count: 0,
        }
    }
}

impl Iterator for ProgressBar<usize> {
    type Item = usize;
    fn next(&mut self) -> Option<Self::Item> {
        let res = if self.count < self.size {
            self.inc();
            Some(self.total[self.count])
        } else {
            None
        };
        self.count += 1;
        res
    }
}

impl ToProgressBar for usize {
    type Output<'a> = usize;
    fn to_progressbar(&self) -> ProgressBar<Self::Output<'_>> {
        ProgressBar {
            t: Instant::now(),
            size: *self,
            total: (0..*self).collect_vec(),
            last_i: Mutex::new(0),
            last_l: Mutex::new(0),
            thre: 1000,
            count: 0,
        }
    }
}

impl ToProgressBar for (usize, usize) {
    type Output<'a> = usize;
    fn to_progressbar(&self) -> ProgressBar<Self::Output<'_>> {
        ProgressBar {
            t: Instant::now(),
            size: self.0,
            total: (0..self.0).collect_vec(),
            last_i: Mutex::new(0),
            last_l: Mutex::new(0),
            thre: self.1,
            count: 0,
        }
    }
}

impl ToProgressBar for  (usize, f32) {
    type Output<'a> = usize;
    fn to_progressbar(&self) -> ProgressBar<Self::Output<'_>> {
        (self.0, (self.0 as f32 * self.1) as usize).to_progressbar()
    }
}
/* #endregion */

pub trait Pip: Sized {
    fn pip<F: Fn(Self) -> T, T>(self, f: F) -> T {
        f(self)
    }
    fn pip_ref<F: Fn(&Self) -> T, T>(&self, f: F) -> T {
        f(self)
    }
    fn pip_clone<F, T>(&self, f: F) -> T
    where
        Self: Clone,
        F: Fn(Self) -> T,
    {
        self.clone().pip(f)
    }
}
impl<T> Pip for T {}

pub trait BoolToOption: Sized {
    fn bool_to_option<F>(self, f: F) -> Option<Self>
    where
        F: Fn(&Self) -> bool,
    {
        if f(&self) { Some(self) } else { None }
    }
    fn bool_to_option_else<F, N>(self, f: F, other: N) -> Option<N>
    where
        F: Fn(Self) -> bool,
    {
        if f(self) { Some(other) } else { None }
    }
}
impl<T> BoolToOption for T {}


pub trait StrHandle: AsRef<str> {
    fn split_path(&self) -> (&str, &str) {
        let p = Path::new(self.as_ref());
        (
            p.file_name().unwrap().to_str().unwrap(), 
            p.parent().unwrap().to_str().unwrap()
        )
    }
}
impl<T: AsRef<str>> StrHandle for T {}

///```
///     let k = HandleDir { 
///  dir: String::from("D:/Rust/arcta/data"),
///   exclude: vec![String::from("Rtick")],
///   let x_path = x.split_path();
///    let y_path = y.split_path();
///    let pcon = rof::<Pcon>(x_path.0, x_path.1);
///    let price = pcon.price.clone();
///    (
///        pcon.ident(),
///        pcon.price.t,
///        vec![price.o, price.h, price.l, price.c, price.v],
///        price.ct,
///    ).sof(y_path.0, y_path.1);
///}
///};
///k.file_move_change("D:/Rust/arcta/data2");
/// 
/// use std::fs;
///DirHandle { 
///    dir: "..".into(),
///    exclude: vec!["notebook".into(), "target".into(), "axum".into()],
///    f: |x: &str, y: &str| { fs::copy(x, y); },
///}
///    .file_move_change("../axum/crates");
/// 
/// {
///let dir_handle = DirHandle {
///    dir: String::from("/root/arcta/data/Rtick"),
///    exclude: vec![],
///    f: |x: &str, y: &str| {
///        let x_path = x.split_path();
///        let y_path = y.split_path();
///        let price_tick = rof::<PriceTick>(x_path.0, x_path.1);
///        let price_tick2: PriceTickVersion = price_tick.into();
///        price_tick2.sof(y_path.0, y_path.1);
///    }
///};
///dir_handle.file_move_change("/mnt/d/Rtick");
///}
/// ```
pub struct DirHandle<T> {
    pub dir: String,
    pub exclude: Vec<String>,
    pub f: T
}

impl<T> DirHandle<T>
where
    T: Fn(&str, &str) + Clone,
{
    pub fn read_dir(&self) -> ReadDir {
        let p = Path::new(&self.dir);
        fs::read_dir(p).unwrap()
    }

    pub fn file_move_change(&self, target: &str) {
        if !Path::new(target).is_dir() {
            fs::create_dir(target).unwrap_or_else(|_| panic!("not such dir: {:?}", target));
        }
        for entry in self.read_dir() {
            let dir = entry.unwrap();
            let b = dir.path();
            let f_n = b.file_name().unwrap().to_str().unwrap();
            let t_a = format!("{}/{}", target, f_n);
            if self.exclude.contains(&f_n.to_string()) {
                continue
            } else if b.is_dir() {
                let handle_dir_next = DirHandle {
                    dir: b.to_str().unwrap().to_string(),
                    exclude: self.exclude.clone(),
                    f: self.f.clone()
                };
                handle_dir_next.file_move_change(&t_a);
            } else {
                (self.f)(b.to_str().unwrap(), &t_a);
            }
        }
    }
}


pub trait IntoTuple
where
    Self: Sized,
{
    fn tuple(&self) -> (Self,)
    where
        Self: Clone,
    {
        (self.clone(),)
    }
    fn into_tuple<T>(self, other: T) -> (Self, T) {
        (self, other)
    }
}
impl<T: Sized> IntoTuple for T {}

pub trait Print {
    fn print(&self)
    where
        Self: std::fmt::Debug,
    {
        println!("{:?}", self);
    }
    fn println(&self)
    where
        Self: std::fmt::Display,
    {
        println!("{}", self);
    }
    fn print_type(&self)
    where
        Self: Sized,
    {
        type_of(self);
    }
}
impl<T> Print for T {}

pub trait DebugString {
    fn debug_string(&self) -> String;
}
impl<T: std::fmt::Debug> DebugString for T {
    fn debug_string(&self) -> String {
        format!("{:?}", self)
    }
}


pub fn copy_dir_for_axum() {
    DirHandle { 
        dir: "/root/arcta".into(),
        exclude: vec![
            "notebook".into(), "target".into(), "axum".into(),
             "OPT2".into(), "data".into()],
        f: |x: &str, y: &str| { fs::copy(x, y).unwrap(); },
    }
        .file_move_change("/root/arcta/axum/crates")
}

pub trait FileStr: AsRef<Path> {
    fn clear_dir(&self) {
        let path = self.as_ref();
        if !path.is_dir() { return }
        self
            .get_file_vec()
            .unwrap_or_default()
            .iter()
            .for_each(|x| {
                path.join(x).remove();
            })
    }

    fn remove(&self) {
        let path = Path::new(self.as_ref());
        if path.is_dir() {
            self.clear_dir();
            std::fs::remove_dir(self).unwrap();
        } else if path.is_file() {
            std::fs::remove_file(self).unwrap();
        }
    }

    fn build_an_empty_dir(&self) {
        let path = Path::new(self.as_ref());
        if path.is_dir() {
            self.clear_dir();
        } else {
            if path.exists() && path.is_file() {
                std::fs::remove_file(path).unwrap();
            }
            std::fs::create_dir(path).unwrap();
        }
    }

    fn create_a_dir(&self) {
        let path = PathBuf::from(self.as_ref());
        if !path.exists() {
            std::fs::create_dir(path).unwrap();
        } 
    }

    fn get_file_vec(&self) -> io::Result<Vec<String>> {
        self
            .as_ref()
            .read_dir()?
            .map(|x| {
                let path_str = x.unwrap().path();
                path_str.file_name_str().to_string()
            })
            .collect_vec()
            .pip(Ok)
    }

    fn get_file_vec_sort(&self) -> io::Result<Vec<String>> {
        let mut res = self.get_file_vec()?;
        res.sort();
        Ok(res)
    }

    fn get_file_map(&self) -> io::Result<impl Iterator<Item = DirEntry>> {
        self
            .as_ref()
            .read_dir()?
            .map(|x| x.unwrap())
            .pip(Ok)
    }

    fn write_by<T: AsRef<Path> + Debug>(&self, data: T) {
        let mut path = std::fs::File
            ::create(Path::new(self.as_ref())).unwrap();
        write!(path, "{:?}", data).unwrap();
    }

    fn write_to<T: AsRef<Path>>(&self, data: T)
    where
        Self: Debug,
    {
        data.write_by(self.as_ref());
    }

    fn handle_file_recur(&self, f: fn(&Path)) {
        if self.as_ref().is_dir() {
            self
                .get_file_vec()
                .unwrap()
                .iter()
                .for_each(|x| {
                    self.as_ref().join(x).as_path().handle_file_recur(f);
                })
        } else {
            f(self.as_ref());
        }
    }

    fn file_name_str(&self) -> &str {
        self.as_ref().file_name().unwrap().to_str().unwrap()
    }

    fn parent_str(&self) -> &str {
        self.as_ref().parent().unwrap().to_str().unwrap()
    }

    fn split_dir_and_file(&self) -> (&str, &str) {
        (
            self.parent_str(),
            self.file_name_str(),
        )
    }

    fn file_size(&self) -> f64 {
        self.as_ref().metadata().unwrap().len() as f64 / 1024. / 1024.
    }

    fn check_or_make(&self) {
        if !self.as_ref().exists() {
            self.build_an_empty_dir()
        }
    }
}
impl<T: AsRef<Path>> FileStr for T {}


pub trait ToStr {
    fn to_str(&self) -> &str;
}

impl ToStr for [u8] {
    fn to_str(&self) -> &str {
        std::str::from_utf8(self).unwrap()
    }
}

pub trait IoTest {
    fn io_test(&self);
}

impl<T> IoTest for T
where
   for<'de> T: Serialize + Deserialize<'de>,
{
    fn io_test(&self) {
        t!(self.sof("_", "."));
        t!(rof::<T>("_", "."));
        "_".file_size().print();
        "_".remove();
    }
}

use std::path::PathBuf;
pub fn rename_date(p: &PathBuf) -> std::io::Result<()> {
    p
        .get_file_vec()
        .unwrap()
        .into_iter()
        .for_each(|x| {
            let x_new = x.replace('-', "");
            let path_old = p.join(&x);
            if path_old.is_dir() { panic!() }
            let path_new = p.join(x_new);
            std::fs::rename(path_old, path_new).unwrap();
        });
    Ok(())
}


use std::time::SystemTime;
use std::time::UNIX_EPOCH;
use chrono::{DateTime, TimeZone};
pub fn system_time_to_date_time(t: SystemTime) -> DateTime<Local> {
    let (sec, nsec) = match t.duration_since(UNIX_EPOCH) {
        Ok(dur) => (dur.as_secs() as i64, dur.subsec_nanos()),
        Err(e) => { // unlikely but should be handled
            let dur = e.duration();
            let (sec, nsec) = (dur.as_secs() as i64, dur.subsec_nanos());
            if nsec == 0 {
                (-sec, 0)
            } else {
                (-sec - 1, 1_000_000_000 - nsec)
            }
        },
    };
    Local.timestamp_opt(sec, nsec).unwrap()
}

pub struct DateRange {
    start: da,
    end: da,
}

pub struct DateRangeIter {
    end: da,
    last: da,
}

impl DateRange {
    pub fn new(start: da, end: da) -> Self {
        Self { start, end }
    }
}

impl IntoIterator for DateRange {
    type Item = da;
    type IntoIter = DateRangeIter;
    fn into_iter(self) -> Self::IntoIter {
        DateRangeIter {
            end: self.end,
            last: self.start.pred_opt().unwrap(),
        }        
    }
}

impl Iterator for DateRangeIter {
    type Item = da;
    fn next(&mut self) -> Option<Self::Item> {
        if self.last >= self.end {
            return None;
        }
        self.last = self.last.succ_opt()?;
        Some(self.last)
    }
}


pub trait LeakData {
    type Output;
    fn leak_data(&self) -> Self::Output;
}

impl<T: Clone + Hash + std::cmp::Eq> LeakData for std::collections::HashMap<T, String> {
    type Output = std::collections::HashMap<T, &'static str>;
    fn leak_data(&self) -> Self::Output {
        self
            .iter()
            .fold(Self::Output::default(), |mut accu, (k, v)| {
                let v_str: &'static str = Box::leak(v.clone().into_boxed_str());
                accu.insert(k.clone(), v_str);
                accu
            })
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WithInfo<T, N> {
    pub data: T,
    pub info: N,
}

pub trait WithInfoTrait: Sized {
    fn with_info<T>(self, info: T) -> WithInfo<Self, T> {
        WithInfo {
            data: self,
            info,
        }
    }
}
impl<T> WithInfoTrait for T {}


pub trait GenUniqueId {
    fn gen_unique_id(&self, n: usize) -> String 
    where
        Self: std::fmt::Debug,
    {
       use sha2::{Sha256, Digest}; 
       let input = format!("{self:?}");
       let mut hasher = Sha256::new();
        hasher.update(input);
        let result = hasher.finalize();
        hex::encode(result).chars().take(n).collect()
    }
}

impl<T> GenUniqueId for T {}