2024-05-10 15:55:24 +02:00
|
|
|
//! # numf
|
|
|
|
//!
|
|
|
|
//! This binary should just take any amount of numbers and print them out formatted to some other
|
|
|
|
//! system.
|
|
|
|
|
2024-05-13 16:26:35 +02:00
|
|
|
use std::io::Read;
|
|
|
|
use std::process::exit;
|
|
|
|
|
2024-05-13 16:55:39 +02:00
|
|
|
use clap::{CommandFactory, Parser};
|
2024-05-10 15:55:24 +02:00
|
|
|
|
2024-05-12 01:02:55 +02:00
|
|
|
mod format;
|
|
|
|
use format::*;
|
2024-05-13 16:55:39 +02:00
|
|
|
use numf::format::numf_parser;
|
2024-05-10 15:55:24 +02:00
|
|
|
|
2024-05-10 13:57:36 +02:00
|
|
|
fn main() {
|
2024-05-13 16:26:35 +02:00
|
|
|
// try to read from stdin first, appending the numbers we read to the FormatOptions
|
2024-05-13 16:55:39 +02:00
|
|
|
let mut options = FormatOptions::parse();
|
2024-05-13 16:26:35 +02:00
|
|
|
let mut stdin_nums = Vec::new();
|
|
|
|
match std::io::stdin().lock().read_to_end(&mut stdin_nums) {
|
|
|
|
Ok(_) => {
|
|
|
|
let whole: String = match String::from_utf8(stdin_nums) {
|
|
|
|
Ok(r) => r,
|
|
|
|
Err(e) => {
|
|
|
|
eprintln!("stdin for this program only accepts text: {e:#?}");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
};
|
2024-05-13 16:29:17 +02:00
|
|
|
let split = whole.split_whitespace();
|
|
|
|
for s in split {
|
2024-05-13 16:55:39 +02:00
|
|
|
let number = match numf_parser(s) {
|
|
|
|
Ok(n) => n,
|
|
|
|
Err(e) => {
|
|
|
|
eprintln!("could not parse number from stdin: {e:#?}");
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
options.push_number(number)
|
2024-05-13 16:26:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
eprintln!("could not read from stdin: {e:#?}");
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-05-13 16:55:39 +02:00
|
|
|
if options.numbers().is_empty() {
|
|
|
|
format!("{}", FormatOptions::command().render_usage());
|
|
|
|
exit(1);
|
|
|
|
}
|
2024-05-10 15:55:24 +02:00
|
|
|
|
|
|
|
let mut out: Vec<String> = Vec::new();
|
|
|
|
|
2024-05-12 19:53:38 +02:00
|
|
|
for num in options.numbers() {
|
|
|
|
out.push(options.format().format(*num, &options));
|
2024-05-10 15:55:24 +02:00
|
|
|
}
|
|
|
|
for o in out {
|
|
|
|
println!("{o}")
|
|
|
|
}
|
|
|
|
}
|