Skip to content

Commit

Permalink
Merge pull request #10 from rage/update-langs
Browse files Browse the repository at this point in the history
Update tmc-langs
  • Loading branch information
jolampi authored Nov 23, 2021
2 parents 0014f12 + 0aec935 commit ec57ba3
Show file tree
Hide file tree
Showing 9 changed files with 376 additions and 498 deletions.
738 changes: 307 additions & 431 deletions Cargo.lock

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ serde = "1"
serde_json = "1"
log = "0.4"
dirs = "3"
tmc-langs-util = { git = "https://github.com/rage/tmc-langs-rust/", rev = "13f3d55" }
tmc-langs-framework = { git = "https://github.com/rage/tmc-langs-rust/", rev = "13f3d55" }
tmc-langs = { git = "https://github.com/rage/tmc-langs-rust/", rev = "13f3d55" }
tmc-client = { git = "https://github.com/rage/tmc-langs-rust/", rev = "13f3d55" }
# rev 74dcb3e = 0.23.2
tmc-langs-util = { git = "https://github.com/rage/tmc-langs-rust/", rev = "74dcb3e" }
tmc-langs-framework = { git = "https://github.com/rage/tmc-langs-rust/", rev = "74dcb3e" }
tmc-langs = { git = "https://github.com/rage/tmc-langs-rust/", rev = "74dcb3e" }
tmc-client = { git = "https://github.com/rage/tmc-langs-rust/", rev = "74dcb3e" }
rpassword = "5.0.1"
mockall = "0.9.0"
indicatif = "0.15.0"
Expand Down
51 changes: 27 additions & 24 deletions src/commands/command_util.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use isolang::Language;
use reqwest::Url;
use std::path::Path;
use std::path::PathBuf;

