From fc1cb1acc0dce412e948475002666bcd1d4b0348 Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Mon, 31 Oct 2022 22:41:18 +0100 Subject: add first impl --- src/input.rs | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 src/input.rs (limited to 'src/input.rs') diff --git a/src/input.rs b/src/input.rs new file mode 100644 index 0000000..689389e --- /dev/null +++ b/src/input.rs @@ -0,0 +1,59 @@ +use std::io::{stdin, Error, ErrorKind}; +use crate::{add, config::Cfg}; + +pub struct Input { + pub command: String, + pub args: Option>, +} + +impl Input { + pub fn from(string: String) -> Result> { + let mut split: Vec<&str> = string.split(' ').collect(); + + let command: String; + let mut args: Option> = None; + + if split[0].is_empty() { split.remove(0); }; + + dbg!(&split); + + match split.len() { + 0 => { Err(Box::new(Error::new(ErrorKind::InvalidInput, "NO_ARGS"))) } + 1 => Ok( Input { command: split[0].to_string(), args }), + 2.. => { + command = split[0].to_string(); + split.remove(0); + let mut str_args: Vec = vec![]; + for e in split { + str_args.push(e.to_string()); + } + args = Some(str_args); + Ok(Input { command, args }) + }, + _ => { panic!("This should never happen") } + } + + + } +} + +pub async fn get_input(config: Cfg) -> Result<(), Box> { + let mut user_input = String::new(); + stdin() + .read_line(&mut user_input) + .expect("ERROR"); + + dbg!(&user_input); + + let input = Input::from(user_input.trim().to_string())?; + + match input.command.as_str() { + "add" => { + if input.args == None { return Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_FEW_ARGUMENTS"))) }; + if input.args.as_ref().unwrap().len() != 1 { return Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_MANY_ARGUMENTS"))) }; + add(config, input.args.unwrap()[0].to_string()).await?; + Ok(()) + }, + _ => Err(Box::new(Error::new(ErrorKind::InvalidInput, "UNKNOWN_COMMAND"))), + } +} -- cgit v1.2.3