Skip to content

Commit

Permalink
Merge pull request #9 from cafkafk/cafk-env-var
Browse files Browse the repository at this point in the history
feat(env): introduce FORTUNE_DIR env var
  • Loading branch information
cafkafk authored Oct 6, 2023
2 parents 93e5c29 + 945177a commit d4556db
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 4 deletions.
2 changes: 1 addition & 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
Expand Up @@ -4,10 +4,12 @@ description = "Fortune, but kind-a better"
authors = ["Christina Sørensen <christina@cafkafk.com>"]
categories = ["command-line-utilities"]
edition = "2021"
# Files to be excluded on `cargo publish`
exclude = [ "/oldtunes" ]
rust-version = "1.65.0"
readme = "README.md"
license = "GPL-3.0-only"
version = "0.1.0"
version = "0.1.1"

[dependencies]
clap = { version = "4.4.4", features = ["cargo"] }
Expand Down
55 changes: 53 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,61 @@ pub mod fortune {
use crate::file;
use crate::random;

use std::env;
use std::process::exit;

/// The default maximum length for a short quote.
const SHORT: usize = 150;

/// The default place to look for fortunes
const FORTUNE_DIR: &str = "fortunes";

/// The default place to look for off-color fortunes
const FORTUNE_OFF_DIR: &str = "fortunes_off";

fn get_fortune_dir() -> String {
match env::var("FORTUNE_DIR") {
Ok(val) => val,
Err(_) => FORTUNE_DIR.to_string(),
}
}

fn get_fortune_off_dir() -> String {
match env::var("FORTUNE_OFF_DIR") {
Ok(val) => val,
Err(_) => FORTUNE_OFF_DIR.to_string(),
}
}

// TODO: refactor
fn handle_file_errors(
input: String,
f: &dyn Fn(String) -> Result<String, Box<dyn std::error::Error>>,
) -> String {
use std::io::ErrorKind;
match f(input.clone()) {
Ok(val) => val,
Err(e) => {
if let Some(io_err) = e.downcast_ref::<std::io::Error>() {
match io_err {
err if io_err.kind() == ErrorKind::NotFound => {
eprintln!("{err}");
println!("Couldn't find \"{input}\", make sure you set FORTUNE_DIR correctly, or verify that you're in a directory with a folder named \"{input}\".",);
std::process::exit(1);
}
&_ => panic!("{e:?}"),
}
}
panic!("{e:?}")
}
}
}

pub fn search_fortunes(pattern: &str) {
let files = file::read_all_files("fortunes").unwrap();
let fortune_dir = get_fortune_dir();

// TODO: Handle your errors!
let files = file::read_all_files(&fortune_dir).unwrap();
for file in files {
let fortune: Option<&str> = file.split("\n%\n").find(|x| x.contains(pattern));
if let Some(fortune) = fortune {
Expand Down Expand Up @@ -150,7 +198,10 @@ pub mod fortune {
/// get_quote(&255); // Prints a humorous message and exits.
/// ```
pub fn get_quote(quote_size: &u8) {
let file = file::pick_file("fortunes".to_string()).unwrap();
let fortune_dir = get_fortune_dir();

let file = handle_file_errors(fortune_dir, &file::pick_file);

let quotes: Vec<&str> = file.split("\n%\n").collect();

let mut tmp = vec![];
Expand Down

0 comments on commit d4556db

Please sign in to comment.