parse generate

master
Avril 4 years ago
parent 6b0f1c72be
commit 12f039849a
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -17,6 +17,7 @@ pub enum ParseErrorKind
CannotCombine(char, char), CannotCombine(char, char),
BadTail(char, String), BadTail(char, String),
NotUnique(char), NotUnique(char),
NotUniqueLong(Option<String>),
ModeSet, ModeSet,
} }
@ -26,7 +27,20 @@ impl fmt::Display for ParseErrorKind
{ {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result 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 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
{ {
match self { 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 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
{ {
match self { 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."), Error::AtomInternal => unreachable!("This should've been handled."),
_ => write!(f, "unknown error"), _ => write!(f, "unknown error"),
} }

@ -1,6 +1,7 @@
//! Parser for generate mode //! Parser for generate mode
use super::*; use super::*;
use tokio::sync::mpsc::Receiver; use tokio::sync::mpsc::Receiver;
use std::collections::HashSet;
#[instrument] #[instrument]
pub(super) async fn handle(mut rx: Receiver<Atom>) -> Result<config::Operation, error::ConstructError> pub(super) async fn handle(mut rx: Receiver<Atom>) -> Result<config::Operation, error::ConstructError>
@ -124,8 +125,14 @@ pub(super) async fn parse<I: IntoIterator<Item=String>>(args: I, atoms: &mut mps
} }
} }
let mut had = HashSet::new();
if reading && arg.starts_with("--") { if reading && arg.starts_with("--") {
let (key, value) = split(&arg); 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..] { match &key.to_lowercase().trim()[2..] {
"input" => send!(GenerateAtom::Input(take_one!("--input: Expected filename"))).await?, "input" => send!(GenerateAtom::Input(take_one!("--input: Expected filename"))).await?,
"password" if value.is_some() => { "password" if value.is_some() => {
@ -149,6 +156,12 @@ pub(super) async fn parse<I: IntoIterator<Item=String>>(args: I, atoms: &mut mps
_ => return Err((arg, error::ParseErrorKind::UnexpectedArg(None)).into()), _ => return Err((arg, error::ParseErrorKind::UnexpectedArg(None)).into()),
} }
} else { } else {
if had.contains(&arg[..]) {
return Err((arg, error::ParseErrorKind::NotUniqueLong(None)).into());
} else {
had.insert(arg.to_owned());
}
reading=false; reading=false;
match sent_output match sent_output
{ {
@ -161,6 +174,6 @@ pub(super) async fn parse<I: IntoIterator<Item=String>>(args: I, atoms: &mut mps
} }
todo!() Ok(())
} }

@ -11,12 +11,14 @@ use std::{
pub async fn find_file_mode<P: AsRef<Path>>(path: P) -> Result<config::op::Mode, Error> pub async fn find_file_mode<P: AsRef<Path>>(path: P) -> Result<config::op::Mode, Error>
{ {
//TODO: we need to calculate mode here //TODO: we need to calculate mode here
todo!() todo!()
} }
pub async fn find_key_mode<P: AsRef<Path>>(path: P) -> Result<config::KeyKind, Error> pub async fn find_key_mode<P: AsRef<Path>>(path: P) -> Result<config::KeyKind, Error>
{ {
//TODO: we need to calculate mode here //TODO: we need to calculate mode here
todo!() todo!()
} }
@ -24,7 +26,7 @@ pub async fn find_key_mode<P: AsRef<Path>>(path: P) -> Result<config::KeyKind, E
#[non_exhaustive] #[non_exhaustive]
pub enum Error pub enum Error
{ {
Unknown
} }
impl error::Error for Error{} impl error::Error for Error{}
@ -32,6 +34,8 @@ impl fmt::Display for Error
{ {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
{ {
Ok(()) match self {
_ => write!(f, "unknown error"),
}
} }
} }

Loading…
Cancel
Save