From 570adfc17df7d7764df39463a4a280b4c09af0f7 Mon Sep 17 00:00:00 2001 From: emma Date: Tue, 28 Mar 2023 18:27:22 -0400 Subject: [PATCH] more changes to error types --- src/args.rs | 31 ++++++++++++++++++++++++++++++- src/error.rs | 48 ------------------------------------------------ src/main.rs | 13 +++---------- 3 files changed, 33 insertions(+), 59 deletions(-) delete mode 100644 src/error.rs diff --git a/src/args.rs b/src/args.rs index d5ef17a..827b13c 100644 --- a/src/args.rs +++ b/src/args.rs @@ -24,7 +24,7 @@ use core::{ }; pub use arg::Args; -use yacexits::EX_DATAERR; +use yacexits::{ EX_DATAERR, EX_USAGE }; #[derive(Args, Debug)] pub struct Arguments { @@ -210,3 +210,32 @@ impl FromStr for PackageType { } } } + +// TODO: Make this an enum for this for matching specific error kinds +pub struct ArgsError { + message: String, + code: u32, +} + +// TODO: More granular matching here with an enum +impl From> for ArgsError { + fn from(_: arg::ParseKind) -> Self { + let message = format!( + "{}", + "[-v] add | get | init | list | remove | update\n\n".to_owned() + + "add [-m version] [-f hopfiles...] packages...\n" + + "get [-n] [-d directory] [-m versions...] [-t types...] packages\n" + + "init [-f hopfiles...] version type\n" + + "list [[-f hopfiles...] | [-m versions...] [-t types...]]\n" + + "remove [[-f hopfiles...] | type version]] packages...\n" + + "update [[-f hopfiles... | [-m versions...] [-t types...]]", + ); + ArgsError { message, code: EX_USAGE } + } +} + +impl From for (String, u32) { + fn from(err: ArgsError) -> Self { + (err.message, err.code) + } +} diff --git a/src/error.rs b/src/error.rs deleted file mode 100644 index f0346ce..0000000 --- a/src/error.rs +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright (c) 2023 [ ] - * SPDX-License-Identifier: AGPL-3.0-or-later - * - * This file is part of Hopper. - * - * Hopper is free software: you can redistribute it and/or modify it under the - * terms of the GNU General Public License as published by the Free Software - * Foundation, either version 3 of the License, or (at your option) any later - * version. - * - * Hopper is distributed in the hope that it will be useful, but WITHOUT ANY - * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR - * A PARTICULAR PURPOSE. See the GNU General Public License for more details. - * You should have received a copy of the GNU General Public License along with - * Hopper. If not, see . - */ - -use yacexits::*; - -pub struct HopError { - pub code: u32, - - pub message: String, -} - -// TODO: More granular matching here -impl From> for HopError { - fn from(_: arg::ParseKind) -> Self { - let message = format!( - "Usage: {}", - "[-v] add | get | init | list | remove | update\n\n".to_owned() + - "add [-m version] [-f hopfiles...] packages...\n" + - "get [-n] [-d directory] [-m versions...] [-t types...] packages\n" + - "init [-f hopfiles...] version type\n" + - "list [[-f hopfiles...] | [-m versions...] [-t types...]]\n" + - "remove [[-f hopfiles...] | type version]] packages...\n" + - "update [[-f hopfiles... | [-m versions...] [-t types...]]", - ); - Self { message, code: EX_USAGE } - } -} - -impl From for (String, u32) { - fn from(err: HopError) -> Self { - (err.message, err.code) - } -} diff --git a/src/main.rs b/src/main.rs index 839cdab..bb600b3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -26,17 +26,14 @@ mod args; mod client; mod config; mod hopfile; -mod error; use api::*; use args::*; use client::*; use config::*; use hopfile::*; -use error::*; use yacexits::{ - exit, EX_OSERR, EX_SOFTWARE, }; @@ -51,12 +48,8 @@ struct AppContext { async fn rust_main(arguments: yacexits::Args) -> Result { let argv: Vec<&str> = arguments.into_iter().collect(); - let args = match Arguments::from_args(argv.clone().into_iter()) { - Ok(args) => args, - Err(_) => { - return Err((format!("Unable to ascertain arguments."), EX_OSERR)); - } - }; + let args = Arguments::from_args(argv.clone().into_iter()) + .map_err(|err| { ArgsError::from(err) })?; let config = Config::read_config()?; let ctx = AppContext { args, config }; @@ -68,7 +61,7 @@ async fn rust_main(arguments: yacexits::Args) -> Result { let message = format!( "{}: Unimplemented subcommand.", ctx.args.sub ); - Err(HopError { message, code: EX_SOFTWARE }) + Err((message, EX_SOFTWARE)) }, }? }