texc-config 0.1.7

Config and Error library for TexCreate
Documentation
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
//! TexCreate Config Library <br>
//! This library contains all code related to `config.toml` <br>
//! Developer: Mustafif Khan <br>
//! License: MIT & GPLv2

use std::io::Write;
use std::path::Path;
use std::path::PathBuf;
use std::process::exit;
use async_std::fs::*;
use async_std::io::prelude::*;
use async_std::io::stdin;
use rocket::form::FromForm;
use serde::{Deserialize, Serialize};
use texcreate_lib::config::Config as LegacyConfig;
use toml::{from_str, to_string_pretty};
use zip::CompressionMethod::Stored;
use zip::write::{FileOptions, ZipWriter};

pub use error::*;
pub use extra::*;
pub use custom_templates::*;
use tex_rs::Latex;
use texc_latex::news::news;
use texc_latex::templates::*;

/// Contains all error handling using `failure` crate
pub mod error;
/// Contains all code related to the `README.md` & `texcreate.toml`
pub mod extra;
/// Contains all code related to creating custom texcreate templates
pub mod custom_templates;

type F = std::fs::File;


/// The Config struct contains all necessary metadata information
/// - Author: The author of the project
/// - Title: The title of the document
/// - Date: The date for the document
/// - Project Name: The name for the project
/// - Template: The prebuilt template to use
/// - Paper-size: The paper-size for the document
/// - Document Class: The document class of the document
/// - Font size: The font size for the document
/// - Packages: Additional packages to add
/// - Only Files: Whether structure is project or files _(default: false)_
#[derive(Debug, Clone, Deserialize, Serialize, FromForm)]
pub struct Config {
    pub author: String,
    pub title: String,
    pub date: String,
    pub project_name: String,
    pub template: Option<String>,
    pub custom_template: Option<String>,
    pub paper_size: String,
    pub document_class: String,
    pub font_size: u8,
    pub packages: Vec<String>,
    pub only_files: Option<bool>,
}

impl Default for Config {
    /// Default settings for Config
    fn default() -> Self {
        Self {
            author: "Author".to_string(),
            title: "Title".to_string(),
            date: "Date".to_string(),
            project_name: "Project".to_string(),
            template: Some("Basic".to_string()),
            custom_template: None,
            paper_size: "letterpaper".to_string(),
            document_class: "article".to_string(),
            font_size: 11,
            packages: vec![],
            only_files: None,
        }
    }
}

impl Config {
    /// Creates a new Config
    pub fn new(
        author: &str,
        title: &str,
        date: &str,
        project_name: &str,
        template: &Option<String>,
        custom_templates: &Option<String>,
        paper_size: &str,
        font_size: u8,
        packages: Vec<&str>,
        only_files: Option<bool>,
        document_class: &str,
    ) -> Self {
        Self {
            author: author.to_string(),
            title: title.to_string(),
            date: date.to_string(),
            project_name: project_name.to_string(),
            template: template.to_owned(),
            custom_template: custom_templates.to_owned(),
            paper_size: paper_size.to_string(),
            document_class: document_class.to_string(),
            font_size,
            packages: packages.iter().map(|x| x.to_string()).collect(),
            only_files,
        }
    }
    /// Using `toml::to_string_pretty()`, turns Config into toml string for `config.toml`
    pub fn to_string(&self) -> Result<String, toml::ser::Error> {
        Ok(to_string_pretty(self)?)
    }
    /// From `config.toml` string, turns into Config
    pub fn from_string(s: String) -> Result<Self, toml::de::Error> {
        Ok(from_str(&s)?)
    }
    /// Turns a v1 `config.toml` into v2 Config toml string
    pub fn migrate() -> Result<String, toml::ser::Error> {
        let legacy = LegacyConfig::config(&None);
        let config = Config::new(
            &legacy.Project.author,
            &legacy.Project.title,
            &legacy.Project.date,
            &legacy.Project.project_name,
            &Some(legacy.Project.template),
            &None,
            &legacy.Document.paper_size,
            legacy.Document.font_size,
            legacy
                .Document
                .packages
                .iter()
                .map(|x| x.as_str())
                .collect(),
            None,
            &legacy.Document.document_class,
        );
        config.to_string()
    }

