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
//! If `serde` turns your structs into markup files,
//!
//! then `edres` turns your markup files into structs.
//!
//! This crate has three main related functionalities:
//!
//! 1.  Generate a set of structs representing the contents of
//!     a single markup file.
//! 2.  Generate an enum to represent the keys of a map in a
//!     single markup file (and optionally structs to represent
//!     the values).
//! 3.  Generate an enum to represent the files of a directory
//!     (and optionally structs to represent the contents of those
//!     files).
//!
//! The crate is mainly intended to be used in `build.rs` build
//! scripts, but could also be used inside a proc-macro. (See the
//! [`codegen`] module.)
//!
//! To use this crate, make sure you enable at least one of the
//! serde features to support the kinds of files you're using:
//!
//! 1. `json`
//! 2. `toml`
//! 3. `yaml`
//!
//! There are two sets of functions provided at the top level:
//! the `create_` functions which will write a Rust source file,
//! and the `generate_` functions which simply return Rust code as
//! a string.
//!
//! # Examples
//!
//! ## Generating structs
//!
//! This example takes a single config file and generates a struct
//! that is compatible with it:
//!
//! ```
//! # use edres::*;
//! # use quote::quote;
//! let my_file_content = "
//! title = \"My TOML file\"
//!
//! [my_table]
//! value = 10
//! ";
//!
//! let generated_code = generate_structs_from_source(
//!     my_file_content,
//!     "MyStruct",
//!     Format::Toml,
//!     &Options::minimal(),
//! ).unwrap();
//!
//! assert_eq!(generated_code, quote!(
//!     #[allow(non_camel_case_types)]
//!     pub struct MyStruct {
//!         pub title: std::borrow::Cow<'static, str>,
//!         pub my_table: MyStruct__my_table,
//!     }
//!
//!     #[allow(non_camel_case_types)]
//!     pub struct MyStruct__my_table {
//!         pub value: i64,
//!     }
//! ).to_string());
//! ```
//!
//! ## Generating enums
//!
//! This example takes a single config file and generates an enum
//! for the keys of the file, and structs for the values:
//!
//! ```
//! # use edres::*;
//! # use quote::quote;
//! let my_file_content = "
//!     Key1:
//!         name: First key
//!     Key2:
//!         name: Second key
//! ";
//!
//! let generated_code = generate_enum_from_source(
//!     my_file_content,
//!     "MyEnum",
//!     Format::Yaml,
//!     &Options {
//!         enums: EnumOptions {
//!             values_struct: Some(ValuesStructOptions::minimal()),
//!             ..EnumOptions::minimal()
//!         },
//!         ..Options::minimal()
//!     },
//! ).unwrap();
//!
//! assert_eq!(generated_code, quote!(
//!     pub enum MyEnum {
//!         Key1,
//!         Key2,
//!     }
//!
//!     #[allow(non_camel_case_types)]
//!     pub struct MyEnum__Value {
//!         pub name: std::borrow::Cow<'static, str>,
//!     }
//! ).to_string());
//! ```
//!
//! ## Generating file enums
//!
//! This example a directory full of JSON files and generates an
//! enum variant for each file name, and a struct to represent the
//! contents of them all.
//!
//! ```no_run
//! # use edres::*;
//! # use quote::quote;
//! /* Example JSON file:
//! {
//!     "name": "file1.json",
//!     "number": 1
//! }
//! */
//!
//! let generated_code = generate_enum_from_filenames(
//!     "./dir_full_of_json_files",
//!     "MyFileEnum",
//!     &Options {
//!         enums: EnumOptions {
//!             values_struct: Some(ValuesStructOptions::minimal()),
//!             ..EnumOptions::minimal()
//!         },
//!         ..Options::minimal()
//!     },
//! ).unwrap();
//!
//! assert_eq!(generated_code, quote!(
//!     pub enum MyFileEnum {
//!         File1,
//!         File2,
//!         File3,
//!     }
//!
//!     #[allow(non_camel_case_types)]
//!     pub struct MyFileEnum__Value {
//!         pub name: std::borrow::Cow<'static, str>,
//!         pub number: i32,
//!     }
//! ).to_string());
//! ```

mod files;

#[cfg(not(any(feature = "json", feature = "toml", feature = "yaml",)))]
compile_error!(
    "The edres crate requires at least one parsing feature to be enabled:\n {json, toml, yaml}"
);

use std::path::Path;

#[cfg(feature = "proc-macros")]
pub use edres_macros::{
    define_enums, define_enums_from_dirs, define_structs, define_structs_from_dirs,
};

pub use edres_core::*;

/// Generate Rust code that defines a set of structs based on a
/// given markup file.
pub fn generate_structs<SrcPath: AsRef<Path>, Name: AsRef<str>>(
    src_path: SrcPath,
    struct_name: Name,
    options: &Options,
) -> Result<String, Error> {
    let path = src_path.as_ref();
    let value = parsing::parse_source_file(path, &options.parse)?.assume_struct()?;
    let tokens = codegen::define_structs(&value, struct_name.as_ref(), Some(path), options)?;
    Ok(tokens.to_string())
}

/// Generate Rust code that defines a set of structs based on the
/// given markup source.
pub fn generate_structs_from_source<Source: AsRef<str>, Name: AsRef<str>>(
    source: Source,
    struct_name: Name,
    format: Format,
    options: &Options,
) -> Result<String, Error> {
    let value = parsing::parse_source(source.as_ref(), format, &options.parse)?.assume_struct()?;
    let tokens = codegen::define_structs(&value, struct_name.as_ref(), None, options)?;
    Ok(tokens.to_string())
}

