half-working build

cli-redesign
Avril 4 years ago
parent e1b1539a57
commit 725374d2e2
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -1 +1,2 @@
implement output to dir
Remove Cargo.lock when termprogress is uploaded and Cargo.toml is properly formatted

@ -3,7 +3,6 @@ use super::*;
use std::{
path::PathBuf,
str,
fmt,
};
#[derive(Debug, PartialEq, Eq, Hash)]

@ -1,4 +1,4 @@
#![allow(unused_macros)]
pub trait JoinStrsExt: Sized
{
/// Join an iterator of `str` with a seperator
@ -25,3 +25,16 @@ where I: Iterator<Item=T>,
output
}
}
macro_rules! unwrap {
($value:expr) => {
{
match $value {
Ok(value) => value,
Err(error) => {
panic!("Fatal error: {}", error)
},
}
}
};
}

@ -1,16 +1,16 @@
use super::*;
use std::{
str,
io::{
self,
Write,
},
};
const HEADER_BASE64_JPEG: &'static [u8] = b"/9j/";
const HEADER_BASE64_PNG: &'static [u8] = b"iVBO";
const HEADER_BASE64_GIF: &'static [u8] = b"R0lG";
const EXT_JPEG: &'static str = "jpg";
const EXT_PNG: &'static str = "png";
const EXT_GIF: &'static str = "gif";
/// An image type header
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub enum ImageType
@ -50,6 +50,16 @@ impl ImageType
Err(error::Error::InvalidFormat)
}
}
/// Get the extension for this image type
pub fn ext(&self) -> &str
{
match self {
Self::Jpeg => EXT_JPEG,
Self::Png => EXT_PNG,
Self::Gif => EXT_GIF,
}
}
}
impl str::FromStr for ImageType

@ -1,4 +1,3 @@
use super::*;
use std::{
error,
fmt,

@ -112,6 +112,18 @@ impl BasedLoli
impl<'a> LoliBounds<'a>
{
/// Get the type of image
pub fn image(&self) -> &encoding::ImageType
{
&self.image_type
}
pub fn bounds(&self) -> &Range<usize>
{
&self.range
}
/// Try to create a decoding container for this `BasedLoli` to the file in `path`.
pub fn create_child(&self, path: impl AsRef<Path>) -> Result<Loli, error::Error>
{

@ -1,11 +1,12 @@
#![feature(linked_list_remove)]
#![feature(label_break_value)]
#![allow(dead_code)]
use termprogress::{
progress,
ProgressBar,
};
#[macro_use]
mod ext;
use ext::*;

@ -16,8 +16,8 @@ impl error::Error for Error
fn source(&self) -> Option<&(dyn error::Error + 'static)>
{
match &self {
_ => None,
Self::Format(rng) => Some(rng),
_ => None,
}
}
}

@ -17,12 +17,15 @@ mod progress;
/// Decode a loli from path
pub async fn decode(from: impl AsRef<Path>, to: impl AsRef<Path>, progress: &mut progress::CommandSender) -> Result<loli::Loli, error::Error>
{
prog_send!(link progress.println("Mapping child"));
prog_send!(progress.println("Mapping child"));
let base = loli::BasedLoli::map(from)?;
prog_send!(link progress.println("Calculating bounds"));
prog_send!(progress.println("Calculating bounds"));
let bounds = base.calculate_bounds()?; // If server is returning error code, this will fail.
prog_send!(link progress.println(format!("Decoding ({:?}) {} -> {} bytes", bounds, base.as_ref().len(), base.decoded_size())));
let mut decoded = bounds.create_child(to)?;
prog_send!(progress.println(format!("Finding bounds ({:?}) {} -> {} bytes", bounds, base.as_ref().len(), base.decoded_size())));
//Find extension
let mut decoded = bounds.create_child(to.as_ref().with_extension(bounds.image().ext()))?;
prog_send!(progress.println("Decoding..."));
let sz = bounds.decode(&mut decoded)?;
prog_send!(link progress.println(format!("Decode complete ({} bytes)", sz)));
@ -37,7 +40,7 @@ pub async fn perform(url: impl AsRef<str>, path: impl AsRef<Path>, progress: &mu
let path = path.as_ref();
let mut resp = reqwest::get(url).await?;
let resp = reqwest::get(url).await?;
let len = resp.content_length();
//prog_send!(link progress.push_task(&task));
@ -93,39 +96,54 @@ pub async fn work(conf: config::Config) -> Result<(), Box<dyn std::error::Error>
let task = format!("{:?}", path); //TODO: Real task name
prog_send!(link unwind prog.push_task(&task));
let temp = tempfile::TempFile::new();
match perform(&url, &temp, &mut prog).await {
Err(e) => panic!("Failed downloading {} -> {:?}: {}", url, temp, e), //TODO: Make real error handler
Ok(_) => {
let path = match path {
config::OutputType::File(file) => file,
config::OutputType::Directory(dir) => unimplemented!(), //TODO: implement get hash to file
};
let loli = match decode(&temp, &path, &mut prog).await {
Ok(v) => v,
Err(e) => panic!("Failed decoding: {}", e),
};
},
}
prog_send!(link prog.pop_task(task)); //TODO: Make sure we don't return gracefully before this is called.
let return_value = 'clean: {
match perform(&url, &temp, &mut prog).await {
Err(e) => prog_send!(link prog.println(format!("Failed downloading {} -> {:?}: {}", url, temp, e))),
Ok(_) => {
let path = match path {
config::OutputType::File(file) => file,
config::OutputType::Directory(dir) => unimplemented!(), //TODO: implement get hash to file
};
let loli = match decode(&temp, &path, &mut prog).await {
Ok(v) => v,
Err(e) => {
prog_send!(link prog.println(format!("Failed decoding: {}", e)));
break 'clean false;
},
};
prog_send!(link prog.println(format!("{:?} Complete", loli)));
break 'clean true;
},
};
false
};
prog_send!(link prog.pop_task(task));
return_value
}));
}
prog_send!(link prog_writer.println("Children working..."));
let mut done =0;
let total = children.len();
for child in children.into_iter()
{
match child.await {
Ok(v) => (),
Ok(true) => done+=1,
Err(err) => {
println!("Child failed: {}", err);
prog_send!(try link unwind prog_writer.println(format!("Child panic: {}", err)));
},
_ => (),
}
}
prog_send!(link prog_writer.set_title(""));
prog_send!(try link prog_writer.kill());
prog.await.expect("mpsc fatal");
println!("Completed {} / {} lolis ({} failed).", done, total, total-done);
Ok(())
}

@ -9,7 +9,6 @@ use tokio::{
UnboundedReceiver,
UnboundedSender
},
Mutex,
},
task::{
self,
@ -18,14 +17,7 @@ use tokio::{
};
use termprogress::{
spinner,
Display,
Spinner,
};
use std::{
sync::Arc,
fmt,
};
enum CommandInternal

@ -1,4 +1,3 @@
use super::*;
use tokio::{
sync::{
mpsc::{

Loading…
Cancel
Save