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
//! 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;
extern crate colored;

use std::fs::File;
use std::io::prelude::*;
use toml::de;
use colored::*;
use std::path::PathBuf;

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.
/// If no such file is found, read from global template directory in `$HOME/.pi_templates/`.
pub fn read_toml_dir(template_path: &str, home: PathBuf) -> (types::Project, bool) {
    let (mut template_file, is_global_template) =
        if let Ok(f) = File::open(&template_path) {
            (f, false)
        }
        else if let Ok(f) = { let mut p = home ; p.push(".pi_templates/") ; p.push(template_path) ; File::open(p) } {
            (f, true)
        }
        else {
            println!("{}: File {:?} could not be opened. Check that it exists.", "Error".red(), template_path);
            std::process::exit(0x0f00);
        };
    let mut template = String::new();
    template_file.read_to_string(&mut template)
        .expect("Failed to read file"); // we can panic because we already errored if the file didn't exist.
    (read_toml_str(template, template_path), is_global_template)
}

/// Read a string containing a toml file
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();
    if let Ok(_) = file.read_to_string(&mut toml_str) {
        if let Ok(t) = toml::from_str(&toml_str) {
            t
        }
        else if let Err(e) = toml::from_str(&toml_str): Result<String, de::Error> {
            println!("Error parsing {:?}: {}", config_path, e);
            std::process::exit(0x0f00);
        }
        else {
            std::process::exit(0x0f00);
        }
    }
    else {
        println!("{}: No ~/.pi.toml found. Using defaults.", "Warning".yellow());
        types::Config { version_control: None, author: None, license: None, user: None }
    }
}