Browse Source

Ran 'cargo fmt'

main
Unbewohnte 3 years ago
parent
commit
8f8ee5eb6e
  1. 77
      src/main.rs

77
src/main.rs

@ -1,18 +1,18 @@
use clap::{App, Arg};
use std::fs; use std::fs;
use std::path::Path; use std::path::Path;
use clap::{Arg, App};
/// Indicator of where the message should be in fumofile /// Indicator of where the message should be in fumofile
const MESSAGE_INDICATOR: &str = "!message"; const MESSAGE_INDICATOR: &str = "!message";
/// Fumofile name of the default fumo /// Fumofile name of the default fumo
const FUMO_DEFAULT: &str = "cirno.fumo"; const FUMO_DEFAULT: &str = "cirno.fumo";
/// Default directory where fumofiles are placed /// Default directory where fumofiles are placed
const DEFAULT_FUMOFILES_DIR: &str = "/usr/local/share/fumosay/fumofiles/"; const DEFAULT_FUMOFILES_DIR: &str = "/usr/local/share/fumosay/fumofiles/";
/// Returns a resulting string with `MESSAGE_INDICATOR` replaced with given /// Returns a resulting string with `MESSAGE_INDICATOR` replaced with given
/// `message`. If `MESSAGE_INDICATOR` is not present in fumofile - the /// `message`. If `MESSAGE_INDICATOR` is not present in fumofile - the
/// `message` will be added on the new line at the end of the fumofile. /// `message` will be added on the new line at the end of the fumofile.
fn process_message(fumofile_contents: &mut String, message: &str) -> String { fn process_message(fumofile_contents: &mut String, message: &str) -> String {
if fumofile_contents.contains(MESSAGE_INDICATOR) { if fumofile_contents.contains(MESSAGE_INDICATOR) {
return fumofile_contents.replace(MESSAGE_INDICATOR, message); return fumofile_contents.replace(MESSAGE_INDICATOR, message);
} }
@ -27,46 +27,48 @@ fn main() {
.about("cowsay, but with soft friends") .about("cowsay, but with soft friends")
.arg( .arg(
Arg::with_name("fumo") Arg::with_name("fumo")
.short("f") .short("f")
.long("fumo") .long("fumo")
.value_name("fumofile") .value_name("fumofile")
.help("Choose another fumofumo to print") .help("Choose another fumofumo to print")
.takes_value(true) .takes_value(true)
.required(false) .required(false)
.default_value(FUMO_DEFAULT) .default_value(FUMO_DEFAULT),
) )
.arg( .arg(
Arg::with_name("fumofiles_directory") Arg::with_name("fumofiles_directory")
.short("d") .short("d")
.long("fumofiles_directory") .long("fumofiles_directory")
.value_name("fumofiles_directory_path") .value_name("fumofiles_directory_path")
.help("Look for fumofiles in given directory") .help("Look for fumofiles in given directory")
.takes_value(true) .takes_value(true)
.required(false) .required(false)
.default_value(DEFAULT_FUMOFILES_DIR) .default_value(DEFAULT_FUMOFILES_DIR),
) )
.arg( .arg(
Arg::with_name("list_fumos") Arg::with_name("list_fumos")
.short("l") .short("l")
.long("list_fumos") .long("list_fumos")
.help("Lists all fumofiles in the default directory instead") .help("Lists all fumofiles in the default directory instead")
.takes_value(false) .takes_value(false)
.required(false) .required(false),
) )
.arg( .arg(
Arg::with_name("message") Arg::with_name("message")
.short("m") .short("m")
.long("message") .long("message")
.help("Message to print") .help("Message to print")
.takes_value(true) .takes_value(true)
.index(1) .index(1)
.multiple(true) .multiple(true),
).get_matches(); )
.get_matches();
// check for list_fumos flag. // check for list_fumos flag.
if matches.is_present("list_fumos") { if matches.is_present("list_fumos") {
// list fumofiles and exit // list fumofiles and exit
let fumofiles = fs::read_dir(DEFAULT_FUMOFILES_DIR).expect("Could not read default fumofiles directory"); let fumofiles = fs::read_dir(DEFAULT_FUMOFILES_DIR)
.expect("Could not read default fumofiles directory");
for entry in fumofiles { for entry in fumofiles {
let file = entry.unwrap(); let file = entry.unwrap();
let filename = file.file_name(); let filename = file.file_name();
@ -76,17 +78,18 @@ fn main() {
} }
// process fumofiles directory // process fumofiles directory
// directory with all fumofiles // directory with all fumofiles
let new_fumofiles_dir = matches.value_of("fumofiles_directory").unwrap(); let new_fumofiles_dir = matches.value_of("fumofiles_directory").unwrap();
// name of the fumofile to work with // name of the fumofile to work with
let fumofile_name = matches.value_of("fumo").unwrap(); let fumofile_name = matches.value_of("fumo").unwrap();
// the whole path to the selected fumo // the whole path to the selected fumo
let fumofile_path = Path::new(&new_fumofiles_dir).join(fumofile_name); let fumofile_path = Path::new(&new_fumofiles_dir).join(fumofile_name);
// read fumofile // read fumofile
let mut fumofile_contents: String = fs::read_to_string(fumofile_path).expect("Could not find a fumofile!"); let mut fumofile_contents: String =
fs::read_to_string(fumofile_path).expect("Could not find a fumofile!");
// process the message // process the message
let message_values = matches.values_of("message").expect("No message provided"); let message_values = matches.values_of("message").expect("No message provided");
let message_vec: Vec<&str> = message_values.collect(); let message_vec: Vec<&str> = message_values.collect();
@ -94,8 +97,6 @@ fn main() {
// parse the file, insert fiven message and get the resulting string // parse the file, insert fiven message and get the resulting string
let fumosay: String = process_message(&mut fumofile_contents, joined_message.as_str()); let fumosay: String = process_message(&mut fumofile_contents, joined_message.as_str());
println!("{}", fumosay); println!("{}", fumosay);
} }

Loading…
Cancel
Save