    /// Grabs the `tex_rs::Latex` template from `&self.template` by matching the string
    pub fn template(&self) -> TexCreateResult<Latex> {
        let f = match &*self.template.clone().unwrap() {
            "Basic" => basic(
                self.clone().font_size,
                &self.paper_size,
                &self.document_class,
                &self.author,
                &self.title,
                &self.date,
                &self.packages
            ),
            "Code" => code(
                self.clone().font_size,
                &self.paper_size,
                &self.document_class,
                &self.author,
                &self.title,
                &self.date,
                &self.packages
            ),
            "Novel" => novel(
                self.clone().font_size,
                &self.paper_size,
                &self.document_class,
                &self.author,
                &self.title,
                &self.date,
                &self.packages
            ),
            "Theatre" => theatre(
                self.clone().font_size,
                &self.paper_size,
                &self.document_class,
                &self.author,
                &self.title,
                &self.date,
                &self.packages
            ),
            "Lachaise" => lachaise(
                self.clone().font_size,
                &self.paper_size,
                &self.document_class,
                &self.author,
                &self.title,
                &self.date,
                &self.packages
            ),
            "Lachaise-Mod" => lachaise_modified(
                self.clone().font_size,
                &self.paper_size,
                &self.document_class,
                &self.author,
                &self.title,
                &self.date,
                &self.packages
            ),
            "Dictionary" => dictionary(
                self.clone().font_size,
                &self.paper_size,
                &self.document_class,
                &self.author,
                &self.title,
                &self.date,
                &self.packages
            ),
            "News" => news(
                self.clone().font_size,
                &self.paper_size,
                &self.document_class,
                &self.author,
                &self.title,
                &self.date,
                &self.packages
            ),
            "Beamer" => beamer(
                self.clone().font_size,
                &self.paper_size,
                &self.document_class,
                &self.author,
                &self.title,
                &self.date,
                &self.packages
            ),
            _ => return Err(TexCreateError::InvalidTemplate(self.template.as_ref().unwrap().clone())),
        };
        Ok(f)
    }
    /// Builds project depending on `Config.only_files`
    pub async fn build(&self, force: Option<bool>) -> TexCreateResult<()> {
        let force = match force{
            Some(a) => a,
            None => false
        };

        if !force{
            println!("Checking for any errors...");
            match check_errors(&self){
                Ok(_) => (),
                Err(e) => {
                    panic!("{}", e)
                }
            };
        }
        let latex: Latex;
        let template: String;
        if self.template.is_some(){
            template = match &self.template{
                Some(a) => a.to_string(),
                None => "".to_string()
            };
            println!("Loading template: {}", &template);
            if &template == "Book"{
                book(&self.project_name, &self.title, &self.author).await.unwrap();
                return Ok(());
            }
            latex = self.template()?;
        } else{
            template = match &self.custom_template{
                Some(a) => a.to_string(),
                None => ".".to_string()
            };
            let path = PathBuf::from(&template);
            println!("Loading custom template: {}", &template);
            latex = create_custom_template(
                path,
                self.clone().font_size,
                &self.paper_size,
                &self.document_class,
                &self.author,
                &self.title,
                &self.date,
                &self.packages
            ).await?;
        }

        println!("Creating project: {}", &self.project_name);
        let path = Path::new(&self.project_name);
        // Project/
        if path.exists(){
            println!("[WARN] {} already exists, remove the directory? (yes/no): ", &self.project_name);
            let mut answer = String::new();
            stdin().read_line(&mut answer).await?;
            match answer.trim(){
                "yes" => {
                    remove_dir_all(&path).await?;
                }
                _ => {
                    println!("Please change the project name and build again.");
                    exit(0);
                }
            }
        }
        create_dir(&path).await?;
        if self.only_files == None || self.only_files.unwrap() == false {
            // README.md
            let readme_path = path.to_path_buf().join("README.md");
            let mut readme = File::create(readme_path).await?;
            readme
                .write_all(readme_make(&self.project_name).as_bytes())
                .await?;
            // texcreate.toml
            write_toml(path.to_path_buf(), &self.project_name).await?;
            // out/
            let out = path.to_path_buf().join("out");
            create_dir(&out).await?;
            // src/
            let src = path.to_path_buf().join("src");
            create_dir(&src).await?;
            println!("Writing {} Template in {}", &template, &src.to_str().unwrap());
            latex
                .split_write(
                    src.clone().join(&format!("{}.tex", &self.project_name)),
                    src.clone().join("structure.tex"),
                )
                .await?;
        } else {
            let path = path.to_path_buf();
            latex
                .split_write(
                    path.clone().join(&format!("{}.tex", &self.project_name)),
                    path.clone().join("structure.tex"),
                )
                .await?;
        }
        Ok(())
    }
    /// Zips a project structure
    pub async fn zip_proj(&self, path: PathBuf) -> TexCreateResult<()> {
        let mut zip = ZipWriter::new(
            F::create(format!(
                "{}/{}.zip",
                path.to_str().unwrap(),
                &self.project_name
            ))
            .unwrap(),
        );
        let options = FileOptions::default().compression_method(Stored);

        // README.md
        let readme_path = path.join("README.md");
        let _ = F::create(&readme_path).unwrap();
        zip.start_file(readme_path.to_str().unwrap(), options)
            .unwrap();
        zip.write_all(README.as_bytes()).unwrap();
        // texcreate.toml
        let toml_path = path.join("texcreate.toml");
        let _ = F::create(&toml_path).unwrap();
        zip.start_file(toml_path.to_str().unwrap(), options)
            .unwrap();
        let mut tex_toml = TexcToml::default();
        tex_toml.project_name = self.clone().project_name;
        zip.write_all(to_string_pretty(&tex_toml).unwrap().as_bytes())
            .unwrap();
        // out/
        let out_path = path.join("out");
        create_dir(&out_path).await?;
        zip.add_directory(out_path.to_str().unwrap(), options).unwrap();
        // src/
        let src = path.join("src");
        create_dir(&src).await?;

        // src/*.tex
        let temp_str = self.template()?.split_string();

        let main = src.join(format!("{}.tex", &self.project_name));
        let _ = F::create(&main).unwrap();

        let structure = src.join("structure.tex");
        let _ = F::create(&structure).unwrap();

        zip.start_file(main.to_str().unwrap(), options).unwrap();
        zip.write_all(&temp_str.0.as_bytes()).unwrap();

        zip.start_file(structure.to_str().unwrap(), options).unwrap();
        zip.write_all(&temp_str.1.as_bytes()).unwrap();

        zip.finish().unwrap();

        // Cleaning step
        remove_file(&readme_path).await?;
        remove_file(&toml_path).await?;
        remove_dir(&out_path).await?;
        remove_dir_all(&src).await?;

        Ok(())
    }
    /// Zips a only files structure
    pub async fn zip_files(&self, path: PathBuf) -> TexCreateResult<()> {
        let mut zip = ZipWriter::new(
            F::create(format!(
                "{}/{}.zip",
                path.to_str().unwrap(),
                &self.project_name
            ))
            .unwrap(),
        );
        let options = FileOptions::default().compression_method(Stored);

        let main = format!("{}/{}.tex", path.to_str().unwrap(), &self.project_name);
        let structure = path.join("structure.tex");
        let _ = File::create(&main);
        let _ = File::create(&structure);
        let temp_str = self.template()?.split_string();

        zip.start_file(&main, options).unwrap();
        zip.write_all(&temp_str.0.as_bytes()).unwrap();

        zip.start_file(structure.to_str().unwrap(), options)
            .unwrap();
        zip.write_all(&temp_str.1.as_bytes()).unwrap();

        zip.finish().unwrap();

        // Cleaning step
        remove_file(&main).await?;
        remove_file(&structure).await?;

        Ok(())
    }
}