begin threaded proc

master
Avril 5 years ago
parent 649e98b9cc
commit e77e8c4290
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -3,17 +3,18 @@ use std::{
hash::Hash,
collections::{HashSet, HashMap},
mem,
fmt,
};
pub trait DedupFullExt
{
fn dedup_full(&mut self);
fn dedup_full(&mut self);
}
impl<T> DedupFullExt for Vec<T>
where T: Hash + Default + Eq
{
fn dedup_full(&mut self)
fn dedup_full(&mut self)
{
let mut set: HashMap<T, usize> = HashMap::new();
for (i,x) in (0..).zip(self.iter_mut())
@ -37,6 +38,70 @@ where T: Hash + Default + Eq
}
}
}
pub trait ErrorLogForgetExt<T, E>: Sized
{
fn log_and_forget(self, mode: &log::Mode, level: log::Level) -> Option<T>;
}
impl<T,E> ErrorLogForgetExt<T,E> for Result<T,E>
where E: fmt::Display,
{
fn log_and_forget(self, mode: &log::Mode, level: log::Level) -> Option<T>
{
match self {
Err(e) => {
log!(mode.level(level), "{}", &e);
if let log::Level::Fatal = level {
std::process::exit(1);
}
None
},
Ok(v) => Some(v),
}
}
}
pub trait ErrorLogExt: Sized
{
fn into_log(self, mode: &log::Mode, level: log::Level) -> Self;
fn into_fatal(self, mode: Option<log::Mode>) -> Self
{
self.into_log(&mode.unwrap_or(log::Mode::Verbose), log::Level::Fatal)
}
}
impl<T,E> ErrorLogExt for Result<T,E>
where E: fmt::Display
{
fn into_log(self, mode: &log::Mode, level: log::Level) -> Self
{
match self {
Err(e) => {
log!(mode.level(level), "{}", &e);
if let log::Level::Fatal = level {
std::process::exit(1);
}
Err(e)
},
Ok(v) => Ok(v),
}
}
}
pub trait ErrorExt<T>
{
fn into_string(self) -> Result<T, String>;
}
impl<T,E> ErrorExt<T> for Result<T,E>
where E: fmt::Display
{
fn into_string(self) -> Result<T, String>
{
match self {
Ok(v) => Ok(v),
Err(e) => Err(format!("{}", e)),
}
}
}
#[cfg(test)]
mod test

@ -102,7 +102,6 @@ impl fmt::Display for Level
macro_rules! log {
($level:tt, $mode:expr => $format:expr, $($rest:expr),*) => {
{
if let Some(level) = $mode.level($crate::log::Level::$level) {
println!("{} [{}]: {}", $crate::log::timestamp(), level, format!($format, $($rest)*));
true
@ -116,6 +115,14 @@ macro_rules! log {
log!(Info, $mode => $format, $($rest)*);
}
};
($level:expr, $format:expr, $($rest:expr),*) => {
if let Some(level) = $level {
println!("{} [{}]: {}", $crate::log::timestamp(), level, format!($format, $($rest)*));
true
} else {
false
};
};
}
pub fn timestamp() -> String

@ -2,14 +2,14 @@
pub const BUFFER_SIZE: usize = 4096;
#[macro_use]
mod log;
mod bytes;
mod ext;
pub use ext::*;
mod error;
mod hash;
mod container;
#[macro_use]
mod log;
mod config;
mod arg;
mod proc;
@ -82,14 +82,55 @@ fn parse_args() -> Result<config::Config, error::Error>
#[cfg_attr(feature="threads", tokio::main)]
#[cfg(feature="threads")]
async fn main() -> Result<(), error::Error>
async fn main() -> Result<(), Box<dyn std::error::Error>>
{
let args = parse_args()?;
use tokio::{
fs::{
OpenOptions,
},
sync,
};
use std::{
path::Path,
};
let args = parse_args().into_string()?;
let lmode = &args.mode.logging_mode;
log!(Debug, lmode => "Args parsed: {:?}", args);
let mut children = Vec::new();
let mut hashes = container::DupeMap::new();
// Load hashes
for load in args.load.iter()
{
let load = Path::new(load);
if load.exists() {
if load.is_file() {
if let Some(mut file) = OpenOptions::new()
.read(true)
.open(load).await.log_and_forget(lmode, log::Level::Warning)
{
}
} else {
log!(Warning, lmode => "Exclusing directory from load path {:?}", load);
}
} else {
log!(Info, lmode => "Ignoring non-existant load path {:?}", load);
}
}
for path in args.paths.iter()
{
let path = Path::new(path);
if path.is_dir() {
children.push(tokio::task::spawn(async move {
//proc::do_dir_async()
}));
}
}
Ok(())
}

Loading…
Cancel
Save