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
use std::path::{Path, PathBuf};
use clap::Parser as ClapParser;
use crate::error::{self, VestiCommandUtilErrKind};
#[derive(ClapParser)]
#[command(author, version, about)]
pub enum VestiOpt {
/// Initialize the vesti project
Init {
#[clap(name = "PROJECT_NAME")]
project_name: Option<String>,
},
/// Compile vesti into Latex file
Run {
/// Compile vesti continuously.
#[clap(short, long)]
continuous: bool,
/// If this flag is on, then vesti compiles all vesti files in that directory.
#[clap(long)]
all: bool,
/// Input file names or directory name.
/// Directory name must type once.
#[clap(value_name = "FILE")]
file_name: Vec<PathBuf>,
},
/// Use the experimental lexer, parser and backend to compile vesti.
/// After all of features are fully implemented, they will be the default
/// front, middle, backend for vesti.
Experimental {
/// Compile vesti continuously.
#[clap(short, long)]
continuous: bool,
/// Input file names or directory name.
/// Directory name must type once.
#[clap(value_name = "FILE")]
file_name: Vec<PathBuf>,
},
}
impl VestiOpt {
pub fn take_filename(&self) -> error::Result<Vec<PathBuf>> {
let mut output: Vec<PathBuf> = Vec::new();
if let Self::Run {
continuous: _,
all,
file_name,
} = self
{
if !all {
return Ok(file_name.clone());
}
assert_eq!(file_name.len(), 1);
let file_dir = file_name[0].ancestors().nth(1);
let current_dir = if file_dir == Some(Path::new("")) {
Path::new(".").to_path_buf()
} else if let Some(path) = file_dir {
path.to_path_buf()
} else {
return Err(error::VestiErr::UtilErr {
err_kind: VestiCommandUtilErrKind::NoFilenameInputErr,
});
};
for path in walkdir::WalkDir::new(current_dir) {
match path {
Ok(dir) => {
if let Some(ext) = dir.path().extension() {
if ext == "ves" {
output.push(dir.into_path())
}
}
}
Err(_) => {
return Err(error::VestiErr::UtilErr {
err_kind: VestiCommandUtilErrKind::TakeFilesErr,
})
}
}
}
output.sort();
}
Ok(output)
}
#[allow(unused)]
pub fn take_filename_experimental(&self) -> error::Result<Vec<PathBuf>> {
todo!()
}
}