@@ -3,7 +3,7 @@ use std::{
33 env,
44 fs:: { File , OpenOptions } ,
55 io:: { self , Read , Seek , StdoutLock , Write } ,
6- path:: Path ,
6+ path:: { Path , MAIN_SEPARATOR_STR } ,
77 process:: { Command , Stdio } ,
88 thread,
99} ;
@@ -15,6 +15,7 @@ use crate::{
1515 embedded:: EMBEDDED_FILES ,
1616 exercise:: { Exercise , RunnableExercise } ,
1717 info_file:: ExerciseInfo ,
18+ term,
1819} ;
1920
2021const STATE_FILE_NAME : & str = ".rustlings-state.txt" ;
@@ -71,6 +72,7 @@ impl AppState {
7172 format ! ( "Failed to open or create the state file {STATE_FILE_NAME}" )
7273 } ) ?;
7374
75+ let dir_canonical_path = term:: canonicalize ( "exercises" ) ;
7476 let mut exercises = exercise_infos
7577 . into_iter ( )
7678 . map ( |exercise_info| {
@@ -82,10 +84,32 @@ impl AppState {
8284 let dir = exercise_info. dir . map ( |dir| & * dir. leak ( ) ) ;
8385 let hint = exercise_info. hint . leak ( ) . trim_ascii ( ) ;
8486
87+ let canonical_path = dir_canonical_path. as_deref ( ) . map ( |dir_canonical_path| {
88+ let mut canonical_path;
89+ if let Some ( dir) = dir {
90+ canonical_path = String :: with_capacity (
91+ 2 + dir_canonical_path. len ( ) + dir. len ( ) + name. len ( ) ,
92+ ) ;
93+ canonical_path. push_str ( dir_canonical_path) ;
94+ canonical_path. push_str ( MAIN_SEPARATOR_STR ) ;
95+ canonical_path. push_str ( dir) ;
96+ } else {
97+ canonical_path =
98+ String :: with_capacity ( 1 + dir_canonical_path. len ( ) + name. len ( ) ) ;
99+ canonical_path. push_str ( dir_canonical_path) ;
100+ }
101+
102+ canonical_path. push_str ( MAIN_SEPARATOR_STR ) ;
103+ canonical_path. push_str ( name) ;
104+ canonical_path. push_str ( ".rs" ) ;
105+ canonical_path
106+ } ) ;
107+
85108 Exercise {
86109 dir,
87110 name,
88111 path,
112+ canonical_path,
89113 test : exercise_info. test ,
90114 strict_clippy : exercise_info. strict_clippy ,
91115 hint,
@@ -486,6 +510,7 @@ mod tests {
486510 dir : None ,
487511 name : "0" ,
488512 path : "exercises/0.rs" ,
513+ canonical_path : None ,
489514 test : false ,
490515 strict_clippy : false ,
491516 hint : "" ,
0 commit comments