diff --git a/src/args/error.rs b/src/args/error.rs index a56928e..b79a1cc 100644 --- a/src/args/error.rs +++ b/src/args/error.rs @@ -17,6 +17,7 @@ pub enum ParseErrorKind CannotCombine(char, char), BadTail(char, String), NotUnique(char), + NotUniqueLong(Option), ModeSet, } @@ -26,7 +27,20 @@ impl fmt::Display for ParseErrorKind { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - Ok(()) + match self { + Self::ExpectedArg(arg) => write!(f, "expected argument {:?}", arg), + Self::UnexpectedArg(None) => write!(f, "unknown argument"), + Self::UnexpectedArg(Some(arg)) => write!(f, "argument {:?} not expected here", arg), + Self::UnknownOption(ch) => write!(f, "unknown option `{}`", ch), + Self::UnknownKeyFormat(key) => write!(f, "unknown key format for {}", key), + Self::UnknownOptionFor(fo, o) => write!(f, "unknown option `{}` for `{}`", o, fo), + Self::CannotCombine(o1,o2) => write!(f, "cannot combine option `{}` with `{}`", o2, o1), + Self::BadTail(ch, ret) => write!(f, "unexpected tail {:?} for option `{}`", ret, ch), + Self::NotUnique(o) => write!(f, "option `{}` already specified", o), + Self::NotUniqueLong(None) => write!(f, "argument already specified"), + Self::NotUniqueLong(Some(opt)) => write!(f, "option {:?} already specified", opt), + Self::ModeSet => write!(f, "mode already set"), + } } } @@ -62,7 +76,10 @@ impl fmt::Display for ConstructError fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { - _ => write!(f, "unknown error"), + Self::NoGenerateKind => write!(f, "no key kind specified"), + Self::ExpectedNotPresent(name) => write!(f, "expected a {}, one was not provided", name), + Self::AlreadyExists(opt) => write!(f, "duplicate atom {}", opt), + Self::Resolve(_) => write!(f, "error resolving autodetect"), } } } @@ -92,6 +109,9 @@ impl fmt::Display for Error fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { + Error::Parse(arg, _) => write!(f, "error while parsing arg {:?}", arg), + Error::ExpectedKind(Some(got)) => write!(f, r#"expected a key kind ("AES" or "RSA"), got {:?}"#, got), + Error::ExpectedKind(None) => write!(f, "expected a key kind"), Error::AtomInternal => unreachable!("This should've been handled."), _ => write!(f, "unknown error"), } diff --git a/src/args/parse/generate.rs b/src/args/parse/generate.rs index a8139ce..1a4ef89 100644 --- a/src/args/parse/generate.rs +++ b/src/args/parse/generate.rs @@ -1,6 +1,7 @@ //! Parser for generate mode use super::*; use tokio::sync::mpsc::Receiver; +use std::collections::HashSet; #[instrument] pub(super) async fn handle(mut rx: Receiver) -> Result @@ -124,8 +125,14 @@ pub(super) async fn parse>(args: I, atoms: &mut mps } } + let mut had = HashSet::new(); if reading && arg.starts_with("--") { let (key, value) = split(&arg); + if had.contains(&key[..]) { + return Err((error::ParseErrorKind::NotUniqueLong(Some(key.to_owned())), arg).swap().into()); + } else { + had.insert(key.to_owned()); + } match &key.to_lowercase().trim()[2..] { "input" => send!(GenerateAtom::Input(take_one!("--input: Expected filename"))).await?, "password" if value.is_some() => { @@ -149,6 +156,12 @@ pub(super) async fn parse>(args: I, atoms: &mut mps _ => return Err((arg, error::ParseErrorKind::UnexpectedArg(None)).into()), } } else { + + if had.contains(&arg[..]) { + return Err((arg, error::ParseErrorKind::NotUniqueLong(None)).into()); + } else { + had.insert(arg.to_owned()); + } reading=false; match sent_output { @@ -161,6 +174,6 @@ pub(super) async fn parse>(args: I, atoms: &mut mps } - todo!() + Ok(()) } diff --git a/src/resolve.rs b/src/resolve.rs index c90e3e2..58bfc97 100644 --- a/src/resolve.rs +++ b/src/resolve.rs @@ -11,12 +11,14 @@ use std::{ pub async fn find_file_mode>(path: P) -> Result { //TODO: we need to calculate mode here + todo!() } pub async fn find_key_mode>(path: P) -> Result { //TODO: we need to calculate mode here + todo!() } @@ -24,7 +26,7 @@ pub async fn find_key_mode>(path: P) -> Result) -> fmt::Result { - Ok(()) + match self { + _ => write!(f, "unknown error"), + } } }