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
//! This library provides the functions/structs/methods used by the main binary. They are included
//! here in the hopes that they can be illuminating to users.

#![feature(type_ascription)]

#[macro_use] extern crate serde_derive;
extern crate toml;
extern crate time;
extern crate core;

use std::fs::File;
use std::io::prelude::*;
use toml::de;

pub mod types;
pub mod repo;
pub mod includes;
pub mod render;

/// Given a filepath, read the .toml file there as containing the directories/templates.
pub fn read_toml_dir(template_path: &str) -> types::Project {
    let mut template_file =
        if let Ok(f) = File::open(&template_path) {
            f
        }
        else {
            println!("File {:?} could not be opened. Check that it exists.", template_path);
            std::process::exit(0x0f00);
        };
    let mut template = String::new();
    template_file.read_to_string(&mut template)
        .expect("Template file read failed");
    read_toml_str(template, template_path)
}

pub fn read_toml_str(template: String, template_path: &str) -> types::Project {
    if let Ok(t) = toml::from_str(&template) {
        t
    }
    else if let Err(e) = toml::from_str(&template): Result<String, de::Error> {
        println!("Error parsing {:?}: {}", template_path, e);
        std::process::exit(0x0f00);
    }
    else {
        std::process::exit(0x0f00);
    }
}
    
/// Given a PathBuf, read the .toml file there as a configuration file.
pub fn read_toml_config(config_path: std::path::PathBuf) -> types::Config {
    let mut file =
        if let Ok(f) = File::open(&config_path) {
            f
        }
        else {
            println!("File {:?} could not be opened. Check that it exists.", config_path);
            std::process::exit(0x0f00);
        };
    let mut toml_str = String::new();
    file.read_to_string(&mut toml_str)
        .expect("File $HOME/.pi.toml read failed");
    toml::from_str(&toml_str).unwrap()
}