fauxrest 0.0.4

A CLI tool for generating static JSON APIs from local data files
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
//! `fauxrest` command-line entry point.
//!
//! Parses CLI arguments into [`Args`], resolves the effective [`Config`]
//! (explicit `--config`, a discovered `_config.json`, CLI flags, or the
//! built-in default), and runs the build via [`fauxrest::run`].

use clap::{Parser, ValueEnum};
use fauxrest::{Config, Error, Layout, Result};
use std::path::{Path, PathBuf};

/// Command-line arguments for the `fauxrest` binary.
#[derive(Parser, Debug)]
#[command(name = "fauxrest", version, about, long_about = None)]
pub struct Args {
    /// Path to the input data directory.
    #[arg(
        help = "Path to the input data directory",
        value_name = "DATA_DIR",
        default_value = "data"
    )]
    inputs: String,

    /// Specify the log level.
    #[clap(short = 'L', long, help = "Specify the log level", value_enum, default_value_t = LogLevel::Warn, value_name = "LEVEL")]
    level: LogLevel,

    /// Path to the configuration file. When unset, [`Args::load_config`]
    /// falls back to discovering one in the input data directory.
    #[clap(
        short,
        long,
        help = "Path to the configuration file",
        value_name = "CONFIG_FILE"
    )]
    config: Option<PathBuf>,

    /// Delivery layout to use for the output, when building the
    /// configuration from CLI flags instead of a config file.
    #[clap(
        short,
        long,
        help = "Layout to use for the output",
        value_name = "LAYOUT"
    )]
    layout: Option<Layout>,

    /// Path to the output directory.
    #[clap(
        short,
        long,
        help = "Path to the output directory [default: dist]",
        value_name = "DEST_DIR"
    )]
    dest: Option<PathBuf>,

    /// Serializer to use for the output (`json`, `typescript`, or `sql`).
    /// Defaults to `json` when neither this flag nor a config file sets one.
    #[clap(
        short,
        long,
        help = "Serializer to use for the output. [available: json, typescript, sql] [default: json]",
        value_name = "SERIALIZER"
    )]
    serializer: Option<String>,

    /// If `true`, minify the serialized output.
    #[clap(long, default_value_t = false, help = "If true, minify the output")]
    minify: bool,

    /// If set, disable minification, overriding any `minify: true` in the
    /// loaded configuration. Conflicts with `--minify`.
    #[clap(
        long,
        default_value_t = false,
        conflicts_with = "minify",
        help = "If set, disable minification (overrides config)"
    )]
    no_minify: bool,

    /// If `true`, allow writing into a non-empty destination directory
    /// (overwriting existing files).
    #[clap(
        long,
        default_value_t = false,
        help = "If true, overwrite existing files in the destination directory"
    )]
    overwrite: bool,

    /// If `true`, copy all non-JSON static files from the data directory
    /// into each destination (allow all); `$static` exclude globs still
    /// take precedence.
    #[clap(
        long,
        default_value_t = false,
        help = "Copy all non-JSON static files from the data directory into each destination (allow all). $static exclude globs still take precedence."
    )]
    copy_static: bool,

    /// If `true` (debug builds only), generate shell completion files
    /// instead of running the build.
    #[cfg(debug_assertions)]
    #[clap(long, default_value_t = false, help = "Generate completion files")]
    gencomp: bool,
}

/// Verbosity level for CLI logging output.
#[derive(Parser, ValueEnum, Debug, Clone, PartialEq, Eq)]
pub enum LogLevel {
    /// Only report errors.
    Error,
    /// Report warnings and errors (the default).
    Warn,
    /// Report informational messages, warnings, and errors.
    Info,
    /// Report debug-level details in addition to `Info`.
    Debug,
    /// Report the most verbose, low-level tracing output.
    Trace,
}

impl Args {
    /// Loads the configuration based on the command line options.
    ///
    /// Resolution order:
    /// 1. If `--config` is set, load that file.
    /// 2. Otherwise, try to [`discover`](Self::discover) a config file in
    ///    the input data directory.
    /// 3. Otherwise, fall back to [`Config::default`].
    ///
    /// Options that the user explicitly passes on the command line always take
    /// precedence over the loaded configuration file, which in turn takes
    /// precedence over the built-in defaults (CLI > config > default). When the
    /// loaded configuration provides serializers, every explicitly given CLI
    /// option (`--dest`, `--serializer`, `--layout`, `--minify`, `--no-minify`,
    /// `--overwrite`) overrides the matching field of each serializer entry
    /// (see [`Args::apply_cli_overrides`]); when it provides none, a single
    /// entry is synthesized from the CLI flags (see
    /// [`Args::serializer_config`]), keeping the loaded routing overlay (if
    /// any). `--copy-static` additionally enables copying of all non-JSON
    /// static files.
    pub(crate) fn load_config(&self) -> Result<Config> {
        let config = if let Some(config) = &self.config {
            fauxrest::Config::load_from_file(config)
        } else if let Some(discovered_path) = Self::discover(Path::new(&self.inputs)) {
            fauxrest::Config::load_from_file(&discovered_path)
        } else {
            Ok(fauxrest::Config::default())
        };
        match config {
            Ok(mut config) => {
                if config.serializers.is_empty() {
                    config.serializers = vec![self.serializer_config()];
                } else {
                    self.apply_cli_overrides(&mut config);
                }
                // `--copy-static` forces every non-JSON static file to be
                // treated as allowed. Any `$static` exclude globs from the
                // configuration file still take precedence (deny wins).
                if self.copy_static {
                    config.copy_static_all = true;
                }
                Ok(config)
            }
            Err(e) => Err(e),
        }
    }

