Skip to content

Commit ec57ba3

Browse files
authored
Merge pull request #10 from rage/update-langs
Update tmc-langs
2 parents 0014f12 + 0aec935 commit ec57ba3

9 files changed

Lines changed: 376 additions & 498 deletions

File tree

Cargo.lock

Lines changed: 307 additions & 431 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@ serde = "1"
2222
serde_json = "1"
2323
log = "0.4"
2424
dirs = "3"
25-
tmc-langs-util = { git = "https://github.com/rage/tmc-langs-rust/", rev = "13f3d55" }
26-
tmc-langs-framework = { git = "https://github.com/rage/tmc-langs-rust/", rev = "13f3d55" }
27-
tmc-langs = { git = "https://github.com/rage/tmc-langs-rust/", rev = "13f3d55" }
28-
tmc-client = { git = "https://github.com/rage/tmc-langs-rust/", rev = "13f3d55" }
25+
# rev 74dcb3e = 0.23.2
26+
tmc-langs-util = { git = "https://github.com/rage/tmc-langs-rust/", rev = "74dcb3e" }
27+
tmc-langs-framework = { git = "https://github.com/rage/tmc-langs-rust/", rev = "74dcb3e" }
28+
tmc-langs = { git = "https://github.com/rage/tmc-langs-rust/", rev = "74dcb3e" }
29+
tmc-client = { git = "https://github.com/rage/tmc-langs-rust/", rev = "74dcb3e" }
2930
rpassword = "5.0.1"
3031
mockall = "0.9.0"
3132
indicatif = "0.15.0"

src/commands/command_util.rs

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
use isolang::Language;
2+
use reqwest::Url;
23
use std::path::Path;
34
use std::path::PathBuf;
45

