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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
//! This is the codegen part of `configure_me` crate. Please refer to the documentation of
//! `configure_me` for details.
//!
//! ## Unstable metabuild feature
//!
//! This crate supports nightly-only `metabuild` feature tracked in https://github.com/rust-lang/rust/issues/49803
//! Since it is unstable you have to opt-in to instability by activating `unstable-metabuild` Cargo
//! feature. If you enable it you don't have to write build script anymore, just add
//! `metabuild = ["configure_me_codegen"]` to `[package]` section of your `Cargo.toml`. Note that
//! you still have to specify the dependency (with the feature) in `[build-dependencies]`.
//!
//! No guarantees about stability are made because of the nature of nightly. Please use this only
//! to test `metabuild` feture of Cargo and report your experience to the tracking issue. I look
//! forward into having this stable and the main way of using this crate. Your reports will help.

extern crate serde;
#[macro_use]
extern crate serde_derive;
extern crate toml;
extern crate unicode_segmentation;
extern crate fmt2io;
extern crate cargo_toml;
#[cfg(feature = "man")]
extern crate man;

pub(crate) mod config;
pub(crate) mod codegen;
#[cfg(feature = "man")]
pub (crate) mod gen_man;
#[cfg(feature = "debconf")]
pub (crate) mod debconf;

pub mod manifest;

use std::borrow::Borrow;
use std::fmt;
use std::io::{self, Read, Write};
use std::path::{Path, PathBuf};
use manifest::LoadManifest;

#[derive(Debug)]
enum ErrorData {
    Toml(toml::de::Error),
    Config(config::ValidationError),
    Io(io::Error),
    Open { file: PathBuf, error: io::Error },
    Manifest(manifest::Error),
    MissingManifestDirEnvVar,
    MissingOutDir,
    #[cfg(feature = "debconf")]
    Debconf(debconf::Error),
}

/// Error that occured during code generation
pub struct Error {
    data: ErrorData,
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match &self.data {
            ErrorData::Toml(err) => write!(f, "failed to parse config specification: {}", err),
            ErrorData::Manifest(error) => write!(f, "failed to process manifest: {}", error),
            ErrorData::Config(err) => fmt::Display::fmt(err, f),
            ErrorData::Io(err) => write!(f, "I/O error: {}", err),
            ErrorData::Open { file, error } => write!(f, "failed to open file {}: {}", file.display(), error),
            ErrorData::MissingManifestDirEnvVar => write!(f, "missing environment variable: CARGO_MANIFEST_DIR"),
            ErrorData::MissingOutDir => write!(f, "missing environment variable: OUT_DIR"),
            #[cfg(feature = "debconf")]
            ErrorData::Debconf(err) => write!(f, "failed to generate debconf: {}", err),
        }
    }
}

/// Implemented using `Display` so that it can be used with `Termination` to display nicer message.
impl fmt::Debug for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        fmt::Display::fmt(self, f)
    }
}

impl From<ErrorData> for Error {
    fn from(data: ErrorData) -> Self {
        Error {
            data,
        }
    }
}

impl From<config::ValidationError> for Error {
    fn from(err: config::ValidationError) -> Self {
        Error {
            data: ErrorData::Config(err),
        }
    }
}

impl From<io::Error> for Error {
    fn from(err: io::Error) -> Self {
        Error {
            data: ErrorData::Io(err),
        }
    }
}

impl From<toml::de::Error> for Error {
    fn from(err: toml::de::Error) -> Self {
        Error {
            data: ErrorData::Toml(err),
        }
    }
}

impl From<manifest::Error> for Error {
    fn from(err: manifest::Error) -> Self {
        Error {
            data: ErrorData::Manifest(err),
        }
    }
}

impl From<manifest::LoadError> for Error {
    fn from(err: manifest::LoadError) -> Self {
        Error {
            data: ErrorData::Manifest(err.into()),
        }
    }
}

impl From<void::Void> for Error {
    fn from(value: void::Void) -> Self {
        match value {}
    }
}


#[cfg(feature = "debconf")]
impl From<debconf::Error> for Error {
    fn from(err: debconf::Error) -> Self {
        Error {
            data: ErrorData::Debconf(err),
        }
    }
}

