Skip to content

Commit

Permalink
feat: select char, help, version
Browse files Browse the repository at this point in the history
  • Loading branch information
dmyTRUEk committed Mar 23, 2024
1 parent 4f36026 commit 52afec2
Show file tree
Hide file tree
Showing 7 changed files with 258 additions and 18 deletions.
216 changes: 215 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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"
2 changes: 1 addition & 1 deletion generate_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -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("//"):
Expand Down
2 changes: 1 addition & 1 deletion src/characters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand Down
42 changes: 33 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Main

use clap::{arg, Parser};
use rand::{thread_rng, Rng};

// mod artbooks;
Expand All @@ -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 } = &QUOTES[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<String>,
}

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<String>) -> 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])
}
2 changes: 1 addition & 1 deletion src/quote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub struct Quote {

impl Quote {
pub const fn default() -> Self {
Self {
Self {
text: "default text",
char: Character::Unknown,
src: None,
Expand Down
8 changes: 4 additions & 4 deletions src/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
}
}

0 comments on commit 52afec2

Please sign in to comment.