Browse Source

Comments, command line arguments

main
Unbewohnte 3 years ago
parent
commit
76494ed6af
  1. 9
      fumofiles/fumo.fumo
  2. 14
      src/fumo.rs
  3. 28
      src/main.rs

9
fumofiles/fumo.fumo

@ -1,6 +1,5 @@
__________
( !message )
----------
(!message)
()
()
\(ᗜˬᗜ)/
()
()
\(ᗜˬᗜ)/

14
src/fumo.rs

@ -1,8 +1,14 @@
/// Indicator of where the message should be in fumofile
pub const MESSAGE_INDICATOR: &str = "!message";
/// Fumofile name of the default fumo
pub const FUMO_DEFAULT: &str = "fumo.fumo";
pub fn sayify(fumofile: &mut String, message: &str) -> String {
if fumofile.contains(MESSAGE_INDICATOR) {
return fumofile.replace(MESSAGE_INDICATOR,message);
/// Returns a resulting string with `MESSAGE_INDICATOR` replaced with given
/// `message`. If `MESSAGE_INDICATOR` is not present in fumofile - the
/// `message` will be added on the new line at the end of the fumofile.
pub fn sayify(fumofile_contents: &mut String, message: &str) -> String {
if fumofile_contents.contains(MESSAGE_INDICATOR) {
return fumofile_contents.replace(MESSAGE_INDICATOR,message);
}
return format!("{}\n{}", fumofile, message);
return format!("{}\n{}", fumofile_contents, message);
}

28
src/main.rs

@ -1,7 +1,31 @@
mod fumo;
use std::path::Path;
use std::fs;
use std::env;
fn main() {
let mut fumofile_contents = std::fs::read_to_string(std::path::Path::new("./fumofiles/fumo.fumo")).unwrap();
let message: String = fumo::sayify(&mut fumofile_contents,"fumo");
// get command line arguments
let args: Vec<String> = env::args().collect();
// try to retrieve a message from args
if args.len() <= 1 {
// no message was provided
std::process::exit(1);
}
let clarg_message = &args[1..].join(" ");
// path to the fumofile
let fumofile_default_path = format!("./fumofiles/{}",fumo::FUMO_DEFAULT);
let fumofile_path = Path::new(&fumofile_default_path);
// read fumofile
let mut fumofile_contents = fs::read_to_string(fumofile_path).unwrap();
// parse the file and get the resulting string
let message: String = fumo::sayify(&mut fumofile_contents, clarg_message);
println!("{}", message);
}

Loading…
Cancel
Save