fn load<S: Read>(mut source: S) -> Result<config::Config, Error> {
    let mut data = Vec::new();
    source.read_to_end(&mut data)?;
    let cfg = toml::from_slice::<config::raw::Config>(&data)?;
    let cfg = cfg.validate()?;

    Ok(cfg)
}

fn load_from_file<P: AsRef<Path>>(source: P) -> Result<::config::Config, Error> {
     let config_spec = std::fs::File::open(&source).map_err(|error| ErrorData::Open { file: source.as_ref().into(), error })?;

     load(config_spec)
}

fn path_in_out_dir<P: AsRef<Path>>(file_name: P) -> Result<PathBuf, Error> {
    let mut out: PathBuf = std::env::var_os("OUT_DIR").ok_or(ErrorData::MissingOutDir)?.into();
    out.push(file_name);

    Ok(out)
}

fn default_out_file(binary: Option<&str>) -> Result<PathBuf, Error> {
    const GENERATED_FILE_NAME: &str = "configure_me_config.rs";

    let file_name_owned;
    let file_name = match binary {
        Some(binary) => {
            file_name_owned = format!("{}_{}", binary, GENERATED_FILE_NAME);
            &file_name_owned
        },
        None => GENERATED_FILE_NAME,
    };
    path_in_out_dir(file_name)
}

// Wrapper for error conversions
fn create_file<P: AsRef<Path> + Into<PathBuf>>(file: P) -> Result<std::fs::File, Error> {
    std::fs::File::create(&file)
        .map_err(|error| ErrorData::Open { file: file.into(), error })
        .map_err(Into::into)
}

fn generate_to_file<P: AsRef<Path> + Into<PathBuf>>(config_spec: &::config::Config, file: P) -> Result<(), Error> {
     let config_code = create_file(file)?;
     ::fmt2io::write(config_code, |config_code| codegen::generate_code(config_spec, config_code)).map_err(Into::into)
}

fn load_and_generate_default<P: AsRef<Path>>(source: P, binary: Option<&str>) -> Result<::config::Config, Error> {
    let config_spec = load_from_file(&source)?;
    generate_to_file(&config_spec, default_out_file(binary)?)?;
    #[cfg(feature = "debconf")]
    debconf::generate_if_requested(&config_spec)?;
    println!("cargo:rerun-if-changed={}", source.as_ref().display());
    Ok(config_spec)
}

/// Generates the source code for you from provided `toml` configuration.
pub fn generate_source<S: Read, O: Write>(source: S, output: O) -> Result<(), Error> {
    let cfg = load(source)?;
    
     ::fmt2io::write(output, |output| codegen::generate_code(&cfg, output)).map_err(Into::into)
}

/// Generates the source code for you from provided `toml` configuration file.
///
/// This function is deprecated because if you use it external tools will be unable to see the path
/// to specification. It's much better to specify the path in `Cargo.toml` so that your app can be
/// processed automatically. (E.g. to generate man page in packagers.)
///
/// This function should be used from build script as it relies on cargo environment. It handles
/// generating the name of the file (it's called `config.rs` inside `OUT_DIR`) as well as notifying
/// cargo of the source file.
#[deprecated = "Use build_script_auto and put the path into Cargo.toml to expose it to external tools"]
pub fn build_script<P: AsRef<Path>>(source: P) -> Result<(), Error> {
    load_and_generate_default(source, None).map(::std::mem::drop)
}

/// Generates the source code for you
///
/// Finds the specification in Cargo.toml `metadata.configure_me`
///
/// This function should be used from build script as it relies on cargo environment. It handles
/// generating the name of the file (it's called `config.rs` inside `OUT_DIR`) as well as notifying
/// cargo of the source file.
pub fn build_script_auto() -> Result<(), Error> {
    use manifest::SpecificationPaths;

    let manifest_dir = manifest::get_dir()?;
    let manifest_file = manifest_dir.join("Cargo.toml");

    let paths = manifest_file
        .load_manifest()?
        .package.ok_or(manifest::Error::MissingPackage)?
        .metadata.ok_or(manifest::Error::MissingMetadata)?
        .configure_me.ok_or(manifest::Error::MissingConfigureMeMetadata)?
        .spec_paths;

    match paths {
        SpecificationPaths::Single(path) => load_and_generate_default(manifest_dir.join(path), None).map(::std::mem::drop),
        SpecificationPaths::PerBinary(binaries) => {
            for (binary, path) in binaries {
                load_and_generate_default(manifest_dir.join(path), Some(&binary)).map(::std::mem::drop)?;
            }
            Ok(())
        },
        SpecificationPaths::Other(other) => match other._private {},
    }
}

