more changes to error types
This commit is contained in:
parent
8448097387
commit
570adfc17d
31
src/args.rs
31
src/args.rs
|
@ -24,7 +24,7 @@ use core::{
|
||||||
};
|
};
|
||||||
|
|
||||||
pub use arg::Args;
|
pub use arg::Args;
|
||||||
use yacexits::EX_DATAERR;
|
use yacexits::{ EX_DATAERR, EX_USAGE };
|
||||||
|
|
||||||
#[derive(Args, Debug)]
|
#[derive(Args, Debug)]
|
||||||
pub struct Arguments {
|
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<arg::ParseKind<'_>> 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<ArgsError> for (String, u32) {
|
||||||
|
fn from(err: ArgsError) -> Self {
|
||||||
|
(err.message, err.code)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
48
src/error.rs
48
src/error.rs
|
@ -1,48 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (c) 2023 [ ] <https://git.tebibyte.media/BlankParenthesis/>
|
|
||||||
* 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 <https://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
use yacexits::*;
|
|
||||||
|
|
||||||
pub struct HopError {
|
|
||||||
pub code: u32,
|
|
||||||
|
|
||||||
pub message: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: More granular matching here
|
|
||||||
impl From<arg::ParseKind<'_>> 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<HopError> for (String, u32) {
|
|
||||||
fn from(err: HopError) -> Self {
|
|
||||||
(err.message, err.code)
|
|
||||||
}
|
|
||||||
}
|
|
13
src/main.rs
13
src/main.rs
|
@ -26,17 +26,14 @@ mod args;
|
||||||
mod client;
|
mod client;
|
||||||
mod config;
|
mod config;
|
||||||
mod hopfile;
|
mod hopfile;
|
||||||
mod error;
|
|
||||||
|
|
||||||
use api::*;
|
use api::*;
|
||||||
use args::*;
|
use args::*;
|
||||||
use client::*;
|
use client::*;
|
||||||
use config::*;
|
use config::*;
|
||||||
use hopfile::*;
|
use hopfile::*;
|
||||||
use error::*;
|
|
||||||
|
|
||||||
use yacexits::{
|
use yacexits::{
|
||||||
exit,
|
|
||||||
EX_OSERR,
|
EX_OSERR,
|
||||||
EX_SOFTWARE,
|
EX_SOFTWARE,
|
||||||
};
|
};
|
||||||
|
@ -51,12 +48,8 @@ struct AppContext {
|
||||||
async fn rust_main(arguments: yacexits::Args) -> Result<u32, (String, u32)> {
|
async fn rust_main(arguments: yacexits::Args) -> Result<u32, (String, u32)> {
|
||||||
let argv: Vec<&str> = arguments.into_iter().collect();
|
let argv: Vec<&str> = arguments.into_iter().collect();
|
||||||
|
|
||||||
let args = match Arguments::from_args(argv.clone().into_iter()) {
|
let args = Arguments::from_args(argv.clone().into_iter())
|
||||||
Ok(args) => args,
|
.map_err(|err| { ArgsError::from(err) })?;
|
||||||
Err(_) => {
|
|
||||||
return Err((format!("Unable to ascertain arguments."), EX_OSERR));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let config = Config::read_config()?;
|
let config = Config::read_config()?;
|
||||||
let ctx = AppContext { args, config };
|
let ctx = AppContext { args, config };
|
||||||
|
@ -68,7 +61,7 @@ async fn rust_main(arguments: yacexits::Args) -> Result<u32, (String, u32)> {
|
||||||
let message = format!(
|
let message = format!(
|
||||||
"{}: Unimplemented subcommand.", ctx.args.sub
|
"{}: Unimplemented subcommand.", ctx.args.sub
|
||||||
);
|
);
|
||||||
Err(HopError { message, code: EX_SOFTWARE })
|
Err((message, EX_SOFTWARE))
|
||||||
},
|
},
|
||||||
}?
|
}?
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue