parsing normal works

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

24
Cargo.lock generated

@ -15,6 +15,15 @@ version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ee2a4ec343196209d6594e19543ae87a39f96d5534d7174822a3ad825dd6ed7e" checksum = "ee2a4ec343196209d6594e19543ae87a39f96d5534d7174822a3ad825dd6ed7e"
[[package]]
name = "ansi_term"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b"
dependencies = [
"winapi 0.3.9",
]
[[package]] [[package]]
name = "ansi_term" name = "ansi_term"
version = "0.12.1" version = "0.12.1"
@ -119,10 +128,23 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "22ba9b5e817f1bc1f2219b5a3474b69f838321b2a2ba8860d6a71c3bfe3d0fc1" checksum = "22ba9b5e817f1bc1f2219b5a3474b69f838321b2a2ba8860d6a71c3bfe3d0fc1"
dependencies = [ dependencies = [
"backtrace", "backtrace",
"color-spantrace",
"eyre", "eyre",
"indenter", "indenter",
"once_cell", "once_cell",
"owo-colors", "owo-colors",
"tracing-error",
]
[[package]]
name = "color-spantrace"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a99aa4aa18448eef4c7d3f86d2720d2d8cad5c860fe9ff9b279293efdc8f5be"
dependencies = [
"ansi_term 0.11.0",
"tracing-core",
"tracing-error",
] ]
[[package]] [[package]]
@ -966,7 +988,7 @@ version = "0.2.12"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "82bb5079aa76438620837198db8a5c529fb9878c730bc2b28179b0241cf04c10" checksum = "82bb5079aa76438620837198db8a5c529fb9878c730bc2b28179b0241cf04c10"
dependencies = [ dependencies = [
"ansi_term", "ansi_term 0.12.1",
"chrono", "chrono",
"lazy_static", "lazy_static",
"matchers", "matchers",

@ -7,7 +7,7 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
color-eyre = {version = "0.5.3", default-features =false} color-eyre = "0.5.3"
lazy_static = "1.4.0" lazy_static = "1.4.0"
cryptohelpers = {version = "1.1.2", features=["full", "async", "serialise"]} cryptohelpers = {version = "1.1.2", features=["full", "async", "serialise"]}
cfg-if = "0.1.10" cfg-if = "0.1.10"

@ -57,6 +57,7 @@ impl From<(String, ParseErrorKind)> for Error
#[non_exhaustive] #[non_exhaustive]
pub enum ConstructError pub enum ConstructError
{ {
Dropped,
NoGenerateKind, NoGenerateKind,
ExpectedNotPresent(&'static str), ExpectedNotPresent(&'static str),
AlreadyExists(String), AlreadyExists(String),
@ -80,6 +81,7 @@ impl fmt::Display for ConstructError
Self::ExpectedNotPresent(name) => write!(f, "expected a {}, one was not provided", name), Self::ExpectedNotPresent(name) => write!(f, "expected a {}, one was not provided", name),
Self::AlreadyExists(opt) => write!(f, "duplicate atom {}", opt), Self::AlreadyExists(opt) => write!(f, "duplicate atom {}", opt),
Self::Resolve(_) => write!(f, "error resolving autodetect"), Self::Resolve(_) => write!(f, "error resolving autodetect"),
Self::Dropped => panic!("mpsc fatal: dropped rx shoudl've been handled not reported"),
} }
} }
} }

@ -43,8 +43,8 @@ OPTIONS:
Other options for `-k` Other options for `-k`
-p Read a password from stdin for this key -p Read a password from stdin for this key
-P <password> The next argument after the key file is a password for the key -P <password> The next argument after the key file is a password for the key
-s This key is also a signing key -s This key is also a signing key (or verifying for decrypt)
-S This key is only a signing key -S This key is only a signing key (or verifying for decrypt, will fail on bad verification)
EXAMPLE: EXAMPLE:
{program} -kRP key.rsa "super secret password" -ka key.aes -te input_file output_encrypted file {program} -kRP key.rsa "super secret password" -ka key.aes -te input_file output_encrypted file

