texcreate_lib/routes.rs
1use crate::Templates::*;
2use std::io::Write;
3use std::path::Path;
4use std::fs::create_dir;
5// Constant Convert
6///
7/// Used to have the template constants into an easy tuple
8///
9/// # Examples
10///
11/// ```
12/// use texcreate_lib::routes::const_conv;
13/// use texcreate_lib::Templates::basic;
14/// use std::io::prelude::*;
15///
16/// // Let's build a basic template
17/// fn main(){
18/// let basic = const_conv(Basic_MAIN, basic::BASIC_STRUCTURE);
19/// // Write main.tex
20/// let mut main = std::fs::File::create("main.tex").unwrap();
21/// main.write_all(basic.0.as_bytes()).unwrap();
22/// // Write structure.tex
23/// let mut structure = std::fs::File::create("structure.tex").unwrap();
24/// structure.write_all(basic.1.as_bytes()).unwrap();
25/// }
26/// ```
27
28
29pub fn const_conv(main: &str, structure: &str)-> (String, String) {
30 (main.to_string(), structure.to_string())
31}
32
33/// Load Template
34///
35/// Given a template name, it will load the template and return it as a string tuple
36///
37/// # Examples
38///
39/// ```
40/// use texcreate_lib::routes::load;
41///
42/// // Recreate the basic template from `const_conv`:
43/// fn main(){
44/// let basic = load("Basic");
45/// // Write main.tex
46/// let mut main = std::fs::File::create("main.tex").unwrap();
47/// main.write_all(basic.0.as_bytes()).unwrap();
48/// // Write structure.tex
49/// let mut structure = std::fs::File::create("structure.tex").unwrap();
50/// structure.write_all(basic.1.as_bytes()).unwrap();
51/// }
52/// ```
53
54
55
56pub fn load(template: &str) -> (String, String) {
57 match template {
58 "Basic" => const_conv(basic::BASIC_MAIN, basic::BASIC_STRUCTURE),
59 "Code" => const_conv(code::CODE_MAIN, code::CODE_STRUCTURE),
60 "Math" => const_conv(math::MATH_MAIN, math::MATH_STRUCTURE),
61 "Theatre" => const_conv(theatre::THEATRE_MAIN, theatre::THEATRE_STRUCTURE),
62 "Novel" => const_conv(novel::NOVEL_MAIN, novel::NOVEL_STRUCTURE),
63 "Beamer" => const_conv(beamer::BEAMER_MAIN, beamer::BEAMER_STRUCTURE),
64 "Lachaise" => const_conv(lachaise::LACHAISE_MAIN, lachaise::LACHAISE_STRUCTURE),
65 _ => panic!("Unknown template: {}", template),
66 }
67}
68
69/// Create Template
70///
71/// Given:
72/// - A project name
73/// - A path to the template (Optional)
74/// - A template name
75///
76/// # Examples
77///
78/// ```
79/// use texcreate_lib::routes::create;
80///
81/// // Recreate the basic template
82/// fn main(){
83/// let name = "MyProj";
84/// let path = "path/";
85/// let template = "Basic";
86/// create(name, path, template);
87/// }
88/// ```
89
90
91pub fn create(name: &str, file_path: &Option<String>, template: &str) {
92 // Sanity check for file_path
93 let file_path = match file_path{
94 Some(path) => path,
95 None => "."
96 };
97 //Loading template tc_files
98 println!("Loading {} template...", template);
99 let (main, structure) = load(template);
100 let path = format!("{}/{}", file_path, name);
101 let def_path = format!("{}/{}_texcreate", file_path, name); //default path
102 let mut p = String::new();
103 // Check if directory exists, if it does, use def
104 // else create the directory
105 if Path::new(&path).exists(){
106 let f = format!("Note: {} exists, creating {} instead\n", &path, &def_path);
107 println!("{}",f);
108 match create_dir(&def_path){
109 Ok(dir) => dir,
110 Err(e) => eprintln!("{}", e)
111 };
112 p = def_path;
113 } else {
114 println!("Creating {}", &path);
115 match create_dir(&path){
116 Ok(dir) => dir,
117 Err(e) => eprintln!("{}", e)
118 };
119 p = path;
120 }
121 //Creating the main file
122 println!("Creating {}/{}.tex", &p, name);
123 let mut main_file =
124 std::fs::File::create(format!("{}/{}.tex", &p, name)).expect("Couldn't create file");
125 match main_file.write_all(main.as_bytes()){
126 Ok(write) => write,
127 Err(e) => eprintln!("{}", e)
128 };
129 //Creating structure.tex
130 println!("Creating {}/structure.tex", &p);
131 let mut structure_file =
132 std::fs::File::create(format!("{}/structure.tex", &p)).expect("Couldn't create file");
133 match structure_file.write_all(structure.as_bytes()){
134 Ok(write) => write,
135 Err(e) => eprintln!("{}", e)
136 };
137}