#[cfg(feature = "unstable-metabuild")]
pub fn metabuild() {
    build_script_auto().unwrap_or_else(|error| {
        println!("Could not generate configuration parser: {}", error);
        std::process::exit(1)
    })
}

/// Generates the source code and manual page at default location.
///
/// This function is deprecated because generating man page in compilation step is surprising. An
/// external `cfg_me` tool is provided that can generate the man page and save it to predictable
/// location. This function uses `OUT_DIR` which is a weird place to put man page (and there's no
/// better).
///
/// This is same as `build_script()`, but additionaly it generates a man page.
/// The resulting man page will be stored in `$OUT_DIR/app.man`.
#[cfg(feature = "man")]
#[deprecated = "use of cfg_me crate to build man pages is cleaner"]
pub fn build_script_with_man<P: AsRef<Path>>(source: P) -> Result<(), Error> {
    #[allow(deprecated)]
    build_script_with_man_written_to(source, path_in_out_dir("app.man")?)
}

/// Generates the source code and manual page at specified location.
///
/// This function is deprecated because generating man page in compilation step is surprising. An
/// external `cfg_me` tool is provided that can generate the man page and save it to predictable
/// location. This function needlessly burdens users of the crate to handle location configuration
/// and makes it hard for toold like packagers to read the man page.
///
/// This is same as `build_script_with_man()`, but it allows you to choose where to put the man
/// page.
#[cfg(feature = "man")]
#[deprecated = "use of cfg_me crate to build man pages is cleaner"]
pub fn build_script_with_man_written_to<P: AsRef<Path>, M: AsRef<Path> + Into<PathBuf>>(source: P, output: M) -> Result<(), Error> {
    let config_spec = load_and_generate_default(source, None)?;
    let manifest = manifest::BuildScript.load_manifest()?;
    let man_page = gen_man::generate_man_page(&config_spec, manifest.borrow())?;

    let mut file = create_file(output)?;
    file.write_all(man_page.as_bytes())?;
    #[cfg(feature = "debconf")]
    debconf::generate_if_requested(&config_spec)?;
    Ok(())
}

/// Generates man page **only**.
///
/// This is useful outside build scripts.
#[cfg(feature = "man")]
pub fn generate_man<M: LoadManifest, W: std::io::Write, S: AsRef<Path>>(source: S, mut dest: W, manifest: M) -> Result<(), Error> where Error: std::convert::From<<M as manifest::LoadManifest>::Error> {
    let config_spec = load_from_file(&source)?;
    let manifest = manifest.load_manifest()?;
    let man_page = gen_man::generate_man_page(&config_spec, manifest.borrow())?;
    dest.write_all(man_page.as_bytes())?;
    Ok(())
}

#[cfg(test)]
#[deny(warnings)]
pub(crate) mod tests {
    use ::generate_source;

    pub const SINGLE_OPTIONAL_PARAM: &str =
r#"
[general]
env_prefix = "TEST_APP"

[[param]]
name = "foo"
type = "u32"
"#;

    pub const SINGLE_MANDATORY_PARAM: &str =
r#"
[general]
env_prefix = "TEST_APP"

[[param]]
name = "foo"
type = "u32"
optional = false
"#;

    pub const SINGLE_DEFAULT_PARAM: &str =
r#"
[general]
env_prefix = "TEST_APP"

[[param]]
name = "foo"
type = "u32"
default = "42"
"#;

    pub const SINGLE_SWITCH: &str =
r#"
[general]
env_prefix = "TEST_APP"

[[switch]]
name = "foo"
"#;

    pub const MULTIPLE_PARAMS: &str =
r#"
[general]
env_prefix = "TEST_APP"

[[param]]
name = "foo"
type = "u32"
default = "42"
doc = "A foo"

[[param]]
name = "bar"
type = "String"
optional = true
doc = "A very, very, very, very, very, very, very, very, very, very, very, very, very, very long documentation..."

[[param]]
name = "baz"
type = "String"
optional = false
doc = "A much, much, much, much, much, much, much, much, much, much, much, much, much, much, much, much, much, much, much, much, much, much, much, much, much, much, much, much, much, much, much, much, much, much, much, much, much, much, much, much, much, much longer documentation..."

[[switch]]
name = "verbose"
# doc intentionally missing, because it's obious...

[[switch]]
name = "fast"
default = true
doc = "Determines whether to mine bitcoins fast or slowly"
"#;

