-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
489 additions
and
466 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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), | ||
|
@@ -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/")?; | ||
|
@@ -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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
Oops, something went wrong.