Skip to content

Commit

Permalink
mommy looooves serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
VixieTSQ authored and Gankra committed Dec 22, 2022
1 parent 2e79f44 commit 8867d04
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 18 deletions.
80 changes: 80 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Expand Up @@ -10,3 +10,5 @@ edition = "2021"

[dependencies]
rand = { version = "0.8.5" }
serde = { version = "1.0.151", features = ["derive"] }
serde_json = "1.0.91"
10 changes: 10 additions & 0 deletions responses.json
@@ -0,0 +1,10 @@
{
"positive": [
"Good AFFECTIONATE_TERM~\nMommy's so proud of you~ ❤️",
"Awe, what a good AFFECTIONATE_TERM~\nMommy knew you could do it~ ❤️"
],
"negative": [
"Mommy knows her little AFFECTIONATE_TERM can do better~ ❤️",
"Just a little further, sweetie~ ❤️"
]
}
2 changes: 0 additions & 2 deletions responses/negative-responses.txt

This file was deleted.

2 changes: 0 additions & 2 deletions responses/positive-responses.txt

This file was deleted.

42 changes: 28 additions & 14 deletions src/main.rs
@@ -1,13 +1,23 @@
use rand::{rngs::StdRng, seq::IteratorRandom, SeedableRng};
use rand::{rngs::StdRng, seq::SliceRandom, SeedableRng};
use serde::Deserialize;
use std::process::ExitCode;

const NEGATIVE_RESPONSES: &str = include_str!("../responses/negative-responses.txt");
const POSITIVE_RESPONSES: &str = include_str!("../responses/positive-responses.txt");
const AFFECTIONATE_TERM_PLACEHOLDER: &str = "AFFECTIONATE_TERM";
const RESPONSES: &'static str = include_str!("../responses.json");
const AFFECTIONATE_TERM_PLACEHOLDER: &'static str = "AFFECTIONATE_TERM";

#[derive(Deserialize)]
struct Responses {
positive: Vec<String>,
negative: Vec<String>,
}

enum ResponseType {
Positive,
Negative,
}

fn main() -> Result<ExitCode, Box<dyn std::error::Error>> {
let cargo = std::env::var("CARGO")?;
let mommys_little = std::env::var("CARGO_MOMMYS_LITTLE").unwrap_or_else(|_| "girl".to_owned());
let mut arg_iter = std::env::args();
let _cargo = arg_iter.next();
let _mommy = arg_iter.next();
Expand All @@ -17,23 +27,27 @@ fn main() -> Result<ExitCode, Box<dyn std::error::Error>> {
let status = cmd.status()?;
eprintln!("\x1b[1m");
if status.success() {
eprintln!("{}", select_response(POSITIVE_RESPONSES, mommys_little))
eprintln!("{}", select_response(ResponseType::Positive))
} else {
eprintln!("{}", select_response(NEGATIVE_RESPONSES, mommys_little));
eprintln!("{}", select_response(ResponseType::Negative));
}
eprintln!("\x1b[0m");
Ok(ExitCode::from(status.code().unwrap_or(-1) as u8))
}

fn select_response(responses: &str, affectionate_term: String) -> String {
fn select_response(response_type: ResponseType) -> String {
let affectionate_term =
std::env::var("CARGO_MOMMYS_LITTLE").unwrap_or_else(|_| "girl".to_owned());
let mut rng = StdRng::from_entropy();
let response = responses
.lines()
.choose(&mut rng)
.expect("non-zero amount of responses")
.replace(AFFECTIONATE_TERM_PLACEHOLDER, &affectionate_term);
let responses: Responses = serde_json::from_str(RESPONSES).expect("RESPONSES to be valid JSON");

return response;
return match response_type {
ResponseType::Positive => responses.positive,
ResponseType::Negative => responses.negative,
}
.choose(&mut rng)
.expect("non-zero amount of responses")
.replace(AFFECTIONATE_TERM_PLACEHOLDER, &affectionate_term);
}

#[cfg(test)]
Expand Down

0 comments on commit 8867d04

Please sign in to comment.