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
#![feature(type_ascription)]
#[macro_use]
extern crate serde_derive;
extern crate toml;
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;
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",
);
(read_toml_str(&template, template_path), is_global_template)
}
pub fn read_toml_str(template: &str, 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);
}
}
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 file.read_to_string(&mut toml_str).is_ok() {
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 {
eprintln!(
"{}: No ~/.pi.toml found. Using defaults.",
"Warning".yellow()
);
types::Config {
version_control: None,
author: None,
license: None,
user: None,
}
}
}