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
//! ZAMM is a literate programming tool to help with Yin and Yang development. It can be used as a
//! binary or as a library. To have it run automatically as a part of the regular Cargo build
//! process, put this in your `Cargo.toml`:
//!
//! ```toml
//! [build-dependencies]
//! zamm = "0.1.1"
//! ```
//!
//! Then define your `build.rs` as such:
//!
//! ```no_run
//! use zamm::generate_default_code;
//!
//! fn main() {
//!     generate_default_code("yin.md").unwrap();
//! }
//! ```

#![warn(missing_docs)]
#![allow(clippy::needless_doctest_main)]

/// Running commandline commands.
pub mod commands;
/// Creating the intermediate build binary.
pub mod intermediate_build;
/// Finding and parsing the input files.
pub mod parse;

use intermediate_build::generate_final_code;
pub use intermediate_build::CodegenConfig;
use parse::{find_file, parse_input, ParseOutput};
use std::io::Error;

/// Generates an intermediate binary from the given file and runs it. If no file is specified, then
/// it will search for a `yin.md` file in the current directory.
///
/// Returns the contents of the input file.
pub fn generate_code(
    input_file: Option<&str>,
    codegen_cfg: &CodegenConfig,
) -> Result<ParseOutput, Error> {
    // no need to regenerate autogenerated files every time
    println!("cargo:rerun-if-changed=build.rs");
    let found_input = find_file(input_file)?;
    let literate_rust_code = parse_input(found_input)?;
    generate_final_code(&literate_rust_code.extractions, codegen_cfg);
    Ok(literate_rust_code)
}

/// Generates an intermediate binary from the given file and runs it with default codegen settings.
/// Recommended for automatic Cargo builds.
pub fn generate_default_code(input_file: &str) -> Result<ParseOutput, Error> {
    generate_code(Some(input_file), &CodegenConfig::default())
}

/// Generates code specifically for Yin.
pub fn generate_yin_code(input_file: &str) -> Result<ParseOutput, Error> {
    generate_code(
        Some(input_file),
        &CodegenConfig {
            yin: true,
            ..CodegenConfig::default()
        },
    )
}