/// Generate Rust code that defines a set of structs based on the
/// contents of the files in the given directory.
pub fn generate_structs_from_files<DirPath: AsRef<Path>, Name: AsRef<str>>(
    dir_path: DirPath,
    struct_name: Name,
    options: &Options,
) -> Result<String, Error> {
    let tokens = codegen::define_structs_from_file_contents(
        dir_path.as_ref(),
        struct_name.as_ref(),
        None,
        options,
    )?;
    Ok(tokens.to_string())
}

/// Generate Rust code that defines an enum based on the map keys
/// of the given markup file.
pub fn generate_enum<SrcPath: AsRef<Path>, Name: AsRef<str>>(
    src_path: SrcPath,
    enum_name: Name,
    options: &Options,
) -> Result<String, Error> {
    let path = src_path.as_ref();
    let value = parsing::parse_source_file(path, &options.parse)?.assume_struct()?;
    let tokens = codegen::define_enum_from_keys(&value, enum_name.as_ref(), Some(path), options)?;
    Ok(tokens.to_string())
}

/// Generate Rust code that defines an enum based on the map keys
/// of the given markup source.
pub fn generate_enum_from_source<Source: AsRef<str>, Name: AsRef<str>>(
    source: Source,
    enum_name: Name,
    format: Format,
    options: &Options,
) -> Result<String, Error> {
    let value = parsing::parse_source(source.as_ref(), format, &options.parse)?.assume_struct()?;
    let tokens = codegen::define_enum_from_keys(&value, enum_name.as_ref(), None, options)?;
    Ok(tokens.to_string())
}

/// Generate Rust code that defines an enum based on the file names
/// within the given directory.
pub fn generate_enum_from_filenames<DirPath: AsRef<Path>, Name: AsRef<str>>(
    dir_path: DirPath,
    enum_name: Name,
    options: &Options,
) -> Result<String, Error> {
    let tokens =
        codegen::define_enum_from_filenames(dir_path.as_ref(), enum_name.as_ref(), options)?;
    Ok(tokens.to_string())
}

/// Create a Rust source file that defines a set of structs
/// based on a given markup file.
pub fn create_structs<SrcPath: AsRef<Path>, DestPath: AsRef<Path>, Name: AsRef<str>>(
    src_path: SrcPath,
    dest_path: DestPath,
    struct_name: Name,
    options: &Options,
) -> Result<(), Error> {
    let output = generate_structs(src_path, struct_name, options)?;
    files::ensure_destination(dest_path.as_ref(), options.output.create_dirs)?;
    files::write_destination(
        dest_path.as_ref(),
        output,
        options.output.write_only_if_changed,
    )?;

    Ok(())
}

/// Create a Rust source file that defines a set of structs based
/// on the given markup source.
pub fn create_structs_from_source<Source: AsRef<str>, DestPath: AsRef<Path>, Name: AsRef<str>>(
    source: Source,
    dest_path: DestPath,
    struct_name: Name,
    format: Format,
    options: &Options,
) -> Result<(), Error> {
    let output = generate_structs_from_source(source, struct_name, format, options)?;
    files::ensure_destination(dest_path.as_ref(), options.output.create_dirs)?;
    files::write_destination(
        dest_path.as_ref(),
        output,
        options.output.write_only_if_changed,
    )?;

    Ok(())
}

/// Create a Rust source file that defines a set of structs based
/// on the contents of the files in the given directory.
pub fn create_structs_from_files<DirPath: AsRef<Path>, DestPath: AsRef<Path>, Name: AsRef<str>>(
    dir_path: DirPath,
    dest_path: DestPath,
    struct_name: Name,
    options: &Options,
) -> Result<(), Error> {
    let output = generate_structs_from_files(dir_path, struct_name, options)?;
    files::ensure_destination(dest_path.as_ref(), options.output.create_dirs)?;
    files::write_destination(
        dest_path.as_ref(),
        output,
        options.output.write_only_if_changed,
    )?;

    Ok(())
}

/// Create a Rust source file that defines an enum based on the
/// map keys of the given markup file.
pub fn create_enum<SrcPath: AsRef<Path>, DestPath: AsRef<Path>, Name: AsRef<str>>(
    src_path: SrcPath,
    dest_path: DestPath,
    enum_name: Name,
    options: &Options,
) -> Result<(), Error> {
    let output = generate_enum(src_path, enum_name, options)?;
    files::ensure_destination(dest_path.as_ref(), options.output.create_dirs)?;
    files::write_destination(
        dest_path.as_ref(),
        output,
        options.output.write_only_if_changed,
    )?;

    Ok(())
}

/// Create a Rust source file that defines an enum based on the
/// map keys of the given markup source.
pub fn create_enum_from_source<Source: AsRef<str>, DestPath: AsRef<Path>, Name: AsRef<str>>(
    source: Source,
    dest_path: DestPath,
    enum_name: Name,
    format: Format,
    options: &Options,
) -> Result<(), Error> {
    let output = generate_enum_from_source(source, enum_name, format, options)?;
    files::ensure_destination(dest_path.as_ref(), options.output.create_dirs)?;
    files::write_destination(
        dest_path.as_ref(),
        output,
        options.output.write_only_if_changed,
    )?;

    Ok(())
}

/// Create a Rust source file that defines an enum based on the
/// file names within the given directory.
pub fn create_enum_from_filenames<DirPath: AsRef<Path>, DestPath: AsRef<Path>, Name: AsRef<str>>(
    dir_path: DirPath,
    dest_path: DestPath,
    enum_name: Name,
    options: &Options,
) -> Result<(), Error> {
    let output = generate_enum_from_filenames(dir_path, enum_name, options)?;
    files::ensure_destination(dest_path.as_ref(), options.output.create_dirs)?;
    files::write_destination(
        dest_path.as_ref(),
        output,
        options.output.write_only_if_changed,
    )?;

    Ok(())
}