Newer
Older
use crate::{analyzer::AnalyzerType, error::OpossumError};
use chrono::DateTime;
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
use clap::{builder::OsStr, Parser};
use rprompt::prompt_reply;
use std::path::{Path, PathBuf};
use structopt::StructOpt;
use strum::IntoEnumIterator;
type Result<T> = std::result::Result<T, OpossumError>;
/// Command line arguments for the OPOSSUM application.
pub struct Args {
/// file path of the optical setup, which should be read in
pub file_path: PathBuf,
/// analyzer type that should be used to analyze the optical setup
pub analyzer: AnalyzerType,
/// destination directory of the report. if not defined, same directory as the filepath for the optical setup is used
pub report_directory: PathBuf,
}
#[derive(Parser, StructOpt)]
#[command(author, version, about, long_about = None)]
pub struct PartialArgs {
/// filepath of the opticscenery to read in
#[arg(short, long)]
file_path: Option<String>,
/// analyzer type that should be used to analyze the optical setup
#[arg(short, long)]
analyzer: Option<String>,
/// destination directory of the report. if not defined, same directory as the filepath for the optical setup is used
#[arg(short, long)]
report_directory: Option<String>,
}
fn file_path_is_valid(path: &Path) -> bool {
Path::exists(path) && Path::is_file(path) && path.extension() == Some(&OsStr::from("opm"))
}
fn eval_file_path_input(file_path: &str) -> Option<PathBuf> {
if file_path_is_valid(Path::new(file_path)) {
Some(PathBuf::from(file_path))
} else {
None
}
}
fn eval_analyzer_input(analyzer_input: &str) -> Option<AnalyzerType> {
match analyzer_input {
"e" => Some(AnalyzerType::Energy),
"p" => Some(AnalyzerType::ParAxialRayTrace),
_ => None,
}
}
fn eval_report_directory_input(report_input: &str) -> Option<PathBuf> {
let r_path = Path::new(&report_input);
if Path::exists(r_path) {
Some(PathBuf::from(report_input))
} else if report_input.is_empty() {
Some(PathBuf::from(""))
} else {
None
}
}
fn create_prompt_str(flag: &str, init_str: &str) -> Result<String> {
let mut prompt_str = init_str.to_owned();
match flag{
"f" =>{
Ok(prompt_str + "Please insert path to optical-setup-description file:\n")
},
"a" =>{
for a_type in AnalyzerType::iter(){
prompt_str += match a_type {
AnalyzerType::Energy => "e for energy analysis\n",
AnalyzerType::ParAxialRayTrace=> "p for paraxial ray-tracing analysis\n",
};
}
Ok(prompt_str)
},
"r" =>{
Ok(prompt_str + "Please insert a report directory or nothing to select the same directory as the optical-setup file\n")
},
_ => Err(OpossumError::Console("Invalid flag type! Cannot create prompt string!".into()))
}
}
fn get_analyzer_args(
func: fn(&str) -> Option<AnalyzerType>,
input: Option<String>,
arg_flag: &str,
) -> Result<AnalyzerType> {
match input {
Some(i) => {
let arg = func(&i);
if arg.is_none() {
let prompt_str = create_prompt_str(arg_flag, "Invalid input!\n")?;
let input: String = prompt_reply(prompt_str).unwrap();
get_analyzer_args(func, Some(input), arg_flag)
} else {
arg.ok_or(OpossumError::Console("Could not extract argument!".into()))
}
}
None => {
let prompt_str = create_prompt_str(arg_flag, "")?;
let input: String = prompt_reply(prompt_str).unwrap();
get_analyzer_args(func, Some(input), arg_flag)
}
}
}
fn get_path_args(
func: fn(&str) -> Option<PathBuf>,
input: Option<&str>,
arg_flag: &str,
) -> Result<PathBuf> {
match input {
Some(i) => {
let arg = func(i);
if arg.is_none() {
let prompt_str = create_prompt_str(arg_flag, "Invalid input!\n")?;
let input: String = prompt_reply(prompt_str).unwrap();
get_path_args(func, Some(&input), arg_flag)
} else {
arg.ok_or(OpossumError::Console("Could not extract argument!".into()))
}
}
None => {
let prompt_str = create_prompt_str(arg_flag, "")?;
let input: String = prompt_reply(prompt_str).unwrap();
get_path_args(func, Some(&input), arg_flag)
}
}
}
fn get_parent_dir(path: &Path) -> PathBuf {
let parent_dir = Path::parent(path).unwrap();
PathBuf::from(parent_dir)
}
impl TryFrom<PartialArgs> for Args {
type Error = OpossumError;
fn try_from(part_args: PartialArgs) -> Result<Args> {
let file_path = get_path_args(eval_file_path_input, part_args.file_path.as_deref(), "f")?;
println!("Path to optical-setup file: {}", file_path.display());
let analyzer = get_analyzer_args(eval_analyzer_input, part_args.analyzer, "a")?;
println!("Analyzer: {}", analyzer);
let report_directory = get_path_args(
eval_report_directory_input,
part_args.report_directory.as_deref(),
"r",
)?;
let report_directory = if report_directory.as_os_str().is_empty() {
get_parent_dir(&file_path)
} else {
report_directory
};
println!("Report directory: {}", report_directory.display());
Ok(Args {
file_path,
analyzer,
report_directory,
})
}
}
pub fn show_intro() {
println!(" .:^
:: ......:::^^^^:.. .:.
:!?Y55YJ?!^. ..:^^^^^^^^^^^^^^^^^^^^^^^^^^:..
.5#&&&&&&&&&&&&#P7. .:^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^:.. :!J5PGGBBGPY!:
~&&&&#GGB#&&&&&&&&&&P?^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^!P#&&&&&&&&&&&&&&B!
B&&&&&#G5YY5GB&&&&&&&&B7^^:^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^:^!P&&&&&&&&&&##B#&&&&&&J
B&&&&&&&&BBG5?7JPB&&&&&&#PJ!^^^^^^^^^^^^^^^^^^^^^^^^^^^^:^~!JP&&&&&&#BP5YJJYPB&&&&&&#
^&&&&&&&&5!!J5Y^:^~5&&&&&&&&BP?~^^^^^^^^^^^^^^^^^^^^:^~75G#&&&&&&&P77J5PB#&&&&&&&&&&B
!&&&&BPPGP~::^^^!JG&&&&&&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BP. !BBBY7!7YBBG^ :#B5 ~BBBG. .BBB
.!J5PP5Y7^ ?JY: .~J5PP5Y7^ 5BB#&@@~. :!Y5PP5Y7^ .~J5PPP5?~. .JJ7 ?JY~ JJJ
7PGBBBGPJ~.
.^!?5BBB!
^55J !BBG
5BBP!^:^7GBB!
^JPBBBBG5?.\n");
println!(
"{: ^119}",
"Opossum - Open-source Optics Simulation System and Unified Modeler"
);
let timestamp = DateTime::parse_from_rfc3339(env!("VERGEN_GIT_COMMIT_TIMESTAMP")).unwrap();
let version_string = format!(
" version {} ({})",
env!("VERGEN_GIT_DESCRIBE"),
timestamp.format("%Y/%m/%d %H:%M")
);
println!("{: ^119}\n", version_string);
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
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn file_path_is_valid_test() {
let path_valid = Path::new("./files_for_testing/CLI/opticscenery.opm");
let path_inexistent_file = Path::new("./files_for_testing/CLI/nonexistent.opm");
let path_inexistent_dir =
Path::new("./files_for_testing/this_dir_does_not_exist/empty.opm");
let path_not_yaml = Path::new("./files_for_testing/CLI/is_not_a_opm.txt");
let path_is_dir = Path::new("./files_for_testing/CLI/");
assert_eq!(file_path_is_valid(path_valid), true);
assert_eq!(file_path_is_valid(path_inexistent_file), false);
assert_eq!(file_path_is_valid(path_inexistent_dir), false);
assert_eq!(file_path_is_valid(path_not_yaml), false);
assert_eq!(file_path_is_valid(path_is_dir), false);
}
#[test]
fn eval_file_path_input_test() {
let path_valid = "./files_for_testing/CLI/opticscenery.opm";
let path_inexistent_file = "./files_for_testing/CLI/nonexistent.opm";
let path_inexistent_dir = "./files_for_testing/this_dir_does_not_exist/empty.opm";
let path_not_yaml = "./files_for_testing/CLI/is_not_an_opm.txt";
let path_is_dir = "./files_for_testing/CLI/";
assert_eq!(
eval_file_path_input(path_valid),
Some(PathBuf::from(path_valid))
);
assert_eq!(eval_file_path_input(path_inexistent_file), None);
assert_eq!(eval_file_path_input(path_inexistent_dir), None);
assert_eq!(eval_file_path_input(path_not_yaml), None);
assert_eq!(eval_file_path_input(path_is_dir), None);
}
#[test]
fn eval_analyzer_input_test() {
assert_eq!(eval_analyzer_input("e").unwrap(), AnalyzerType::Energy);
assert_eq!(
eval_analyzer_input("p").unwrap(),
AnalyzerType::ParAxialRayTrace
);
}
#[test]
fn eval_report_directory_input_test() {
let dir_valid = "./files_for_testing/CLI";
assert_eq!(eval_report_directory_input(""), Some(PathBuf::from("")));
assert_eq!(
eval_report_directory_input(dir_valid),
Some(PathBuf::from(dir_valid))
);
}
#[test]
fn get_parent_dir_test() {
let path_valid = "./files_for_testing/CLI/empty_yaml.yaml".to_owned();
assert_eq!(
get_parent_dir(&PathBuf::from(path_valid)),
PathBuf::from("./files_for_testing/CLI")
);
}
}