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
#[macro_export]
/// A macro that generates a file based on a provided value, a generator function, and an optional custom serializer.
///
/// The macro takes four parameters: `$file_type:expr`, `$value:expr`, `$generator:expr`, and an optional `$serializer:expr`.
///
/// - `$file_type:expr`: A string literal representing the type of the file to be generated (e.g., "yaml", "json", "txt").
/// - `$value:expr`: A reference to a value.
/// - `$generator:expr`: A closure that takes a string slice (the serialized content) and generates the file.
/// - `$serializer:expr`: An optional custom serializer function that takes a reference to the value and returns a `Result<String, String>`.
///
/// The macro attempts to generate the file using the provided `$generator` function. If an error occurs during the generation process, it prints an error message to the standard error stream.
///
/// # Examples
///
/// ## YAML Example
/// ```rust
/// use std::fs;
/// use serde::Serialize;
/// use serde_yml::to_string as to_yaml_string;
/// use serde_yml::generate_file;
///
/// #[derive(Serialize, Debug)]
/// struct MyData {
///     key: String,
///     value: String,
///     nested: NestedData,
///     items: Vec<Item>,
/// }
///
/// #[derive(Serialize, Debug)]
/// struct NestedData {
///     id: u32,
///     description: String,
/// }
///
/// #[derive(Serialize, Debug)]
/// struct Item {
///     name: String,
///     quantity: u32,
///     price: f64,
/// }
///
/// let value = MyData {
///     key: "example".to_string(),
///     value: "Hello, Serde YML!".to_string(),
///     nested: NestedData {
///         id: 1,
///         description: "This is a nested structure".to_string(),
///     },
///     items: vec![
///         Item {
///             name: "Item1".to_string(),
///             quantity: 10,
///             price: 99.99,
///         },
///         Item {
///             name: "Item2".to_string(),
///             quantity: 5,
///             price: 9.99,
///         },
///     ],
/// };
///
/// generate_file!("yaml", &value, |content| {
///     fs::write("output.yaml", content)
/// });
/// fs::remove_file("output.yaml").unwrap();
/// ```
///
/// ## JSON Example
/// ```rust
/// use std::fs;
/// use serde::Serialize;
/// use serde_json::to_string as to_json_string;
/// use serde_yml::generate_file;
///
/// #[derive(Serialize, Debug)]
/// struct MyData {
///     key: String,
///     value: String,
///     nested: NestedData,
///     items: Vec<Item>,
/// }
///
/// #[derive(Serialize, Debug)]
/// struct NestedData {
///     id: u32,
///     description: String,
/// }
///
/// #[derive(Serialize, Debug)]
/// struct Item {
///     name: String,
///     quantity: u32,
///     price: f64,
/// }
///
/// let value = MyData {
///     key: "example".to_string(),
///     value: "Hello, Serde JSON!".to_string(),
///     nested: NestedData {
///         id: 1,
///         description: "This is a nested structure".to_string(),
///     },
///     items: vec![
///         Item {
///             name: "Item1".to_string(),
///             quantity: 10,
///             price: 99.99,
///         },
///         Item {
///             name: "Item2".to_string(),
///             quantity: 5,
///             price: 9.99,
///         },
///     ],
/// };
///
/// generate_file!("json", &value, |content| {
///     fs::write("output.json", content)
/// });
/// fs::remove_file("output.json").unwrap();
/// ```
///
/// ## TXT Example
/// ```rust
/// use std::fs;
/// use serde::Serialize;
/// use serde_yml::generate_file;
///
/// #[derive(Serialize, Debug)]
/// struct MyData {
///     key: String,
///     value: String,
///     nested: NestedData,
///     items: Vec<Item>,
/// }
///
/// #[derive(Serialize, Debug)]
/// struct NestedData {
///     id: u32,
///     description: String,
/// }
///
/// #[derive(Serialize, Debug)]
/// struct Item {
///     name: String,
///     quantity: u32,
///     price: f64,
/// }
///
/// let value = MyData {
///     key: "example".to_string(),
///     value: "Hello, Serde TXT!".to_string(),
///     nested: NestedData {
///         id: 1,
///         description: "This is a nested structure".to_string(),
///     },
///     items: vec![
///         Item {
///             name: "Item1".to_string(),
///             quantity: 10,
///             price: 99.99,
///         },
///         Item {
///             name: "Item2".to_string(),
///             quantity: 5,
///             price: 9.99,
///         },
///     ],
/// };
///
/// generate_file!("txt", &value, |content| {
///     let txt_string = format!("{:?}", content);
///     fs::write("output.txt", txt_string)
/// });
/// fs::remove_file("output.txt").unwrap();
/// ```
///
macro_rules! generate_file {
    ($file_type:expr, $value:expr, $generator:expr, $serializer:expr) => {
        let result = $serializer($value);
        if let Ok(content) = result {
            if let Err(err) = $generator(&content) {
                eprintln!(
                    "Error generating {} file: {}",
                    $file_type, err
                );
            }
        } else {
            eprintln!(
                "Error serializing value to {}: {}",
                $file_type,
                result.unwrap_err()
            );
        }
    };
    ($file_type:expr, $value:expr, $generator:expr) => {
        generate_file!($file_type, $value, $generator, |value| {
            match $file_type {
                "yaml" => serde_yml::to_string(value)
                    .map_err(|e| e.to_string()),
                "json" => serde_json::to_string(value)
                    .map_err(|e| e.to_string()),
                "txt" => Ok(format!("{:?}", value)),
                _ => Err("Unsupported file type".to_string()),
            }
        });
    };
}