begin threaded proc

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

@ -3,17 +3,18 @@ use std::{
hash::Hash, hash::Hash,
collections::{HashSet, HashMap}, collections::{HashSet, HashMap},
mem, mem,
fmt,
}; };
pub trait DedupFullExt pub trait DedupFullExt
{ {
fn dedup_full(&mut self); fn dedup_full(&mut self);
} }
impl<T> DedupFullExt for Vec<T> impl<T> DedupFullExt for Vec<T>
where T: Hash + Default + Eq where T: Hash + Default + Eq
{ {
fn dedup_full(&mut self) fn dedup_full(&mut self)
{ {
let mut set: HashMap<T, usize> = HashMap::new(); let mut set: HashMap<T, usize> = HashMap::new();
for (i,x) in (0..).zip(self.iter_mut()) 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)] #[cfg(test)]
mod test mod test

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

@ -2,14 +2,14 @@
pub const BUFFER_SIZE: usize = 4096; pub const BUFFER_SIZE: usize = 4096;
#[macro_use]
mod log;
mod bytes; mod bytes;
mod ext; mod ext;
pub use ext::*; pub use ext::*;
mod error; mod error;
mod hash; mod hash;
mod container; mod container;
#[macro_use]
mod log;
mod config; mod config;
mod arg; mod arg;
mod proc; mod proc;
@ -82,14 +82,55 @@ fn parse_args() -> Result<config::Config, error::Error>
#[cfg_attr(feature="threads", tokio::main)] #[cfg_attr(feature="threads", tokio::main)]
#[cfg(feature="threads")] #[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; let lmode = &args.mode.logging_mode;
log!(Debug, lmode => "Args parsed: {:?}", args); 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(()) Ok(())
} }

Loading…
Cancel
Save