From 52afec2c7fe8f208728a9828061f93a6d27388fc Mon Sep 17 00:00:00 2001 From: dmyTRUEk <25669613+dmyTRUEk@users.noreply.github.com> Date: Sat, 23 Mar 2024 10:45:37 +0200 Subject: [PATCH] feat: select char, help, version --- Cargo.lock | 216 +++++++++++++++++++++++++++++++++++++++++++++- Cargo.toml | 4 +- generate_enum.py | 2 +- src/characters.rs | 2 +- src/main.rs | 42 +++++++-- src/quote.rs | 2 +- src/source.rs | 8 +- 7 files changed, 258 insertions(+), 18 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 93097da..41c6afd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,16 +2,111 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "anstream" +version = "0.6.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d96bd03f33fe50a863e394ee9718a706f988b9079b20c3784fb726e7678b62fb" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8901269c6307e8d93993578286ac0edf7f195079ffff5ebdeea6a59ffb7e36bc" + +[[package]] +name = "anstyle-parse" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c75ac65da39e5fe5ab759307499ddad880d724eed2f6ce5b5e8a26f4f387928c" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e28923312444cdd728e4738b3f9c9cac739500909bb3d3c94b43551b16517648" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1cd54b81ec8d6180e24654d0b371ad22fc3dd083b6ff8ba325b72e00c87660a7" +dependencies = [ + "anstyle", + "windows-sys", +] + [[package]] name = "cfg-if" version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "clap" +version = "4.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "949626d00e063efc93b6dca932419ceb5432f99769911c0b995f7e884c778813" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae129e2e766ae0ec03484e609954119f123cc1fe650337e155d03b022f24f7b4" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90239a040c80f5e14809ca132ddc4176ab33d5e17e49691793296e3fcb34d72f" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "98cc8fbded0c607b7ba9dd60cd98df59af97e84d24e49c8557331cfc26d301ce" + +[[package]] +name = "colorchoice" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" + [[package]] name = "gensoquote" -version = "0.2.0" +version = "0.3.0" dependencies = [ + "clap", "rand", ] @@ -26,6 +121,12 @@ dependencies = [ "wasi", ] +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + [[package]] name = "libc" version = "0.2.153" @@ -38,6 +139,24 @@ version = "0.2.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" +[[package]] +name = "proc-macro2" +version = "1.0.79" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e835ff2298f5721608eb1a980ecaee1aef2c132bf95ecc026a11b7bf3c01c02e" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef" +dependencies = [ + "proc-macro2", +] + [[package]] name = "rand" version = "0.8.5" @@ -68,8 +187,103 @@ dependencies = [ "getrandom", ] +[[package]] +name = "strsim" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ee073c9e4cd00e28217186dbe12796d692868f432bf2e97ee73bed0c56dfa01" + +[[package]] +name = "syn" +version = "2.0.53" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7383cd0e49fff4b6b90ca5670bfd3e9d6a733b3f90c686605aa7eec8c4996032" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" + +[[package]] +name = "utf8parse" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" + [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.52.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7dd37b7e5ab9018759f893a1952c9420d060016fc19a472b4bb20d1bdd694d1b" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bcf46cf4c365c6f2d1cc93ce535f2c8b244591df96ceee75d8e83deb70a9cac9" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da9f259dd3bcf6990b55bffd094c4f7235817ba4ceebde8e6d11cd0c5633b675" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b474d8268f99e0995f25b9f095bc7434632601028cf86590aea5c8a5cb7801d3" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1515e9a29e5bed743cb4415a9ecf5dfca648ce85ee42e15873c3cd8610ff8e02" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5eee091590e89cc02ad514ffe3ead9eb6b660aedca2183455434b93546371a03" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77ca79f2451b49fa9e2af39f0747fe999fcda4f5e241b2898624dca97a1f2177" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32b752e52a2da0ddfbdbcc6fceadfeede4c939ed16d13e648833a61dfb611ed8" diff --git a/Cargo.toml b/Cargo.toml index f306de7..9a0f953 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,8 @@ [package] name = "gensoquote" -version = "0.2.0" +version = "0.3.0" edition = "2021" +authors = ["Myshko Dm"] # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html @@ -16,4 +17,5 @@ strip = true #target-cpu = "native" -> RUSTFLAGS='-C target-cpu=native' [dependencies] +clap = { version = "4.5.3", features = ["derive"] } rand = "0.8.5" diff --git a/generate_enum.py b/generate_enum.py index d50b971..6044e0f 100644 --- a/generate_enum.py +++ b/generate_enum.py @@ -28,7 +28,7 @@ def main(): output.append("use crate::to_str::ToStr;") output.append("") output.append("#[allow(non_camel_case_types, dead_code)]") - output.append("#[derive(Debug)]") + output.append("#[derive(Debug, Clone, Copy)]") output.append(f"pub enum {enum_name_camelcase} {{") for line in lines: if line.startswith("//"): diff --git a/src/characters.rs b/src/characters.rs index 2d16914..862cceb 100644 --- a/src/characters.rs +++ b/src/characters.rs @@ -8,7 +8,7 @@ use crate::to_str::ToStr; #[allow(non_camel_case_types, dead_code)] -#[derive(Debug)] +#[derive(Debug, Clone, Copy)] pub enum Character { Unknown, diff --git a/src/main.rs b/src/main.rs index bb0e2e7..1468e3f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ //! Main +use clap::{arg, Parser}; use rand::{thread_rng, Rng}; // mod artbooks; @@ -12,17 +13,40 @@ mod to_str; use crate::{quote::Quote, quotes::QUOTES, to_str::ToStr}; -fn main() { - let random_quote_index: usize = get_random_quote_index(); - let Quote { text, char, src, whom_about, whom_to } = "ES[random_quote_index]; - let char: &'static str = char.to_str(); - let maybe_to = if let Some(whom_to) = whom_to { format!(" to {}", whom_to.to_str()) } else { format!("") }; - let maybe_about = if let Some(whom_about) = whom_about { format!(" about {}", whom_about.to_str()) } else { format!("") }; - let maybe_src = if let Some(src) = src { format!(", \"{}\"", src)} else { format!("") }; +#[derive(Parser, Debug)] +#[command(about, version, long_about = None, author)] +struct CliArgs { + /// Name of the character + #[arg(short, long)] + character: Option, +} + +fn main() -> Result<(), &'static str> { + let cli_args = CliArgs::parse(); + let Quote { text, char, src, whom_about, whom_to } = get_random_quote(cli_args.character)?; + let char = char.to_str(); + let maybe_to = whom_to + .map(|whom_to| format!(" to {}", whom_to.to_str())) + .unwrap_or_default(); + let maybe_about = whom_about + .map(|whom_about| format!(" about {}", whom_about.to_str())) + .unwrap_or_default(); + let maybe_src = src + .map(|src| format!(", \"{}\"", src)) + .unwrap_or_default(); println!("\"{text}\"\n-- {char}{maybe_to}{maybe_about}{maybe_src}"); + Ok(()) } -fn get_random_quote_index() -> usize { +fn get_random_quote(char: Option) -> Result<&'static Quote, &'static str> { + let quotes: Vec<&Quote> = match char { + None => QUOTES.iter().collect(), + Some(char) => QUOTES.iter() + .filter(|quote| quote.char.to_str().to_lowercase().contains(&char.to_lowercase())) + .collect() + }; + if quotes.len() == 0 { return Err("Character or her quote not found.") } let mut rng = thread_rng(); - rng.gen_range(0..QUOTES.len()) + let random_quote_index: usize = rng.gen_range(0..quotes.len()); + Ok(quotes[random_quote_index]) } diff --git a/src/quote.rs b/src/quote.rs index ec6fcc5..b8fafbe 100644 --- a/src/quote.rs +++ b/src/quote.rs @@ -13,7 +13,7 @@ pub struct Quote { impl Quote { pub const fn default() -> Self { - Self { + Self { text: "default text", char: Character::Unknown, src: None, diff --git a/src/source.rs b/src/source.rs index 2025d72..5aac5df 100644 --- a/src/source.rs +++ b/src/source.rs @@ -10,9 +10,9 @@ pub enum Source { impl ToStr for Source { fn to_str(&self) -> &str { - match self { - Self::Game(game) => game.to_str(), - Self::Artbook(artbook) => artbook.to_str(), - } + match self { + Self::Game(game) => game.to_str(), + Self::Artbook(artbook) => artbook.to_str(), + } } }