56
use std::env;
6-
use tmc_client::{
7-
ClientError, Course, CourseDetails, CourseExercise, ExercisesDetails, NewSubmission,
8-
Organization, SubmissionFinished, TmcClient, Token,
7+
use tmc_client::response::{
8+
Course, CourseDetails, CourseExercise, ExercisesDetails, NewSubmission, Organization,
9+
SubmissionFinished,
910
};
11+
use tmc_client::{ClientError, TmcClient, Token};
1012
use tmc_langs::Credentials;
1113
use tmc_langs::DownloadOrUpdateCourseExercisesResult;
1214
use tmc_langs::DownloadResult;
@@ -33,26 +35,26 @@ pub trait Client {
3335
fn list_courses(&mut self) -> Result<Vec<Course>, String>;
3436
fn get_organizations(&mut self) -> Result<Vec<Organization>, String>;
3537
fn logout(&mut self);
36-
fn wait_for_submission(&self, submission_url: &str) -> Result<SubmissionFinished, ClientError>;
38+
fn wait_for_submission(&self, submission_url: Url) -> Result<SubmissionFinished, ClientError>;
3739
fn submit(
3840
&self,
3941
projects_dir: &Path,
4042
course_slug: &str,
4143
exercise_slug: &str,
4244
locale: Option<Language>,
4345
) -> Result<NewSubmission, LangsError>;
44-
fn get_course_exercises(&mut self, course_id: usize) -> Result<Vec<CourseExercise>, String>;
46+
fn get_course_exercises(&mut self, course_id: u32) -> Result<Vec<CourseExercise>, String>;
4547
fn get_exercise_details(
4648
&mut self,
47-
exercise_ids: Vec<usize>,
49+
exercise_ids: Vec<u32>,
4850
) -> Result<Vec<ExercisesDetails>, String>;
4951
fn download_or_update_exercises(
5052
&mut self,
51-
download_params: &[usize],
53+
download_params: &[u32],
5254
path: &Path,
5355
) -> Result<DownloadResult, LangsError>;
5456
fn is_test_mode(&mut self) -> bool;
55-
fn get_course_details(&self, course_id: usize) -> Result<CourseDetails, ClientError>;
57+
fn get_course_details(&self, course_id: u32) -> Result<CourseDetails, ClientError>;
5658
fn get_organization(&self, organization_slug: &str) -> Result<Organization, ClientError>;
5759
fn update_exercises(
5860
&mut self,
@@ -69,10 +71,11 @@ pub trait Client {
6971
}
7072

7173
static SERVER_ADDRESS: &str = "https://tmc.mooc.fi";
74+
7275
impl ClientProduction {
7376
pub fn new(test_mode: bool) -> Self {
7477
let (tmc_client, _credentials) = tmc_langs::init_tmc_client_with_credentials(
75-
SERVER_ADDRESS.to_string(),
78+
Url::parse(SERVER_ADDRESS).expect("Server address should always be correct."),
7679
PLUGIN,
7780
"1.0.0",
7881
)
@@ -165,12 +168,11 @@ impl Client for ClientProduction {
165168
}
166169

167170
if let Some(credentials) = get_credentials() {
168-
match self.tmc_client.set_token(credentials.token()) {
169-
Ok(()) => return Ok(()),
170-
_ => return Err("Setting login token failed".to_string()),
171-
}
171+
self.tmc_client.set_token(credentials.token());
172+
Ok(())
173+
} else {
174+
Err("No login found. You need to be logged in to use this command".to_string())
172175
}
173-
Err("No login found. You need to be logged in to use this command".to_string())
174176
}
175177

176178
fn try_login(&mut self, username: String, password: String) -> Result<String, String> {
@@ -256,7 +258,7 @@ impl Client for ClientProduction {
256258
}
257259
Ok(course_list)
258260
}
259-
Err(ClientError::NotLoggedIn) => {
261+
Err(ClientError::NotAuthenticated) => {
260262
Err("Login token is invalid. Please try logging in again.".to_string())
261263
}
262264
_ => Err("Unknown error. Please try again.".to_string()),
@@ -318,8 +320,8 @@ impl Client for ClientProduction {
318320
credentials.remove().unwrap();
319321
}
320322

321-
fn wait_for_submission(&self, submission_url: &str) -> Result<SubmissionFinished, ClientError> {
322-
self.tmc_client.wait_for_submission(submission_url)
323+
fn wait_for_submission(&self, submission_url: Url) -> Result<SubmissionFinished, ClientError> {
324+
self.tmc_client.wait_for_submission_at(submission_url)
323325
}
324326
fn update_exercises(
325327
&mut self,
@@ -359,7 +361,7 @@ impl Client for ClientProduction {
359361
)
360362
}
361363

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

386388
fn get_exercise_details(
387389
&mut self,
388-
exercise_ids: Vec<usize>,
390+
exercise_ids: Vec<u32>,
389391
) -> Result<Vec<ExercisesDetails>, String> {
390392
if self.test_mode {
391393
return Ok(vec![ExercisesDetails {
392394
id: 0,
393395
course_name: "test_course".to_string(),
394396
exercise_name: "test_exercise".to_string(),
395397
checksum: "test_checksum".to_string(),
398+
hide_submission_results: false,
396399
}]);
397400
}
398401
match self.tmc_client.get_exercises_details(&exercise_ids) {
@@ -403,7 +406,7 @@ impl Client for ClientProduction {
403406

404407
fn download_or_update_exercises(
405408
&mut self,
406-
exercise_ids: &[usize],
409+
exercise_ids: &[u32],
407410
path: &Path,
408411
) -> Result<DownloadResult, LangsError> {
409412
if self.test_mode {
@@ -416,7 +419,7 @@ impl Client for ClientProduction {
416419
tmc_langs::download_or_update_course_exercises(&self.tmc_client, path, exercise_ids, true)
417420
}
418421

419-
fn get_course_details(&self, course_id: usize) -> Result<CourseDetails, ClientError> {
422+
fn get_course_details(&self, course_id: u32) -> Result<CourseDetails, ClientError> {
420423
if self.test_mode {
421424
let course = Course {
422425
id: 0,
@@ -496,11 +499,11 @@ pub fn set_organization(org: &str) -> Result<(), String> {
496499
Ok(())
497500
}
498501

499-
/// Returns course id as: Ok(Some(usize)) or Ok(None) if not found, Err(msg) if could not get id list
502+
/// Returns course id as: Ok(Some(u32)) or Ok(None) if not found, Err(msg) if could not get id list
500503
pub fn get_course_id_by_name(
501504
client: &mut dyn Client,
502505
course_name: String,
503-
) -> Result<Option<usize>, String> {
506+
) -> Result<Option<u32>, String> {
504507
match client.list_courses() {
505508
Ok(courses) => {
506509
for course in courses {

src/commands/courses_command.rs

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::command_util::Client;
22
use crate::io_module::{Io, PrintColor};
3-
use tmc_client::Course;
3+
use tmc_client::response::Course;
44

55
/// Lists available courses from clients organization
66
pub fn list_courses(io: &mut dyn Io, client: &mut dyn Client) {
@@ -24,10 +24,14 @@ fn print_courses(io: &mut dyn Io, course_list: Vec<Course>) {
2424
mod tests {
2525
use super::*;
2626
use isolang::Language;
27+
use reqwest::Url;
2728
use std::path::Path;
2829
use std::slice::Iter;
29-
use tmc_client::{ClientError, CourseExercise, NewSubmission, SubmissionStatus};
30-
use tmc_client::{Organization, SubmissionFinished};
30+
use tmc_client::response::{
31+
CourseDetails, CourseExercise, ExercisesDetails, NewSubmission, Organization,
32+
SubmissionFinished, SubmissionStatus,
33+
};
34+
use tmc_client::ClientError;
3135
use tmc_langs::DownloadOrUpdateCourseExercisesResult;
3236
use tmc_langs::DownloadResult;
3337
use tmc_langs::LangsError;
@@ -142,7 +146,7 @@ mod tests {
142146
}
143147
fn wait_for_submission(
144148
&self,
145-
_submission_url: &str,
149+
_submission_url: Url,
146150
) -> Result<SubmissionFinished, ClientError> {
147151
Ok(SubmissionFinished {
148152
api_version: 0,
@@ -170,23 +174,20 @@ mod tests {
170174
validations: None,
171175
})
172176
}
173-
fn get_course_exercises(
174-
&mut self,
175-
_course_id: usize,
176-
) -> Result<Vec<CourseExercise>, String> {
177+
fn get_course_exercises(&mut self, _course_id: u32) -> Result<Vec<CourseExercise>, String> {
177178
Ok(vec![])
178179
}
179180

180181
fn get_exercise_details(
181182
&mut self,
182-
_exercise_ids: Vec<usize>,
183-
) -> Result<Vec<tmc_client::ExercisesDetails>, String> {
183+
_exercise_ids: Vec<u32>,
184+
) -> Result<Vec<ExercisesDetails>, String> {
184185
todo!()
185186
}
186187

187188
fn download_or_update_exercises(
188189
&mut self,
189-
_download_params: &[usize],
190+
_download_params: &[u32],
190191
_path: &Path,
191192
) -> Result<DownloadResult, LangsError> {
192193
Ok(DownloadResult::Success {
@@ -195,16 +196,10 @@ mod tests {
195196
})
196197
}
197198

198-
fn get_course_details(
199-
&self,
200-
_: usize,
201-
) -> std::result::Result<tmc_client::CourseDetails, tmc_client::ClientError> {
199+
fn get_course_details(&self, _: u32) -> std::result::Result<CourseDetails, ClientError> {
202200
todo!()
203201
}
204-
fn get_organization(
205-
&self,
206-
_: &str,
207-
) -> std::result::Result<Organization, tmc_client::ClientError> {
202+
fn get_organization(&self, _: &str) -> std::result::Result<Organization, ClientError> {
208203
todo!()
209204
}
210205
}

src/commands/download_command.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::interactive;
77
use crate::io_module::{Io, PrintColor};
88
use crate::progress_reporting;
99
use crate::progress_reporting::ProgressBarManager;
10-
use tmc_client::Course;
10+
use tmc_client::response::Course;
1111
use tmc_langs::ClientUpdateData;
1212
use tmc_langs::DownloadResult;
1313

@@ -149,7 +149,7 @@ pub fn download_exercises(
149149
) -> Result<String, String> {
150150
match client.get_course_exercises(course.id) {
151151
Ok(exercises) => {
152-
let exercise_ids: Vec<usize> = exercises
152+
let exercise_ids: Vec<u32> = exercises
153153
.iter()
154154
.filter(|t| !t.disabled && t.unlocked)
155155
.map(|t| t.id)

src/commands/exercises_command.rs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use super::command_util::{get_course_id_by_name, Client};
22
use crate::io_module::{Io, PrintColor};
33

4-
use tmc_client::CourseExercise;
4+
use tmc_client::response::CourseExercise;
55

66
/// Lists exercises for a given course
77
pub fn list_exercises(io: &mut dyn Io, client: &mut dyn Client, course_name: String) {
@@ -94,12 +94,13 @@ fn print_exercises(io: &mut dyn Io, course_name: String, exercises: Vec<CourseEx
9494
#[cfg(test)]
9595
mod tests {
9696
use isolang::Language;
97+
use reqwest::Url;
9798
use std::path::Path;
98-
use tmc_client::Course;
99-
use tmc_client::Organization;
100-
use tmc_client::{
101-
ClientError, CourseExercise, NewSubmission, SubmissionFinished, SubmissionStatus,
99+
use tmc_client::response::{
100+
Course, CourseDetails, CourseExercise, ExercisesDetails, NewSubmission, Organization,
101+
SubmissionFinished, SubmissionStatus,
102102
};
103+
use tmc_client::ClientError;
103104
use tmc_langs::DownloadOrUpdateCourseExercisesResult;
104105
use tmc_langs::DownloadResult;
105106
use tmc_langs::LangsError;
@@ -211,7 +212,7 @@ mod tests {
211212
}
212213
fn wait_for_submission(
213214
&self,
214-
_submission_url: &str,
215+
_submission_url: Url,
215216
) -> Result<SubmissionFinished, ClientError> {
216217
Ok(SubmissionFinished {
217218
api_version: 0,
@@ -239,10 +240,7 @@ mod tests {
239240
validations: None,
240241
})
241242
}
242-
fn get_course_exercises(
243-
&mut self,
244-
_course_id: usize,
245-
) -> Result<Vec<CourseExercise>, String> {
243+
fn get_course_exercises(&mut self, _course_id: u32) -> Result<Vec<CourseExercise>, String> {
246244
/*TODO: ExercisePoint is in private module*/
247245
//let points = vec![];
248246
//let awarded_points = vec![/*"1.1".to_string()*/];
@@ -316,8 +314,8 @@ mod tests {
316314

317315
fn get_exercise_details(
318316
&mut self,
319-
_exercise_ids: Vec<usize>,
320-
) -> Result<Vec<tmc_client::ExercisesDetails>, String> {
317+
_exercise_ids: Vec<u32>,
318+
) -> Result<Vec<ExercisesDetails>, String> {
321319
todo!()
322320
}
323321
fn update_exercises(
@@ -328,7 +326,7 @@ mod tests {
328326
}
329327
fn download_or_update_exercises(
330328
&mut self,
331-
_download_params: &[usize],
329+
_download_params: &[u32],
332330
_path: &Path,
333331
) -> Result<DownloadResult, LangsError> {
334332
Ok(DownloadResult::Success {
@@ -339,8 +337,8 @@ mod tests {
339337

340338
fn get_course_details(
341339
&self,
342-
_: usize,
343-
) -> std::result::Result<tmc_client::CourseDetails, tmc_client::ClientError> {
340+
_: u32,
341+
) -> std::result::Result<CourseDetails, tmc_client::ClientError> {
344342
todo!()
345343
}
346344
fn get_organization(

src/commands/login_command.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ mod tests {
132132
use super::super::command_util::*;
133133
use super::*;
134134
use std::slice::Iter;
135-
use tmc_client::Organization;
135+
use tmc_client::response::Organization;
136136

137137
pub struct IoTest<'a> {
138138
list: &'a mut Vec<String>,

src/commands/submit_command.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::io_module::{Io, PrintColor};
44
use crate::progress_reporting;
55
use crate::progress_reporting::ProgressBarManager;
66
use anyhow::{Context, Result};
7+
use reqwest::Url;
78
use tmc_langs::ClientUpdateData;
89
use tmc_langs::Language;
910
use tmc_langs::NewSubmission;
@@ -66,7 +67,12 @@ fn submit_logic(io: &mut dyn Io, client: &mut dyn Client, path: Option<&str>) {
6667
new_submission.show_submission_url
6768
));
6869

69-
match client.wait_for_submission(&new_submission.submission_url) {
70+
let submission_url = Url::parse(&new_submission.submission_url);
71+
if let Err(err) = submission_url {
72+
io.println(&err.to_string(), PrintColor::Failed);
73+
return;
74+
}
75+
match client.wait_for_submission(submission_url.unwrap()) {
7076
Ok(submission_finished) => {
7177
manager.join();
7278

src/updater.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ fn checktemp() {
4747
std::io::ErrorKind::PermissionDenied => {
4848
println!("Permission Denied! Restarting with administrator privileges...");
4949
elevate("cleartemp".to_string());
50-
return;
5150
}
5251
_ => {
5352
println!("{:#?}", e);

0 commit comments

Comments
 (0)