n4n5/
macros.rs

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
/// automaticallly generate the input path
#[macro_export]
macro_rules! config_path {
    ($config:ident, $setting_name: ident, $struct_name: ident, $key_name: ident, $string: expr) => {
        match &$config.config_data.$setting_name {
            Some($struct_name {
                $key_name: Some(path),
                ..
            }) => PathBuf::from(path),
            _ => {
                println!(concat!(
                    "Please enter the path to the folder where to save ",
                    $string,
                    ":"
                ));
                let file_path = input_path();
                let cloned_path = file_path.1.clone();
                $config.update(|config_data| {
                    if let Some(local_config) = config_data.$setting_name.as_mut() {
                        local_config.$key_name = Some(cloned_path);
                    } else {
                        config_data.$setting_name = Some($struct_name {
                            $key_name: Some(cloned_path),
                            ..Default::default()
                        });
                    }
                    config_data
                });
                file_path.0
            }
        }
    };
}

/// automatically generate the input path
#[macro_export]
macro_rules! config_sub_path {
    ($config:ident, $setting_name: ident, $struct_name: ident, $key_name: ident, $sub_struct_name: ident, $sub_key_name: ident, $string: expr) => {
        match &$config.config_data.$setting_name {
            Some($struct_name {
                $key_name:
                    Some($sub_struct_name {
                        $sub_key_name: Some(path),
                        ..
                    }),
                ..
            }) => PathBuf::from(path),
            _ => {
                println!(concat!(
                    "Please enter the path to the folder where to save ",
                    $string,
                    ":"
                ));
                let file_path = input_path();
                let cloned_path = file_path.1.clone();
                $config.update(|config_data| {
                    match config_data.$setting_name {
                        Some($struct_name {
                            $key_name: Some(ref mut local_config),
                            ..
                        }) => {
                            local_config.$sub_key_name = Some(cloned_path);
                        }
                        _ => {
                            config_data.$setting_name = Some($struct_name {
                                $key_name: Some($sub_struct_name {
                                    $sub_key_name: Some(cloned_path),
                                    ..Default::default()
                                }),
                                ..Default::default()
                            });
                        }
                    }
                    config_data
                });
                file_path.0
            }
        }
    };
}