    /// Applies the options that were explicitly given on the command line onto
    /// every serializer entry of the loaded configuration. Options that were not
    /// given keep the value coming from the configuration file.
    fn apply_cli_overrides(&self, config: &mut Config) {
        for serializer in config.serializers.iter_mut() {
            if let Some(dest) = &self.dest {
                serializer.dest = dest.clone();
            }
            if let Some(s) = &self.serializer {
                serializer.serializer = s.clone();
            }
            if let Some(layout) = &self.layout {
                serializer.layout = layout.clone();
            }
            if self.minify {
                serializer.minify = true;
            } else if self.no_minify {
                serializer.minify = false;
            }
            // When `--overwrite` is passed on the command line it forces
            // every serializer to allow overwriting, regardless of the
            // value present in the loaded configuration file.
            if self.overwrite {
                serializer.overwrite = true;
            }
        }
    }

    /// Builds a single [`fauxrest::SerializerConfig`] from the command line
    /// options (`--serializer`/`--layout`/`--dest`/`--minify`/`--overwrite`),
    /// falling back to the built-in defaults (`json`, [`Layout::Index`],
    /// `dist`) for any option that was not given.
    fn serializer_config(&self) -> fauxrest::SerializerConfig {
        fauxrest::SerializerConfig {
            layout: self.layout.clone().unwrap_or(Layout::Index),
            serializer: self
                .serializer
                .clone()
                .unwrap_or_else(|| String::from("json")),
            dest: self.dest.clone().unwrap_or_else(|| PathBuf::from("dist")),
            minify: self.minify,
            overwrite: self.overwrite,
        }
    }

    /// Discovers and loads a configuration file from a directory.
    /// It searches for '_config.json', '_fauxrest.json', '.config.json', and '.fauxrest.json' in order.
    fn discover(dir: &Path) -> Option<PathBuf> {
        let configs = [
            "_config.json",
            "_fauxrest.json",
            ".config.json",
            ".fauxrest.json",
        ];
        configs
            .iter()
            .map(|c| dir.join(c))
            .find(|path| path.exists())
    }
}

/// Shell completion file generation (debug builds only); see
/// [`gencomp::_generate`].
mod gencomp;

/// Runs the requested action for parsed CLI `args`: in debug builds, generates
/// shell completion files if `--gencomp` was passed; otherwise resolves the
/// configuration and runs the static API build via [`fauxrest::run`].
fn perform_build(args: Args) -> Result<()> {
    #[cfg(debug_assertions)]
    if args.gencomp {
        gencomp::_generate("assets/completions");
        return Ok(());
    }

    let config = args.load_config()?;
    fauxrest::run(config, PathBuf::from(args.inputs))
}