@ -3,7 +3,7 @@ use super::*;
use tokio::sync::mpsc::Receiver; use tokio::sync::mpsc::Receiver;
use std::collections::HashSet; use std::collections::HashSet;
#[instrument] #[instrument(level="trace", err, skip(rx))]
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>
{ {
let mut had = smallmap::Map::new(); let mut had = smallmap::Map::new();
@ -37,7 +37,10 @@ pub(super) async fn handle(mut rx: Receiver<Atom>) -> Result<config::Operation,
} }
} }
let mut stage = Output{ let mut stage = Output{
kind: Some(match rx.recv().await.unwrap() { kind: Some(match match rx.recv().await {
Some(val) => val,
None => return Err(error::ConstructError::Dropped),
} {
Atom::Generate(GenerateAtom::Type(ty)) => ty, Atom::Generate(GenerateAtom::Type(ty)) => ty,
_ => invalidate!("First atom is not type."), _ => invalidate!("First atom is not type."),
}), }),
@ -91,8 +94,8 @@ fn split<'a>(input: &'a str) -> (&'a str, Option<&'a str>)
} }
} }
#[instrument(level="trace", skip(args, atoms))]
pub(super) async fn parse<I: IntoIterator<Item=String>>(args: I, atoms: &mut mpsc::Sender<Atom>) -> Result<(), error::Error> pub(super) async fn parse<I: IntoIterator<Item=String>>(args: I, mut atoms: mpsc::Sender<Atom>) -> Result<(), error::Error>
{ {
let mut args = args.into_iter(); let mut args = args.into_iter();
let mut reading = true; let mut reading = true;
@ -112,8 +115,9 @@ pub(super) async fn parse<I: IntoIterator<Item=String>>(args: I, atoms: &mut mps
_ => return Err(error::Error::ExpectedKind(arg)), _ => return Err(error::Error::ExpectedKind(arg)),
} }
}; };
let mut had = HashSet::new();
let mut hadv = HashSet::new();
while let Some(arg) = args.next() while let Some(arg) = args.next()
{ {
macro_rules! take_one { macro_rules! take_one {
@ -124,15 +128,12 @@ 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[..]) { if !had.insert(key.to_owned()) {
return Err((error::ParseErrorKind::NotUniqueLong(Some(key.to_owned())), arg).swap().into()); 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() => {
@ -155,12 +156,12 @@ pub(super) async fn parse<I: IntoIterator<Item=String>>(args: I, atoms: &mut mps
})).await?, })).await?,
_ => return Err((arg, error::ParseErrorKind::UnexpectedArg(None)).into()), _ => return Err((arg, error::ParseErrorKind::UnexpectedArg(None)).into()),
} }
} else if reading && arg == "-" {
reading= false;
} else { } else {
if had.contains(&arg[..]) { if !hadv.insert(arg.to_owned()) {
return Err((arg, error::ParseErrorKind::NotUniqueLong(None)).into()); return Err((arg, error::ParseErrorKind::NotUniqueLong(None)).into());
} else {
had.insert(arg.to_owned());
} }
reading=false; reading=false;
match sent_output match sent_output

@ -153,10 +153,10 @@ impl Atom
} }
} }
#[instrument] #[instrument(level="debug", err, skip(args))]
pub async fn parse<I: IntoIterator<Item=String> + std::fmt::Debug>(args: I) -> Result<config::Operation, eyre::Report> pub async fn parse<I: IntoIterator<Item=String> + std::fmt::Debug>(args: I) -> Result<config::Operation, eyre::Report>
{ {
let (mut tx, rx) = mpsc::channel(1); let (tx, rx) = mpsc::channel(1);
macro_rules! unwrap_handler { macro_rules! unwrap_handler {
($handler:expr) => ($handler.await ($handler:expr) => ($handler.await
@ -170,8 +170,7 @@ pub async fn parse<I: IntoIterator<Item=String> + std::fmt::Debug>(args: I) -> R
match arg.to_lowercase().trim() { match arg.to_lowercase().trim() {
"--generate" => { "--generate" => {
let handler = tokio::spawn(generate::handle(rx)); let handler = tokio::spawn(generate::handle(rx));
match generate::parse(std::iter::once(arg) match generate::parse(args.map(Into::into), tx).await
.chain(args.map(Into::into)), &mut tx).await
{ {
// The `handler` has dropped `rx` and returned an error, let unwrap_handler down there handle this. // The `handler` has dropped `rx` and returned an error, let unwrap_handler down there handle this.
Err(error::Error::AtomInternal) => Ok(()), Err(error::Error::AtomInternal) => Ok(()),
@ -186,7 +185,7 @@ pub async fn parse<I: IntoIterator<Item=String> + std::fmt::Debug>(args: I) -> R
_ => { _ => {
let handler = tokio::spawn(normal::handle(rx)); let handler = tokio::spawn(normal::handle(rx));
match normal::parse(std::iter::once(arg) match normal::parse(std::iter::once(arg)
.chain(args.map(Into::into)), &mut tx).await .chain(args.map(Into::into)), tx).await
{ {
// The `handler` has dropped `rx` and returned an error, let unwrap_handler down there handle this. // The `handler` has dropped `rx` and returned an error, let unwrap_handler down there handle this.
Err(error::Error::AtomInternal) => Ok(()), Err(error::Error::AtomInternal) => Ok(()),
@ -202,6 +201,9 @@ pub async fn parse<I: IntoIterator<Item=String> + std::fmt::Debug>(args: I) -> R
}; };
match unwrap_handler!(handler)? { match unwrap_handler!(handler)? {
Err(d @ error::ConstructError::Dropped) => {
unreachable!("{}", d);
},
Err(error) => { Err(error) => {
return Err(error) return Err(error)
.wrap_err_with(|| eyre!("Failed to construct operation from arguments")) .wrap_err_with(|| eyre!("Failed to construct operation from arguments"))

@ -2,7 +2,7 @@
use super::*; use super::*;
use tokio::sync::mpsc::Receiver; use tokio::sync::mpsc::Receiver;
#[instrument] #[instrument(level="trace", err, skip(rx))]
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>
{ {
let mut output = op::Normal::default(); let mut output = op::Normal::default();
@ -36,13 +36,15 @@ pub(super) async fn handle(mut rx: Receiver<Atom>) -> Result<config::Operation,
}; };
if sign != IsSigning::No && kind == config::KeyKind::Aes { if sign != IsSigning::No && kind == config::KeyKind::Aes {
warn!("Key was detected to not be RSA, option to sign is set and ignored."); warn!("Key was detected to not be RSA, option to sign is set and ignored.");
} else { } else if sign != IsSigning::No {
output.sign.push((string.clone(), password.clone())); output.sign.push((string.clone(), password.clone()));
} }
match kind { if sign != IsSigning::Only {
config::KeyKind::Aes => output.aes.push((string, password)), match kind {
config::KeyKind::RsaPublic => output.rsa.push((string, password)), config::KeyKind::Aes => output.aes.push((string, password)),
config::KeyKind::RsaPrivate => output.rsa.push((string, password)) config::KeyKind::RsaPublic => output.rsa.push((string, password)),
config::KeyKind::RsaPrivate => output.rsa.push((string, password))
}
} }
}, },
Atom::Generate(_) => unreachable!(), Atom::Generate(_) => unreachable!(),
@ -51,7 +53,8 @@ pub(super) async fn handle(mut rx: Receiver<Atom>) -> Result<config::Operation,
Ok(config::Operation::Normal(output)) Ok(config::Operation::Normal(output))
} }
pub(super) async fn parse<I: IntoIterator<Item=String>>(args: I, atoms: &mut mpsc::Sender<Atom>) -> Result<(), error::Error> #[instrument(level="trace", skip(args, atoms))]
pub(super) async fn parse<I: IntoIterator<Item=String>>(args: I, mut atoms: mpsc::Sender<Atom>) -> Result<(), error::Error>
{ {
let mut args = args.into_iter(); let mut args = args.into_iter();
let mut reading=true; let mut reading=true;
@ -59,7 +62,7 @@ pub(super) async fn parse<I: IntoIterator<Item=String>>(args: I, atoms: &mut mps
let mut mode_set=false; let mut mode_set=false;
while let Some(arg) = args.next() while let Some(arg) = args.next()
{ {
trace!("[Normal (parse)] opt string {:?}", arg);
macro_rules! take_one { macro_rules! take_one {
($msg:literal $($tt:tt)*) => { ($msg:literal $($tt:tt)*) => {
match args.next() { match args.next() {
@ -131,8 +134,8 @@ pub(super) async fn parse<I: IntoIterator<Item=String>>(args: I, atoms: &mut mps
Aes, Aes,
} }
let mut kind = KeySpec::Autodetect; let mut kind = KeySpec::Autodetect;
let mut password = Password::default(); let mut password = Password::No;
let mut signing = IsSigning::default(); let mut signing = IsSigning::No;
let mut had = smallmap::Map::new(); let mut had = smallmap::Map::new();
while let Some(opt) = opt.next() { while let Some(opt) = opt.next() {
@ -161,12 +164,12 @@ pub(super) async fn parse<I: IntoIterator<Item=String>>(args: I, atoms: &mut mps
check_combine!['r','R']; check_combine!['r','R'];
kind = KeySpec::Aes; kind = KeySpec::Aes;
}, },
'P' => {
check_combine!['p'];
password= Password::Yes;
},
'p' => { 'p' => {
check_combine!['P']; check_combine!['P'];
password= Password::Yes;
},
'P' => {
check_combine!['p'];
password = Password::Specific(take_one!("P: Expected a password")); password = Password::Specific(take_one!("P: Expected a password"));
}, },
'S' => { 'S' => {

@ -68,7 +68,12 @@ fn install_tracing() {
async fn work(op: config::Operation) -> Result<(), eyre::Report> async fn work(op: config::Operation) -> Result<(), eyre::Report>
{ {
todo!() debug!("Got op: {:#?}", op);
match op {
config::Operation::Help => args::usage(),
_ => todo!(),
}
Ok(())
} }
#[instrument] #[instrument]

Loading…
Cancel
Save