    pub const NO_ARG: &str =
r#"
[general]
env_prefix = "TEST_APP"

[[param]]
name = "foo"
type = "u32"
argument = false
"#;

    pub const SHORT_SWITCHES: &str =
r#"
[[switch]]
name = "a"
abbr = "a"
doc = "test"

[[switch]]
name = "b"
abbr = "b"

[[switch]]
name = "c"
abbr = "c"
count = true

[[param]]
name = "d"
type = "String"
abbr = "d"
optional = true

[[param]]
name = "e"
type = "String"
abbr = "e"
optional = true

[[switch]]
name = "foo_bar"
abbr = "f"
"#;

    pub const CONF_FILES: &str =
r#"
[general]
env_prefix = "TEST_APP"
conf_file_param = "config"
conf_dir_param = "conf_dir"

[[param]]
name = "foo"
type = "u32"
doc = "A foo"
"#;

    pub const CUSTOM_MERGE_FN: &str =
r#"
[general]
env_prefix = "TEST_APP"

[[param]]
name = "foo"
type = "u32"
merge_fn = "(|a: &mut u32, b: u32| *a += b)"

[[param]]
name = "bar"
type = "String"
merge_fn = "(|a: &mut String, b: String| a.push_str(&b))"
"#;

    pub struct ExpectedOutput {
        pub raw_config: &'static str,
        pub validate: &'static str,
        pub merge_in: &'static str,
        pub merge_args: &'static str,
        pub config: &'static str,
        pub arg_parse_error: &'static str,
    }

    pub const EXPECTED_EMPTY: ExpectedOutput = ExpectedOutput {
        raw_config: include_str!("../tests/expected_outputs/empty/raw_config.rs"),
        validate: include_str!("../tests/expected_outputs/empty/validate.rs"),
        merge_in: include_str!("../tests/expected_outputs/empty/merge_in.rs"),
        merge_args: include_str!("../tests/expected_outputs/empty/merge_args.rs"),
        config: include_str!("../tests/expected_outputs/empty/config.rs"),
        arg_parse_error: include_str!("../tests/expected_outputs/empty/arg_parse_error.rs"),
    };

    pub const EXPECTED_SINGLE_OPTIONAL_PARAM: ExpectedOutput = ExpectedOutput {
        raw_config: include_str!("../tests/expected_outputs/single_optional_param/raw_config.rs"),
        validate: include_str!("../tests/expected_outputs/single_optional_param/validate.rs"),
        merge_in: include_str!("../tests/expected_outputs/single_optional_param/merge_in.rs"),
        merge_args: include_str!("../tests/expected_outputs/single_optional_param/merge_args.rs"),
        config: include_str!("../tests/expected_outputs/single_optional_param/config.rs"),
        arg_parse_error: include_str!("../tests/expected_outputs/single_optional_param/arg_parse_error.rs"),
    };

    pub const EXPECTED_SINGLE_MANDATORY_PARAM: ExpectedOutput = ExpectedOutput {
        raw_config: include_str!("../tests/expected_outputs/single_mandatory_param/raw_config.rs"),
        validate: include_str!("../tests/expected_outputs/single_mandatory_param/validate.rs"),
        merge_in: include_str!("../tests/expected_outputs/single_mandatory_param/merge_in.rs"),
        merge_args: include_str!("../tests/expected_outputs/single_mandatory_param/merge_args.rs"),
        config: include_str!("../tests/expected_outputs/single_mandatory_param/config.rs"),
        arg_parse_error: include_str!("../tests/expected_outputs/single_mandatory_param/arg_parse_error.rs"),
    };

    pub const EXPECTED_SINGLE_DEFAULT_PARAM: ExpectedOutput = ExpectedOutput {
        raw_config: include_str!("../tests/expected_outputs/single_default_param/raw_config.rs"),
        validate: include_str!("../tests/expected_outputs/single_default_param/validate.rs"),
        merge_in: include_str!("../tests/expected_outputs/single_default_param/merge_in.rs"),
        merge_args: include_str!("../tests/expected_outputs/single_default_param/merge_args.rs"),
        config: include_str!("../tests/expected_outputs/single_default_param/config.rs"),
        arg_parse_error: include_str!("../tests/expected_outputs/single_default_param/arg_parse_error.rs"),
    };

