Skip to content

Commit

Permalink
made getList save to file option & improved word_type checking
Browse files Browse the repository at this point in the history
  • Loading branch information
cqb13 committed Feb 27, 2024
1 parent 21b1c38 commit 3c3384a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 21 deletions.
1 change: 0 additions & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ impl<'a> Command<'a> {
/**
* Adds arguments to the command
*/

pub fn with_args(mut self, args: &Vec<Arg>) -> Command<'a> {
if self.args.is_none() {
self.args = Some(vec![]);
Expand Down
21 changes: 1 addition & 20 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,26 +181,7 @@ fn main() {
let display = command.has("display");
let to = command.get_value_of("to");

if type_of_words != "english"
&& type_of_words != "latin"
&& type_of_words != "inflections"
&& type_of_words != "inflection"
&& type_of_words != "not_packons"
&& type_of_words != "not_packon"
&& type_of_words != "packon"
&& type_of_words != "packons"
&& type_of_words != "prefixes"
&& type_of_words != "prefix"
&& type_of_words != "stems"
&& type_of_words != "stem"
&& type_of_words != "suffixes"
&& type_of_words != "suffix"
&& type_of_words != "tackons"
&& type_of_words != "tackon"
&& type_of_words != "tickons"
&& type_of_words != "tickon"
&& type_of_words != "unique_latin"
{
if !WordType::is_valid_word_type(&type_of_words) {
println!(
"Invalid type of words. Please use `help` to see the available types of words."
);
Expand Down
15 changes: 15 additions & 0 deletions src/use_data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ impl WordType {
_ => Err(format!("Invalid word type: {}", s)),
}
}

pub fn is_valid_word_type(s: &str) -> bool {
match s {
"english" | "latin" | "inflections" | "inflection" | "not_packons" | "not_packon"
| "packon" | "packons" | "prefixes" | "prefix" | "stems" | "stem" | "suffixes"
| "suffix" | "tackons" | "tackon" | "tickons" | "tickon" | "unique_latin" => true,
_ => false,
}
}
}

#[derive(Debug, Serialize)]
Expand Down Expand Up @@ -141,4 +150,10 @@ pub fn get_list(
if display {
println!("{}", serde_json::to_string_pretty(&list).unwrap());
}

if to.is_some() {
let file_path = to.unwrap();
let file = std::fs::File::create(file_path).unwrap();
serde_json::to_writer_pretty(file, &list).unwrap();
}
}

0 comments on commit 3c3384a

Please sign in to comment.