/// Binary entry point: parses CLI arguments, runs [`perform_build`], and on
/// error prints the error to stderr and exits the process with status 1.
fn main() -> Result<()> {
    let r = match Args::try_parse() {
        Ok(args) => perform_build(args),
        Err(e) => Err(Error::Clap(e)),
    };
    if let Err(e) = r {
        eprint!("{}", e);
        std::process::exit(1);
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use fauxrest::Layout;
    use std::io::Write;

    /// Writes a config file with a single `$config` serializer entry into a
    /// fresh temp directory and returns the directory handle and the config path.
    fn write_config(body: &str) -> (tempfile::TempDir, PathBuf) {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("config.json");
        let mut file = std::fs::File::create(&path).unwrap();
        file.write_all(body.as_bytes()).unwrap();
        (dir, path)
    }

    const CONFIG_BODY: &str = r#"{"$config": [{"serializer": "typescript", "layout": "file", "dest": "from-config", "minify": true}]}"#;

    /// Without CLI overrides, every serializer field comes from the config file.
    #[test]
    fn config_values_used_when_no_cli_override() {
        let (_dir, path) = write_config(CONFIG_BODY);
        let args =
            Args::try_parse_from(["fauxrest", "data", "-c", path.to_str().unwrap()]).unwrap();
        let config = args.load_config().unwrap();
        assert_eq!(config.serializers.len(), 1);
        let s = &config.serializers[0];
        assert_eq!(s.serializer, "typescript");
        assert_eq!(s.dest, PathBuf::from("from-config"));
        assert!(matches!(s.layout, Layout::File));
        assert!(s.minify);
    }

    /// An explicit `--dest` overrides the config file; other fields are kept.
    #[test]
    fn explicit_dest_overrides_config() {
        let (_dir, path) = write_config(CONFIG_BODY);
        let args = Args::try_parse_from([
            "fauxrest",
            "data",
            "-c",
            path.to_str().unwrap(),
            "-d",
            "cli-dest",
        ])
        .unwrap();
        let config = args.load_config().unwrap();
        let s = &config.serializers[0];
        // dest comes from the CLI, the rest is still from the config.
        assert_eq!(s.dest, PathBuf::from("cli-dest"));
        assert_eq!(s.serializer, "typescript");
        assert!(matches!(s.layout, Layout::File));
        assert!(s.minify);
    }

    /// Explicit `--serializer`/`--layout` override the config file; `dest` is kept.
    #[test]
    fn explicit_serializer_and_layout_override_config() {
        let (_dir, path) = write_config(CONFIG_BODY);
        let args = Args::try_parse_from([
            "fauxrest",
            "data",
            "-c",
            path.to_str().unwrap(),
            "-s",
            "json",
            "-l",
            "index",
        ])
        .unwrap();
        let config = args.load_config().unwrap();
        let s = &config.serializers[0];
        assert_eq!(s.serializer, "json");
        assert!(matches!(s.layout, Layout::Index));
        // dest is untouched by the CLI here.
        assert_eq!(s.dest, PathBuf::from("from-config"));
    }

    /// `--minify` turns minification on even when the config file leaves it off.
    #[test]
    fn minify_flag_overrides_config() {
        // config has minify=false, CLI passes --minify.
        let (_dir, path) = write_config(
            r#"{"$config": [{"serializer": "json", "layout": "index", "dest": "from-config"}]}"#,
        );
        let args =
            Args::try_parse_from(["fauxrest", "data", "-c", path.to_str().unwrap(), "--minify"])
                .unwrap();
        let config = args.load_config().unwrap();
        assert!(config.serializers[0].minify);
    }

    /// `--no-minify` turns minification off even when the config file enables it.
    #[test]
    fn no_minify_flag_overrides_config() {
        // config has minify=true, CLI passes --no-minify.
        let (_dir, path) = write_config(CONFIG_BODY);
        let args = Args::try_parse_from([
            "fauxrest",
            "data",
            "-c",
            path.to_str().unwrap(),
            "--no-minify",
        ])
        .unwrap();
        let config = args.load_config().unwrap();
        assert!(!config.serializers[0].minify);
    }

    /// `--minify` and `--no-minify` are mutually exclusive at parse time.
    #[test]
    fn minify_and_no_minify_conflict() {
        let result = Args::try_parse_from(["fauxrest", "data", "--minify", "--no-minify"]);
        assert!(result.is_err());
    }

    /// Without a config file, `--no-minify` keeps the default (unminified).
    #[test]
    fn no_config_with_no_minify_stays_unminified() {
        // Without a config file, --no-minify keeps the default (false).
        let args = Args::try_parse_from(["fauxrest", "no-such-dir", "--no-minify"]).unwrap();
        let config = args.load_config().unwrap();
        assert!(!config.serializers[0].minify);
    }

    /// CLI overrides are applied to every `$config` serializer entry, not just the first.
    #[test]
    fn overrides_apply_to_every_serializer_entry() {
        let (_dir, path) = write_config(
            r#"{"$config": [
                {"serializer": "json", "layout": "index", "dest": "a"},
                {"serializer": "sqlite", "layout": "file", "dest": "b"}
            ]}"#,
        );
        let args = Args::try_parse_from([
            "fauxrest",
            "data",
            "-c",
            path.to_str().unwrap(),
            "-d",
            "cli-dest",
        ])
        .unwrap();
        let config = args.load_config().unwrap();
        assert_eq!(config.serializers.len(), 2);
        for s in &config.serializers {
            assert_eq!(s.dest, PathBuf::from("cli-dest"));
        }
    }

    /// Without a config file or CLI options, the built-in defaults are used.
    #[test]
    fn no_config_uses_defaults_without_cli_options() {
        // "no-such-dir" has no discoverable config, so the default config is used.
        let args = Args::try_parse_from(["fauxrest", "no-such-dir"]).unwrap();
        let config = args.load_config().unwrap();
        let s = &config.serializers[0];
        assert_eq!(s.serializer, "json");
        assert_eq!(s.dest, PathBuf::from("dist"));
        assert!(matches!(s.layout, Layout::Index));
        assert!(!s.minify);
    }

    /// Without a config file, explicitly given CLI options are still honored.
    #[test]
    fn no_config_honors_explicit_cli_options() {
        // Even without a config file, an explicit --dest must be honored.
        let args =
            Args::try_parse_from(["fauxrest", "no-such-dir", "-d", "cli-dest", "-s", "sqlite"])
                .unwrap();
        let config = args.load_config().unwrap();
        let s = &config.serializers[0];
        assert_eq!(s.dest, PathBuf::from("cli-dest"));
        assert_eq!(s.serializer, "sqlite");
    }
}