use std::env;
use tmc_client::{
ClientError, Course, CourseDetails, CourseExercise, ExercisesDetails, NewSubmission,
Organization, SubmissionFinished, TmcClient, Token,
use tmc_client::response::{
Course, CourseDetails, CourseExercise, ExercisesDetails, NewSubmission, Organization,
SubmissionFinished,
};
use tmc_client::{ClientError, TmcClient, Token};
use tmc_langs::Credentials;
use tmc_langs::DownloadOrUpdateCourseExercisesResult;
use tmc_langs::DownloadResult;
Expand All @@ -33,26 +35,26 @@ pub trait Client {
fn list_courses(&mut self) -> Result<Vec<Course>, String>;
fn get_organizations(&mut self) -> Result<Vec<Organization>, String>;
fn logout(&mut self);
fn wait_for_submission(&self, submission_url: &str) -> Result<SubmissionFinished, ClientError>;
fn wait_for_submission(&self, submission_url: Url) -> Result<SubmissionFinished, ClientError>;
fn submit(
&self,
projects_dir: &Path,
course_slug: &str,
exercise_slug: &str,
locale: Option<Language>,
) -> Result<NewSubmission, LangsError>;
fn get_course_exercises(&mut self, course_id: usize) -> Result<Vec<CourseExercise>, String>;
fn get_course_exercises(&mut self, course_id: u32) -> Result<Vec<CourseExercise>, String>;
fn get_exercise_details(
&mut self,
exercise_ids: Vec<usize>,
exercise_ids: Vec<u32>,
) -> Result<Vec<ExercisesDetails>, String>;
fn download_or_update_exercises(
&mut self,
download_params: &[usize],
download_params: &[u32],
path: &Path,
) -> Result<DownloadResult, LangsError>;
fn is_test_mode(&mut self) -> bool;
fn get_course_details(&self, course_id: usize) -> Result<CourseDetails, ClientError>;
fn get_course_details(&self, course_id: u32) -> Result<CourseDetails, ClientError>;
fn get_organization(&self, organization_slug: &str) -> Result<Organization, ClientError>;
fn update_exercises(
&mut self,
Expand All @@ -69,10 +71,11 @@ pub trait Client {
}

static SERVER_ADDRESS: &str = "https://tmc.mooc.fi";

impl ClientProduction {
pub fn new(test_mode: bool) -> Self {
let (tmc_client, _credentials) = tmc_langs::init_tmc_client_with_credentials(
SERVER_ADDRESS.to_string(),
Url::parse(SERVER_ADDRESS).expect("Server address should always be correct."),
PLUGIN,
"1.0.0",
)
Expand Down Expand Up @@ -165,12 +168,11 @@ impl Client for ClientProduction {
}

if let Some(credentials) = get_credentials() {
match self.tmc_client.set_token(credentials.token()) {
Ok(()) => return Ok(()),
_ => return Err("Setting login token failed".to_string()),
}
self.tmc_client.set_token(credentials.token());
Ok(())
} else {
Err("No login found. You need to be logged in to use this command".to_string())
}
Err("No login found. You need to be logged in to use this command".to_string())
}

fn try_login(&mut self, username: String, password: String) -> Result<String, String> {
Expand Down Expand Up @@ -256,7 +258,7 @@ impl Client for ClientProduction {
}
Ok(course_list)
}
Err(ClientError::NotLoggedIn) => {
Err(ClientError::NotAuthenticated) => {
Err("Login token is invalid. Please try logging in again.".to_string())
}
_ => Err("Unknown error. Please try again.".to_string()),
Expand Down Expand Up @@ -318,8 +320,8 @@ impl Client for ClientProduction {
credentials.remove().unwrap();
}

fn wait_for_submission(&self, submission_url: &str) -> Result<SubmissionFinished, ClientError> {
self.tmc_client.wait_for_submission(submission_url)
fn wait_for_submission(&self, submission_url: Url) -> Result<SubmissionFinished, ClientError> {
self.tmc_client.wait_for_submission_at(submission_url)
}
fn update_exercises(
&mut self,
Expand Down Expand Up @@ -359,7 +361,7 @@ impl Client for ClientProduction {
)
}

fn get_course_exercises(&mut self, course_id: usize) -> Result<Vec<CourseExercise>, String> {
fn get_course_exercises(&mut self, course_id: u32) -> Result<Vec<CourseExercise>, String> {
if self.test_mode {
return Ok(vec![CourseExercise {
id: 0,
Expand All @@ -376,7 +378,7 @@ impl Client for ClientProduction {
}
match self.tmc_client.get_course_exercises(course_id) {
Ok(exercises) => Ok(exercises),
Err(ClientError::NotLoggedIn) => {
Err(ClientError::NotAuthenticated) => {
Err("Login token is invalid. Please try logging in again.".to_string())
}
_ => Err("Unknown error. Please try again.".to_string()),
Expand All @@ -385,14 +387,15 @@ impl Client for ClientProduction {

fn get_exercise_details(
&mut self,
exercise_ids: Vec<usize>,
exercise_ids: Vec<u32>,
) -> Result<Vec<ExercisesDetails>, String> {
if self.test_mode {
return Ok(vec![ExercisesDetails {
id: 0,
course_name: "test_course".to_string(),
exercise_name: "test_exercise".to_string(),
checksum: "test_checksum".to_string(),
hide_submission_results: false,
}]);
}
match self.tmc_client.get_exercises_details(&exercise_ids) {
Expand All @@ -403,7 +406,7 @@ impl Client for ClientProduction {

fn download_or_update_exercises(
&mut self,
exercise_ids: &[usize],
exercise_ids: &[u32],
path: &Path,
) -> Result<DownloadResult, LangsError> {
if self.test_mode {
Expand All @@ -416,7 +419,7 @@ impl Client for ClientProduction {
tmc_langs::download_or_update_course_exercises(&self.tmc_client, path, exercise_ids, true)
}

fn get_course_details(&self, course_id: usize) -> Result<CourseDetails, ClientError> {
fn get_course_details(&self, course_id: u32) -> Result<CourseDetails, ClientError> {
if self.test_mode {
let course = Course {
id: 0,
Expand Down Expand Up @@ -496,11 +499,11 @@ pub fn set_organization(org: &str) -> Result<(), String> {
Ok(())
}

/// Returns course id as: Ok(Some(usize)) or Ok(None) if not found, Err(msg) if could not get id list
/// Returns course id as: Ok(Some(u32)) or Ok(None) if not found, Err(msg) if could not get id list
pub fn get_course_id_by_name(
client: &mut dyn Client,
course_name: String,
) -> Result<Option<usize>, String> {
) -> Result<Option<u32>, String> {
match client.list_courses() {
Ok(courses) => {
for course in courses {
Expand Down
33 changes: 14 additions & 19 deletions src/commands/courses_command.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::command_util::Client;
use crate::io_module::{Io, PrintColor};
use tmc_client::Course;
use tmc_client::response::Course;

/// Lists available courses from clients organization
pub fn list_courses(io: &mut dyn Io, client: &mut dyn Client) {
Expand All @@ -24,10 +24,14 @@ fn print_courses(io: &mut dyn Io, course_list: Vec<Course>) {
mod tests {
use super::*;
use isolang::Language;
use reqwest::Url;
use std::path::Path;
use std::slice::Iter;
use tmc_client::{ClientError, CourseExercise, NewSubmission, SubmissionStatus};
use tmc_client::{Organization, SubmissionFinished};
use tmc_client::response::{
CourseDetails, CourseExercise, ExercisesDetails, NewSubmission, Organization,
SubmissionFinished, SubmissionStatus,
};
use tmc_client::ClientError;
use tmc_langs::DownloadOrUpdateCourseExercisesResult;
use tmc_langs::DownloadResult;
use tmc_langs::LangsError;
Expand Down Expand Up @@ -142,7 +146,7 @@ mod tests {
}
fn wait_for_submission(
&self,
_submission_url: &str,
_submission_url: Url,
) -> Result<SubmissionFinished, ClientError> {
Ok(SubmissionFinished {
api_version: 0,
Expand Down Expand Up @@ -170,23 +174,20 @@ mod tests {
validations: None,
})
}
fn get_course_exercises(
&mut self,
_course_id: usize,
) -> Result<Vec<CourseExercise>, String> {
fn get_course_exercises(&mut self, _course_id: u32) -> Result<Vec<CourseExercise>, String> {
Ok(vec![])
}

fn get_exercise_details(
&mut self,
_exercise_ids: Vec<usize>,
) -> Result<Vec<tmc_client::ExercisesDetails>, String> {
_exercise_ids: Vec<u32>,
) -> Result<Vec<ExercisesDetails>, String> {
todo!()
}

fn download_or_update_exercises(
&mut self,
_download_params: &[usize],
_download_params: &[u32],
_path: &Path,
) -> Result<DownloadResult, LangsError> {
Ok(DownloadResult::Success {
Expand All @@ -195,16 +196,10 @@ mod tests {
})
}

fn get_course_details(
&self,
_: usize,
) -> std::result::Result<tmc_client::CourseDetails, tmc_client::ClientError> {
fn get_course_details(&self, _: u32) -> std::result::Result<CourseDetails, ClientError> {
todo!()
}
fn get_organization(
&self,
_: &str,
) -> std::result::Result<Organization, tmc_client::ClientError> {
fn get_organization(&self, _: &str) -> std::result::Result<Organization, ClientError> {
todo!()
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/commands/download_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::interactive;
use crate::io_module::{Io, PrintColor};
use crate::progress_reporting;
use crate::progress_reporting::ProgressBarManager;
use tmc_client::Course;
use tmc_client::response::Course;
use tmc_langs::ClientUpdateData;
use tmc_langs::DownloadResult;

Expand Down Expand Up @@ -149,7 +149,7 @@ pub fn download_exercises(
) -> Result<String, String> {
match client.get_course_exercises(course.id) {
Ok(exercises) => {
let exercise_ids: Vec<usize> = exercises
let exercise_ids: Vec<u32> = exercises
.iter()
.filter(|t| !t.disabled && t.unlocked)
.map(|t| t.id)
Expand Down
28 changes: 13 additions & 15 deletions src/commands/exercises_command.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::command_util::{get_course_id_by_name, Client};
use crate::io_module::{Io, PrintColor};

use tmc_client::CourseExercise;
use tmc_client::response::CourseExercise;

/// Lists exercises for a given course
pub fn list_exercises(io: &mut dyn Io, client: &mut dyn Client, course_name: String) {
Expand Down Expand Up @@ -94,12 +94,13 @@ fn print_exercises(io: &mut dyn Io, course_name: String, exercises: Vec<CourseEx
#[cfg(test)]
mod tests {
use isolang::Language;
use reqwest::Url;
use std::path::Path;
use tmc_client::Course;
use tmc_client::Organization;
use tmc_client::{
ClientError, CourseExercise, NewSubmission, SubmissionFinished, SubmissionStatus,
use tmc_client::response::{
Course, CourseDetails, CourseExercise, ExercisesDetails, NewSubmission, Organization,
SubmissionFinished, SubmissionStatus,
};
use tmc_client::ClientError;
use tmc_langs::DownloadOrUpdateCourseExercisesResult;
use tmc_langs::DownloadResult;
use tmc_langs::LangsError;
Expand Down Expand Up @@ -211,7 +212,7 @@ mod tests {
}
fn wait_for_submission(
&self,
_submission_url: &str,
_submission_url: Url,
) -> Result<SubmissionFinished, ClientError> {
Ok(SubmissionFinished {
api_version: 0,
Expand Down Expand Up @@ -239,10 +240,7 @@ mod tests {
validations: None,
})
}
fn get_course_exercises(
&mut self,
_course_id: usize,
) -> Result<Vec<CourseExercise>, String> {
fn get_course_exercises(&mut self, _course_id: u32) -> Result<Vec<CourseExercise>, String> {
/*TODO: ExercisePoint is in private module*/
//let points = vec![];
//let awarded_points = vec![/*"1.1".to_string()*/];
Expand Down Expand Up @@ -316,8 +314,8 @@ mod tests {

fn get_exercise_details(
&mut self,
_exercise_ids: Vec<usize>,
) -> Result<Vec<tmc_client::ExercisesDetails>, String> {
_exercise_ids: Vec<u32>,
) -> Result<Vec<ExercisesDetails>, String> {
todo!()
}
fn update_exercises(
Expand All @@ -328,7 +326,7 @@ mod tests {
}
fn download_or_update_exercises(
&mut self,
_download_params: &[usize],
_download_params: &[u32],
_path: &Path,
) -> Result<DownloadResult, LangsError> {
Ok(DownloadResult::Success {
Expand All @@ -339,8 +337,8 @@ mod tests {

fn get_course_details(
&self,
_: usize,
) -> std::result::Result<tmc_client::CourseDetails, tmc_client::ClientError> {
_: u32,
) -> std::result::Result<CourseDetails, tmc_client::ClientError> {
todo!()
}
fn get_organization(
Expand Down
2 changes: 1 addition & 1 deletion src/commands/login_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ mod tests {
use super::super::command_util::*;
use super::*;
use std::slice::Iter;
use tmc_client::Organization;
use tmc_client::response::Organization;

pub struct IoTest<'a> {
list: &'a mut Vec<String>,
Expand Down
8 changes: 7 additions & 1 deletion src/commands/submit_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::io_module::{Io, PrintColor};
use crate::progress_reporting;
use crate::progress_reporting::ProgressBarManager;
use anyhow::{Context, Result};
use reqwest::Url;
use tmc_langs::ClientUpdateData;
use tmc_langs::Language;
use tmc_langs::NewSubmission;
Expand Down Expand Up @@ -66,7 +67,12 @@ fn submit_logic(io: &mut dyn Io, client: &mut dyn Client, path: Option<&str>) {
new_submission.show_submission_url
));

match client.wait_for_submission(&new_submission.submission_url) {
let submission_url = Url::parse(&new_submission.submission_url);
if let Err(err) = submission_url {
io.println(&err.to_string(), PrintColor::Failed);
return;
}
match client.wait_for_submission(submission_url.unwrap()) {
Ok(submission_finished) => {
manager.join();

Expand Down
1 change: 0 additions & 1 deletion src/updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ fn checktemp() {
std::io::ErrorKind::PermissionDenied => {
println!("Permission Denied! Restarting with administrator privileges...");
elevate("cleartemp".to_string());
return;
}
_ => {
println!("{:#?}", e);
Expand Down

0 comments on commit ec57ba3

Please sign in to comment.