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
pub use struct_to_json_db_macro::auto_json_db; 
use std::fs;
use std::io::Write; 
use rand::Rng;
pub use paste::paste;

pub fn read_string_from_txt(filename: &str) -> String {
    let file_contents = fs::read_to_string(filename).unwrap_or_default();
    file_contents
}
pub fn write_string_to_txt(filename: &str, content: String) {
    let mut file = fs::OpenOptions::new()
        .write(true)
        .create(true)
        .truncate(true)
        .open(filename)
        .unwrap();
    file.write_all(content.as_bytes()).unwrap();
}
pub fn unique_id() -> (u64,u64) {
    let start = std::time::SystemTime::now();
    let timestamp = start.duration_since(std::time::UNIX_EPOCH).unwrap().as_nanos() as u64 ;
    let random_number = rand::thread_rng().gen::<u64>();
    (random_number,timestamp)
}

#[macro_export]
macro_rules! json_db_get_by {
    ($first:ident, $second:ident:$second_type:expr) => {
        struct_to_json_db::paste! {
        |all_data:&HashMap<u64, [<$first>]>,byval:[<$second_type>]|->Vec<(u64,[<$first>])>{
             all_data.iter().filter_map(|(id, obj)| {
                if obj.[<$second>] == byval {
                    Some((id.clone(), obj.clone()))
                } else {
                    None
                }
                }).collect()      
        }
             
        }
    };
}

#[macro_export]
macro_rules! auto_json_db_config {
    ($path_str:expr) => {
        use struct_to_json_db::*;
        use serde::{Deserialize, Serialize};
        static DB_STRUCT_JSON_PATH:&str = $path_str;
    };
}
#[macro_export]
macro_rules! json_db_one_many {
    ($first:ident, $second:ident) => {
        struct_to_json_db::paste! {
            #[derive(Serialize,Deserialize,Clone,Debug)]
            pub struct [<$first $second OneMany>] {
                pub idx:u64,
                pub data:Vec<[<$second>]>
            }
            impl [<$first $second OneMany>] {
                pub fn new(idx: u64, data: Vec<[<$second>]>) -> Self {
                    Self { idx, data }
                }
                pub fn add(&mut self, new_data: Vec<[<$second>]>) {
                    self.data.extend(new_data);
                }
                pub fn get_path()->String{
                    format!("{}/one_many_{}_{}.json", DB_STRUCT_JSON_PATH, stringify!($first), stringify!($second))
                }
                pub fn get_all()->std::collections::HashMap<u64,Self>{
                    let file_path = Self::get_path();
                    let db_string = read_string_from_txt(&file_path);
                    serde_json::from_str(&db_string).unwrap_or_default() 
                }
                pub fn save(&self){ 
                    let db = Self::get_all();
                    db.insert(self.idx, self.clone());
                    Self::save_all(&db);
                }
                pub fn save_all(db:&std::collections::HashMap<u64,Self>){
                    let file_path = Self::get_path();
                    let db_string = serde_json::to_string(db).unwrap();
                    struct_to_json_db::write_string_to_txt(&file_path, db_string);
                }
                pub fn remove(&self){
                    Self::remove_by_id(self.idx);
                }
                pub fn remove_by_id(id: u64){ 
                    let mut db = Self::get_all(); 
                    db.remove(&id);
                    Self::save_all(&db); 
                }
                pub fn remove_by_ids(ids: &Vec<u64>){
                    let mut db = Self::get_all(); 
                    for id in ids{
                        db.remove(&id);
                    }
                    Self::save_all(&db); 
                }
                pub fn clear(){
                    let file_path = Self::get_path();
                    struct_to_json_db::write_string_to_txt(&file_path, "".to_owned());
                }
            }
        }
    };
}

#[macro_export]
macro_rules! json_db_many_many {
    ($first:ident, $second:ident) => {
        struct_to_json_db::paste! {
            #[auto_json_db]
            pub struct [<$first $second Relation>] {
                pub [<$first _ id>]:u64,
                pub [<$second _ id>]:u64,
                pub weight:f32,
                pub name:String
            }
            
        }
    };
}
#[macro_export]
macro_rules! json_db_many_many_add {
    ($first:ident=$first_val:literal, $second:ident=$second_val:literal) => {
        struct_to_json_db::paste! {
            [<$first $second Relation>]::new($first_val,$second_val, 0.0, "".to_string())
        }
    };
    ($first:ident=$first_val:expr, $second:ident=$second_val:expr, weight=$weight_val:expr) => {
        struct_to_json_db::paste! {
            [<$first $second Relation>]::new($first_val,$second_val,  $weight_val , "".to_string() ) 
        }
    };
    ($first:ident=$first_val:expr, $second:ident=$second_val:expr, name=$name_val:expr) => {
        struct_to_json_db::paste! {
            [<$first $second Relation>]::new($first_val,$second_val, 0.0, $name_val ) 
        }
    };
    ($first:ident=$first_val:expr, $second:ident=$second_val:expr, name=$name_val:expr,weight=$weight_val:expr) => {
        struct_to_json_db::paste! {
            [<$first $second Relation>]::new($first_val,$second_val, $weight_val, $name_val ) 
        }
    };
}
#[macro_export]
macro_rules! json_db_many_many_get {
    ($first:ident=$first_val:literal, $second:ident) => {
        struct_to_json_db::paste! {
            {
                let all_relation = [<$first $second Relation>]::get_all();
                let ret:Vec<(u64,[<$first $second Relation>])> = all_relation.into_iter().filter(|x| x.1.[<$first _ id>] == $first_val).collect(); 
                ret
            }
        }
    };
    ($first:ident, $second:ident=$second_val:literal) => {
        struct_to_json_db::paste! {
            {
                let all_relation = [<$first $second Relation>]::get_all();
                let ret:Vec<(u64,[<$first $second Relation>])> = all_relation.into_iter().filter(|x| x.1.[<$second _ id>] == $second_val).collect();
                ret
            
            }
        }
    };     
}