start aparse

work
Avril 4 years ago
parent 878c58b49e
commit e96f9ab8c7
Signed by: flanchan
GPG Key ID: 284488987C31F630

1
Cargo.lock generated

@ -650,6 +650,7 @@ dependencies = [
"chrono",
"color-eyre",
"lazy_format",
"lazy_static",
"rustc_version",
"sha2",
"tokio",

@ -24,6 +24,7 @@ sha2 = "0.9.1"
chrono = "0.4.13"
color-eyre = "0.5.1"
lazy_format = "1.8.3"
lazy_static = "1.4.0"
[build-dependencies]
rustc_version = "0.2"

@ -0,0 +1,30 @@
//! Arg parsing
use super::*;
pub fn program_name() -> &'static str
{
lazy_static::lazy_static!{
static ref NAME: &'static str = Box::leak(std::env::args().next().unwrap().into_boxed_str());
}
&NAME[..]
}
#[inline] pub fn parse() -> ReportedResult<config::Config>
{
parse_as_args(std::env::args().skip(1))
}
pub fn parse_as_args<T,I>(args: I) -> ReportedResult<config::Config>
where I: IntoIterator<Item=T>,
T: AsRef<str>
{
let mut args = args.into_iter();
while let Some(arg) = args.next() {
let arg = arg.as_ref();
}
todo!()
}

@ -0,0 +1,104 @@
//! Config and params
use std::{
path::{
PathBuf,
},
error,
};
/// The error handling mode for this run.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Ord, PartialOrd)]
pub enum ErrorMode
{
Ignore,
Report,
Cancel,
Terminate,
}
pub trait ErrModeExt<T,E>
{
fn handle_with(self, err: &ErrorMode) -> Result<Option<T>,E>;
}
impl<T,E> ErrModeExt<T,E> for Result<T,E>
where E: error::Error
{
#[inline] fn handle_with(self, err: &ErrorMode) -> Result<Option<T>,E>
{
err.handle(self)
}
}
impl ErrorMode
{
pub fn handle<T,E>(&self, err: Result<T,E>) -> Result<Option<T>, E>
where E: error::Error,
{
match err {
Ok(v) => Ok(Some(v)),
Err(err) => {
match self {
Self::Ignore => Ok(None),
Self::Report => {
eprintln!("Error: {}", err);
Ok(None)
},
Self::Cancel => {
Err(err)
},
Self::Terminate => {
eprintln!("Fatal error: {}", err);
std::process::exit(-1)
}
}
}
}
}
}
impl Default for ErrorMode
{
#[inline]
fn default() -> Self
{
Self::Report
}
}
/// The common options flags for all modes
#[derive(Debug, Clone, PartialEq, Eq, Hash, Ord, PartialOrd)]
pub struct Flags
{
pub verbose: bool,
pub error_mode: ErrorMode,
}
impl Default for Flags
{
#[inline]
fn default() -> Self
{
Self {
verbose: false,
error_mode: ErrorMode::default(),
}
}
}
/// Each option mode
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Mode
{
Normal {
paths: Vec<PathBuf>,
},
Help,
}
/// All config for this run
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Config
{
pub data: Mode,
pub flags: Flags,
}

@ -164,7 +164,7 @@ impl error::Error for Error
#[macro_export] macro_rules! error {
(@ $msg:expr) => {
{
pub struct DisplayError<D>(D) where D: fmt::Display;
pub struct DisplayError<D>(D, Context) where D: fmt::Display;
impl<D> fmt::Debug for DisplayError<D>
where D: fmt::Display
{
@ -184,7 +184,16 @@ impl error::Error for Error
impl<D> error::Error for DisplayError<D>
where D: fmt::Display{}
DisplayError($msg)
impl<D> From<DisplayError<D>> for Error
{
fn from(from: DisplayError<D) -> Self
{
Self::new_none(ErrorKind::Any(box from), from.context);
}
}
DisplayError($msg, context!())
}
};

@ -20,7 +20,7 @@ use error::{
use color_eyre::{
eyre::{
self,
Result as ReportResult,
Result as ReportedResult,
},
};
@ -30,14 +30,19 @@ mod hash;
mod metadata;
mod resolve;
mod database;
mod config;
mod args;
async fn begin() -> Result<i32, Box<dyn std::error::Error>>
async fn begin() -> ReportedResult<i32>
{
color_eyre::install()?;
Ok(0)
}
#[tokio::main]
async fn main() {
async fn main()
{
std::process::exit(match begin().await {
Ok(0) => return,
Err(err) => {

Loading…
Cancel
Save