    pub const EXPECTED_SINGLE_SWITCH: ExpectedOutput = ExpectedOutput {
        raw_config: include_str!("../tests/expected_outputs/single_switch/raw_config.rs"),
        validate: include_str!("../tests/expected_outputs/single_switch/validate.rs"),
        merge_in: include_str!("../tests/expected_outputs/single_switch/merge_in.rs"),
        merge_args: include_str!("../tests/expected_outputs/single_switch/merge_args.rs"),
        config: include_str!("../tests/expected_outputs/single_switch/config.rs"),
        arg_parse_error: include_str!("../tests/expected_outputs/single_switch/arg_parse_error.rs"),
    };

    pub const EXPECTED_SHORT_SWITCHES: ExpectedOutput = ExpectedOutput {
        raw_config: include_str!("../tests/expected_outputs/short_switches/raw_config.rs"),
        validate: include_str!("../tests/expected_outputs/short_switches/validate.rs"),
        merge_in: include_str!("../tests/expected_outputs/short_switches/merge_in.rs"),
        merge_args: include_str!("../tests/expected_outputs/short_switches/merge_args.rs"),
        config: include_str!("../tests/expected_outputs/short_switches/config.rs"),
        arg_parse_error: include_str!("../tests/expected_outputs/short_switches/arg_parse_error.rs"),
    };

    fn check(src: &str, expected: &str) {
        use std::io::Write;

        let mut src = src.as_bytes();
        let mut out = Vec::new();
        generate_source(&mut src, &mut out).unwrap();
        if out != expected.as_bytes() {
            let mut expected_temp = tempfile::Builder::new().prefix("expected").tempfile().unwrap();
            let mut out_temp = tempfile::Builder::new().prefix("output").tempfile().unwrap();

            expected_temp.write_all(expected.as_bytes()).unwrap();
            out_temp.write_all(&out).unwrap();

            std::process::Command::new("diff")
                .arg(expected_temp.path())
                .arg(out_temp.path())
                .spawn()
                .expect("failed to run diff")
                .wait()
                .unwrap();
            panic!("output differs from expected");
        }
    }

    #[test]
    fn empty() {
        check("", include_str!(concat!(env!("OUT_DIR"), "/expected_outputs/empty-config.rs")));
    }

    #[test]
    fn single_optional_param() {
        check(SINGLE_OPTIONAL_PARAM, include_str!(concat!(env!("OUT_DIR"), "/expected_outputs/single_optional_param-config.rs")));
    }

    #[test]
    fn single_mandatory_param() {
        check(SINGLE_MANDATORY_PARAM, include_str!(concat!(env!("OUT_DIR"), "/expected_outputs/single_mandatory_param-config.rs")));
    }

    #[test]
    fn single_default_param() {
        check(SINGLE_DEFAULT_PARAM, include_str!(concat!(env!("OUT_DIR"), "/expected_outputs/single_default_param-config.rs")));
    }

    #[test]
    fn single_switch() {
        check(SINGLE_SWITCH, include_str!(concat!(env!("OUT_DIR"), "/expected_outputs/single_switch-config.rs")));
    }

    #[test]
    fn multiple_params() {
        check(MULTIPLE_PARAMS, include_str!(concat!(env!("OUT_DIR"), "/expected_outputs/multiple_params-config.rs")));
    }

    #[test]
    fn no_arg() {
        check(NO_ARG, include_str!(concat!(env!("OUT_DIR"), "/expected_outputs/no_arg-config.rs")));
    }

    #[test]
    fn short_switches() {
        check(SHORT_SWITCHES, include_str!(concat!(env!("OUT_DIR"), "/expected_outputs/short_switches-config.rs")));
    }

    #[test]
    fn conf_files() {
        check(CONF_FILES, include_str!(concat!(env!("OUT_DIR"), "/expected_outputs/conf_files-config.rs")));
    }

    #[test]
    fn custom_merge_fn() {
        check(CUSTOM_MERGE_FN, include_str!(concat!(env!("OUT_DIR"), "/expected_outputs/with_custom_merge-config.rs")));
    }
}