Skip to content

Commit 7bcd043

Browse files
committed
comments, cleanup
1 parent ac0e013 commit 7bcd043

File tree

25 files changed

+131
-51
lines changed

25 files changed

+131
-51
lines changed

tmc-langs-core/src/error.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! The core error type.
2+
13
use crate::response;
24
use reqwest::StatusCode;
35
use std::path::PathBuf;

tmc-langs-core/src/lib.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ mod response;
66
mod tmc_core;
77

88
pub use error::CoreError;
9+
pub use isolang::Language;
910
pub use request::FeedbackAnswer;
1011
pub use response::{
11-
Course, CourseDetails, CourseExercise, ExerciseDetails, NewSubmission, NuCourse,
12-
NuCourseExercise, NuExercisePoint, Organization, Review, Submission,
13-
SubmissionFeedbackResponse, User,
12+
Course, CourseData, CourseDataExercise, CourseDataExercisePoint, CourseDetails, CourseExercise,
13+
ExerciseDetails, NewSubmission, Organization, Review, Submission, SubmissionFeedbackResponse,
14+
User,
1415
};
1516
pub use tmc_core::TmcCore;
17+
pub use tmc_langs_util::{RunResult, ValidationResult};

tmc-langs-core/src/response.rs

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Contains types which model the JSON responses from tmc-server
22
33
use crate::CoreError;
4+
45
use lazy_static::lazy_static;
56
use regex::Regex;
67
use serde::{
@@ -12,7 +13,7 @@ use std::str::FromStr;
1213
use thiserror::Error;
1314

1415
/// Models the responses from tmc-server, which can either
15-
/// be some successful response or a list of errors
16+
/// be some successful response, a single error or a list of errors
1617
#[derive(Debug, Deserialize)]
1718
#[serde(untagged)]
1819
pub enum Response<T> {
@@ -34,7 +35,7 @@ impl<T> Response<T> {
3435

3536
/// Represents an error response from tmc-server
3637
#[derive(Debug, Error, Deserialize)]
37-
#[error("Response contained an error: {errors:#?}")]
38+
#[error("Response contained errors: {errors:#?}")]
3839
pub struct ResponseErrors {
3940
pub errors: Vec<String>,
4041
}
@@ -84,7 +85,7 @@ pub struct Course {
8485
}
8586

8687
#[derive(Debug, Deserialize)]
87-
pub struct NuCourse {
88+
pub struct CourseData {
8889
pub name: String,
8990
//pub hide_after
9091
pub hidden: bool,
@@ -186,7 +187,7 @@ pub struct CourseExercise {
186187
}
187188

188189
#[derive(Debug, Deserialize)]
189-
pub struct NuCourseExercise {
190+
pub struct CourseDataExercise {
190191
pub id: usize,
191192
pub available_points: Vec<ExercisePoint>,
192193
pub name: String,
@@ -205,7 +206,7 @@ pub struct ExercisePoint {
205206
}
206207

207208
#[derive(Debug, Deserialize)]
208-
pub struct NuExercisePoint {
209+
pub struct CourseDataExercisePoint {
209210
awarded_point: AwardedPoint,
210211
exercise_id: usize,
211212
}
@@ -353,21 +354,18 @@ pub enum SubmissionFeedbackKind {
353354
IntRange { lower: usize, upper: usize },
354355
}
355356

356-
// parses "text" into Text, and intrange[x..y] into IntRange {lower: x, upper: y}
357357
impl<'de> Deserialize<'de> for SubmissionFeedbackKind {
358358
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
359359
where
360360
D: Deserializer<'de>,
361361
{
362-
log::debug!("res");
363-
let res = deserializer.deserialize_string(SubmissionFeedbackKindVisitor {});
364-
log::debug!("{:?}", res);
365-
res
362+
deserializer.deserialize_string(SubmissionFeedbackKindVisitor {})
366363
}
367364
}
368365

369366
struct SubmissionFeedbackKindVisitor {}
370367

368+
// parses "text" into Text, and "intrange[x..y]" into IntRange {lower: x, upper: y}
371369
impl<'de> Visitor<'de> for SubmissionFeedbackKindVisitor {
372370
type Value = SubmissionFeedbackKind;
373371

@@ -386,11 +384,19 @@ impl<'de> Visitor<'de> for SubmissionFeedbackKindVisitor {
386384
Ok(SubmissionFeedbackKind::Text)
387385
} else if let Some(captures) = RANGE.captures(&value) {
388386
let lower = &captures[1];
389-
let lower = usize::from_str(lower)
390-
.map_err(|e| E::custom(format!("error parsing lower bound {}: {}", lower, e)))?;
387+
let lower = usize::from_str(lower).map_err(|e| {
388+
E::custom(format!(
389+
"error parsing intrange lower bound {}: {}",
390+
lower, e
391+
))
392+
})?;
391393
let upper = &captures[2];
392-
let upper = usize::from_str(upper)
393-
.map_err(|e| E::custom(format!("error parsing upper bound {}: {}", upper, e)))?;
394+
let upper = usize::from_str(upper).map_err(|e| {
395+
E::custom(format!(
396+
"error parsing intrange upper bound {}: {}",
397+
upper, e
398+
))
399+
})?;
394400
Ok(SubmissionFeedbackKind::IntRange { lower, upper })
395401
} else {
396402
Err(E::custom("expected \"text\" or \"intrange[x..y]\""))
@@ -460,10 +466,16 @@ mod test {
460466

461467
let text = serde_json::json!("text");
462468
let text: SubmissionFeedbackKind = serde_json::from_value(text).unwrap();
463-
log::debug!("{:?}", text);
469+
if let SubmissionFeedbackKind::Text = text {
470+
} else {
471+
panic!("wrong type")
472+
}
464473

465474
let intrange = serde_json::json!("intrange[1..5]");
466475
let intrange: SubmissionFeedbackKind = serde_json::from_value(intrange).unwrap();
467-
log::debug!("{:?}", intrange);
476+
if let SubmissionFeedbackKind::IntRange { lower: 1, upper: 5 } = intrange {
477+
} else {
478+
panic!("wrong type")
479+
}
468480
}
469481
}

tmc-langs-core/src/tmc_core.rs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use crate::error::{CoreError, Result};
44
use crate::request::*;
55
use crate::response::*;
66
use crate::response::{Course, CourseDetails, Organization};
7+
use crate::{Language, RunResult, ValidationResult};
78

8-
use isolang::Language;
99
use oauth2::basic::BasicClient;
1010
use oauth2::prelude::*;
1111
use oauth2::{
@@ -21,7 +21,6 @@ use std::path::Path;
2121
use std::path::PathBuf;
2222
use tempfile::NamedTempFile;
2323
use tmc_langs_util::task_executor;
24-
use tmc_langs_util::{RunResult, ValidationResult};
2524
use url1::Url as Url1;
2625

2726
pub type Token =
@@ -40,7 +39,12 @@ pub struct TmcCore {
4039
impl TmcCore {
4140
pub fn new(config_dir: PathBuf, root_url: String) -> Result<Self> {
4241
// guarantee a trailing slash, otherwise join will drop the last component
43-
let tmc_url = Url::parse(&format!("{}/", root_url))?;
42+
let root_url = if root_url.ends_with('/') {
43+
root_url
44+
} else {
45+
format!("{}/", root_url)
46+
};
47+
let tmc_url = Url::parse(&root_url)?;
4448
let api_url = tmc_url.join("api/v8/")?;
4549
let auth_url = tmc_url.join("oauth/token")?;
4650
Ok(Self {
@@ -94,14 +98,17 @@ impl TmcCore {
9498
Ok(())
9599
}
96100

101+
/// Fetches all organizations.
97102
pub fn get_organizations(&self) -> Result<Vec<Organization>> {
98103
self.organizations()
99104
}
100105

106+
/// UNIMPLEMENTED
101107
pub fn send_diagnostics(&self) {
102108
unimplemented!()
103109
}
104110

111+
/// Downloads the given exercises.
105112
pub fn download_or_update_exercises(&self, exercises: Vec<(usize, &Path)>) -> Result<()> {
106113
for (exercise_id, target) in exercises {
107114
let zip_file = NamedTempFile::new().map_err(CoreError::TempFile)?;
@@ -111,14 +118,17 @@ impl TmcCore {
111118
Ok(())
112119
}
113120

121+
/// Fetches the course's information.
114122
pub fn get_course_details(&self, course_id: usize) -> Result<CourseDetails> {
115123
self.core_course(course_id)
116124
}
117125

126+
/// Fetches all courses under the given organization.
118127
pub fn list_courses(&self, organization_slug: &str) -> Result<Vec<Course>> {
119128
self.organization_courses(organization_slug)
120129
}
121130

131+
/// Sends the given submission as a paste.
122132
pub fn paste_with_comment(
123133
&self,
124134
submission_url: Url,
@@ -135,6 +145,7 @@ impl TmcCore {
135145
self.post_submission_to_paste(submission_url, file.path(), paste_message, locale)
136146
}
137147

148+
/// Runs checkstyle for the project.
138149
pub fn run_checkstyle(
139150
&self,
140151
path: &Path,
@@ -143,10 +154,12 @@ impl TmcCore {
143154
Ok(task_executor::run_check_code_style(path, locale)?)
144155
}
145156

157+
/// Runs tests for the project.
146158
pub fn run_tests(&self, path: &Path) -> Result<RunResult> {
147159
Ok(task_executor::run_tests(path)?)
148160
}
149161

162+
/// Sends feedback.
150163
pub fn send_feedback(
151164
&self,
152165
feedback_url: Url,
@@ -155,10 +168,12 @@ impl TmcCore {
155168
self.post_feedback(feedback_url, feedback)
156169
}
157170

171+
/// UNIMPLEMENTED
158172
pub fn send_snapshot_events(&self) {
159173
unimplemented!()
160174
}
161175

176+
/// Sends the submission to the server.
162177
pub fn submit(
163178
&self,
164179
submission_url: Url,
@@ -205,14 +220,17 @@ impl TmcCore {
205220
})
206221
}
207222

223+
/// Mark the review as read on the server.
208224
pub fn mark_review_as_read(&self, review_update_url: String) -> Result<()> {
209225
self.mark_review(review_update_url, true)
210226
}
211227

228+
/// Fetches all reviews.
212229
pub fn get_unread_reviews(&self, reviews_url: Url) -> Result<Vec<Review>> {
213230
self.get_json_from_url(reviews_url)
214231
}
215232

233+
/// Request code review.
216234
pub fn request_code_review(
217235
&self,
218236
submission_url: Url,
@@ -229,13 +247,15 @@ impl TmcCore {
229247
self.post_submission_for_review(submission_url, file.path(), message_for_reviewer, locale)
230248
}
231249

250+
/// Downloads the model solution from the given url.
232251
pub fn download_model_solution(&self, solution_download_url: Url, target: &Path) -> Result<()> {
233252
let zip_file = NamedTempFile::new().map_err(CoreError::TempFile)?;
234253
self.download_from(solution_download_url, zip_file.path())?;
235254
task_executor::extract_project(zip_file.path(), target)?;
236255
Ok(())
237256
}
238257

258+
/// Checks the status of a submission on the server.
239259
pub fn check_submission(&self, submission_url: &str) -> Result<SubmissionProcessingStatus> {
240260
if self.token.is_none() {
241261
return Err(CoreError::AuthRequired);
@@ -247,6 +267,7 @@ impl TmcCore {
247267
Ok(res)
248268
}
249269

270+
// convenience function for requesting JSON data from the TMC server
250271
fn request_json<T: DeserializeOwned + Debug>(&self, url: Url) -> Result<T> {
251272
log::debug!("requesting {}", url);
252273
let mut req = self.client.get(url);

0 commit comments

Comments
 (0)