@@ -23,9 +23,10 @@ pub type Token =
2323 oauth2:: StandardTokenResponse < oauth2:: EmptyExtraTokenFields , oauth2:: basic:: BasicTokenType > ;
2424
2525#[ derive( Debug , Serialize ) ]
26- pub enum StatusUpdate {
27- Processing ( & ' static str , f64 ) ,
28- Finished ,
26+ pub struct StatusUpdate {
27+ finished : bool ,
28+ message : & ' static str ,
29+ percent_done : f64 ,
2930}
3031
3132/// A struct for interacting with the TestMyCode service, including authentication
@@ -92,23 +93,35 @@ impl TmcCore {
9293 Self :: new ( config_dir, root_url)
9394 }
9495
96+ pub fn set_token ( & mut self , token : Token ) {
97+ self . token = Some ( token) ;
98+ }
99+
95100 pub fn set_progress_report < F > ( & mut self , progress_report : F )
96101 where
97102 F : Fn ( StatusUpdate ) + ' static ,
98103 {
99104 self . progress_report = Some ( Box :: new ( progress_report) ) ;
100105 }
101106
102- pub fn report_progress ( & self , msg : & ' static str , progress : f64 ) {
103- self . progress_report
104- . as_ref ( )
105- . map ( |f| f ( StatusUpdate :: Processing ( msg, progress) ) ) ;
107+ pub fn report_progress ( & self , message : & ' static str , percent_done : f64 ) {
108+ self . progress_report . as_ref ( ) . map ( |f| {
109+ f ( StatusUpdate {
110+ finished : false ,
111+ message,
112+ percent_done,
113+ } )
114+ } ) ;
106115 }
107116
108- pub fn report_complete ( & self ) {
109- self . progress_report
110- . as_ref ( )
111- . map ( |f| f ( StatusUpdate :: Finished ) ) ;
117+ pub fn report_complete ( & self , message : & ' static str ) {
118+ self . progress_report . as_ref ( ) . map ( |f| {
119+ f ( StatusUpdate {
120+ finished : true ,
121+ message,
122+ percent_done : 1.0 ,
123+ } )
124+ } ) ;
112125 }
113126
114127 /// Attempts to log in with the given credentials, returns an error if an authentication token is already present.
@@ -131,7 +144,7 @@ impl TmcCore {
131144 client_name : & str ,
132145 email : String ,
133146 password : String ,
134- ) -> Result < ( ) > {
147+ ) -> Result < Token > {
135148 if self . token . is_some ( ) {
136149 return Err ( CoreError :: AlreadyAuthenticated ) ;
137150 }
@@ -161,9 +174,9 @@ impl TmcCore {
161174 & ResourceOwnerPassword :: new ( password) ,
162175 )
163176 . request ( oauth2:: reqwest:: http_client) ?;
164- self . token = Some ( token) ;
177+ self . token = Some ( token. clone ( ) ) ;
165178 log:: debug!( "authenticated" ) ;
166- Ok ( ( ) )
179+ Ok ( token )
167180 }
168181
169182 /// Fetches all organizations.
@@ -247,6 +260,9 @@ impl TmcCore {
247260 /// let courses = core.list_courses("hy").unwrap();
248261 /// ```
249262 pub fn list_courses ( & self , organization_slug : & str ) -> Result < Vec < Course > > {
263+ if self . token . is_none ( ) {
264+ return Err ( CoreError :: AuthRequired ) ;
265+ }
250266 self . organization_courses ( organization_slug)
251267 }
252268
@@ -285,7 +301,7 @@ impl TmcCore {
285301 file. write_all ( & compressed)
286302 . map_err ( |e| CoreError :: FileWrite ( file. path ( ) . to_path_buf ( ) , e) ) ?;
287303
288- self . post_submission_to_paste ( submission_url, file. path ( ) , paste_message, locale)
304+ self . post_submission_to_paste ( submission_url, file. path ( ) , paste_message, Some ( locale) )
289305 }
290306
291307 /// Checks the coding style for the project.
@@ -354,7 +370,7 @@ impl TmcCore {
354370 & self ,
355371 submission_url : Url ,
356372 submission_path : & Path ,
357- locale : Language ,
373+ locale : Option < Language > ,
358374 ) -> Result < NewSubmission > {
359375 // compress
360376 self . report_progress ( "Submitting exercise. Compressing submission..." , 0.0 ) ;
@@ -367,7 +383,7 @@ impl TmcCore {
367383 self . report_progress ( "Wrote compressed data. Posting submission..." , 0.75 ) ;
368384
369385 let result = self . post_submission ( submission_url, file. path ( ) , locale) ;
370- self . report_complete ( ) ;
386+ self . report_complete ( "Submission finished!" ) ;
371387 result
372388 }
373389
@@ -450,7 +466,12 @@ impl TmcCore {
450466 file. write_all ( & compressed)
451467 . map_err ( |e| CoreError :: FileWrite ( file. path ( ) . to_path_buf ( ) , e) ) ?;
452468
453- self . post_submission_for_review ( submission_url, file. path ( ) , message_for_reviewer, locale)
469+ self . post_submission_for_review (
470+ submission_url,
471+ file. path ( ) ,
472+ message_for_reviewer,
473+ Some ( locale) ,
474+ )
454475 }
455476
456477 /// Downloads the model solution from the given url.
@@ -738,7 +759,7 @@ mod test {
738759 . submit (
739760 submission_url,
740761 Path :: new ( "tests/data/exercise" ) ,
741- Language :: from_639_3 ( "eng" ) . unwrap ( ) ,
762+ Some ( Language :: Eng ) ,
742763 )
743764 . unwrap ( ) ;
744765 assert_eq ! (
@@ -1024,4 +1045,26 @@ mod test {
10241045 SubmissionProcessingStatus :: Processing ( _) => panic ! ( "wrong status" ) ,
10251046 }
10261047 }
1048+
1049+ #[ test]
1050+ fn status_serde ( ) {
1051+ let p = StatusUpdate {
1052+ finished : false ,
1053+ message : "submitting..." ,
1054+ percent_done : 0.5 ,
1055+ } ;
1056+ assert_eq ! (
1057+ r#"{"finished":false,"message":"submitting...","percent_done":0.5}"# ,
1058+ serde_json:: to_string( & p) . unwrap( )
1059+ ) ;
1060+ let f = StatusUpdate {
1061+ finished : true ,
1062+ message : "done" ,
1063+ percent_done : 1.0 ,
1064+ } ;
1065+ assert_eq ! (
1066+ r#"{"finished":true,"message":"done","percent_done":1.0}"# ,
1067+ serde_json:: to_string( & f) . unwrap( )
1068+ ) ;
1069+ }
10271070}
0 commit comments