Skip to content

Commit

Permalink
Refactor into submodules
Browse files Browse the repository at this point in the history
  • Loading branch information
rdvdev2 committed Feb 8, 2022
1 parent 68179bb commit 07234e5
Show file tree
Hide file tree
Showing 16 changed files with 489 additions and 466 deletions.
File renamed without changes.
6 changes: 6 additions & 0 deletions src/compilation/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
mod compiler;
mod template;

pub use compiler::P1XX;
pub use template::generate_main;
pub use compiler::CompilationError as Error;
File renamed without changes.
7 changes: 3 additions & 4 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use core::fmt;
use std::{env, fs, io, path};
use configparser::ini;
use crate::connection_manager::Credentials;
use crate::{connection_manager, debug, ux};
use crate::{debug, fetch, ux};

#[derive(Debug)]
pub enum Error {
Expand Down Expand Up @@ -41,7 +40,7 @@ pub struct Config {
pub config_dir: path::PathBuf,
pub cache_dir: path::PathBuf,
pub tmp_dir: path::PathBuf,
pub credentials: Option<Credentials>
pub credentials: Option<fetch::Credentials>
}

impl Config {
Expand Down Expand Up @@ -104,7 +103,7 @@ impl Config {
let password = auth.get("password");

if let (Some(Some(email)), Some(Some(password))) = (email, password) {
self.credentials = Some(connection_manager::Credentials::new(email.as_bytes(), password.as_bytes()));
self.credentials = Some(fetch::Credentials::new(email.as_bytes(), password.as_bytes()));
}
}
}
Expand Down
126 changes: 0 additions & 126 deletions src/download.rs

This file was deleted.

39 changes: 2 additions & 37 deletions src/connection_manager.rs → src/fetch/connection_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::{fmt, fs, io, path};
use std::io::Write;
use curl::easy;
use crate::{config, debug, warning};
use crate::fetch::credentials;

pub enum Error {
CurlError(curl::Error),
Expand Down Expand Up @@ -81,7 +82,7 @@ impl ConnectionManager {
Ok(())
}

fn try_to_authenticate(&mut self, credentials: &Credentials) -> Result<bool, Error> {
fn try_to_authenticate(&mut self, credentials: &credentials::Credentials) -> Result<bool, Error> {
if let Some(form) = credentials.build_form() {
debug!("Attempting to authenticate");
self.handle.url("https://jutge.org/")?;
Expand Down Expand Up @@ -112,40 +113,4 @@ impl ConnectionManager {

Ok(!String::from_utf8_lossy(&response).contains("Did you sign in?"))
}
}

#[derive(Clone)]
pub struct Credentials {
email: Vec<u8>,
password: Vec<u8>
}

impl Credentials {
pub fn new(email: &[u8], password: &[u8]) -> Credentials {
Credentials {
email: Vec::from(email),
password: Vec::from(password)
}
}

fn build_form(&self) -> Option<easy::Form> {
let mut form = easy::Form::new();

form.part("email").contents(self.email.as_slice()).add().ok()?;
form.part("password").contents(self.password.as_slice()).add().ok()?;
form.part("submit").contents(b"").add().ok()?;

Some(form)
}
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn credentials_build_form_test() {
let credentials = Credentials::new(b"[email protected]", b"1234");
assert!(credentials.build_form().is_some());
}
}
37 changes: 37 additions & 0 deletions src/fetch/credentials.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use curl::easy;

#[derive(Clone)]
pub struct Credentials {
email: Vec<u8>,
password: Vec<u8>
}

impl Credentials {
pub fn new(email: &[u8], password: &[u8]) -> Credentials {
Credentials {
email: Vec::from(email),
password: Vec::from(password)
}
}

pub fn build_form(&self) -> Option<easy::Form> {
let mut form = easy::Form::new();

form.part("email").contents(self.email.as_slice()).add().ok()?;
form.part("password").contents(self.password.as_slice()).add().ok()?;
form.part("submit").contents(b"").add().ok()?;

Some(form)
}
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn credentials_build_form_test() {
let credentials = Credentials::new(b"[email protected]", b"1234");
assert!(credentials.build_form().is_some());
}
}
54 changes: 54 additions & 0 deletions src/fetch/download.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use crate::{debug, problem, ux};
use crate::fetch::{connection_manager, unzip};

pub fn download_problem_zip(problem: &problem::Problem, connection: &mut connection_manager::ConnectionManager) -> (ux::TaskStatus, Option<connection_manager::Error>) {
let path = problem.work_dir.join("problem.zip");

if path.is_file() {
debug!("Problem zip already downloaded");
(ux::TaskStatus::SkipGood, None)
} else if path.is_dir() {
debug!("The download path is a folder");
(ux::TaskStatus::SkipBad, None)
} else {
match connection.get_file(&problem.zip_url, &path) {
Ok(()) => (ux::TaskStatus::Done, None),
Err(e) => (ux::TaskStatus::Fail, Some(e)),
}
}
}

pub fn download_problem_main(problem: &problem::Problem, connection: &mut connection_manager::ConnectionManager) -> (ux::TaskStatus, Option<connection_manager::Error>) {
let path = problem.work_dir.join("main.cc");

if problem.has_main || path.is_file() {
debug!("Problem main.cc already downloaded or unnecessary");
(ux::TaskStatus::SkipGood, None)
} else if path.is_dir() {
debug!("The download path is a folder");
(ux::TaskStatus::SkipBad, None)
} else {
match connection.get_file(&problem.main_cc_url, &path) {
Ok(()) => (ux::TaskStatus::Done, None),
Err(e) => (ux::TaskStatus::Fail, Some(e))
}
}
}

pub fn unzip_problem_tests(problem: &problem::Problem) -> (ux::TaskStatus, Option<unzip::Error>) {
let zip_path = problem.work_dir.join("problem.zip");
let tests_path = problem.work_dir.join("samples");

if tests_path.is_dir() {
debug!("Problem tests already extracted");
(ux::TaskStatus::SkipGood, None)
} else if tests_path.is_file() || !zip_path.exists() {
debug!("Unable to extract problem tests");
(ux::TaskStatus::SkipBad, None)
} else {
match unzip::unzip_samples(&zip_path, &tests_path) {
Ok(()) => (ux::TaskStatus::Done, None),
Err(e) => (ux::TaskStatus::Fail, Some(e))
}
}
}
52 changes: 52 additions & 0 deletions src/fetch/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use std::fmt;
use crate::{config, error, problem, ux, warning};

mod download;
mod connection_manager;
mod credentials;
mod unzip;

pub use credentials::Credentials as Credentials;

pub fn fetch_resources(problem: &problem::Problem, config: &config::Config) -> Result<(bool, bool, bool), crate::Error> {
let mut connection = connection_manager::ConnectionManager::new(config)
.map_err(|e| crate::Error {
description: format!("Couldn't start the connection manager: {}", e),
exitcode: exitcode::IOERR
})?;

let zip = execute_task("Downloading problem zip", || download::download_problem_zip(problem, &mut connection));
let main_cc = execute_task("Downloading problem main.cc", || download::download_problem_main(problem, &mut connection));
let tests = execute_task("Extracting tests", || download::unzip_problem_tests(problem));

if !zip {
warning!("Unable to retrieve tests!");
}

if !main_cc {
return Err( crate::Error {
description: String::from("Unable to retrieve the main.cc file, which is required to compile your binary!"),
exitcode: exitcode::IOERR
});
}

if !tests {
warning!("Unable to unzip tests!");
}

Ok((zip, main_cc, tests))
}

fn execute_task<T, E: fmt::Display + Sized>(name: &str, mut task: T) -> bool
where
T: FnMut() -> (ux::TaskStatus, Option<E>)
{
ux::show_task_status(name, ux::TaskType::Fetch, &ux::TaskStatus::InProgress);
let (status, err) = task();

ux::show_task_status(name, ux::TaskType::Fetch, &status);
if let Some(err) = err {
error!("The task [{}] returned the following error: {}", name, err);
}
status.is_ok()
}
Loading

0 comments on commit 07234e5

Please sign in to comment.