11// Download the exercises from https://rustplatform.com/
22// and make them available locally.
33
4- // use reqwest::Error; // DEBUG
54use serde:: Deserialize ;
65use std:: env;
76use std:: fs;
@@ -21,25 +20,26 @@ struct Bite {
2120 author : String ,
2221}
2322
24- fn write_root_toml ( path : & Path ) -> std:: io:: Result < ( ) > {
23+ fn write_root_toml ( path : & Path , all_slugs : String ) -> std:: io:: Result < ( ) > {
2524 // main Cargo.toml template
26- let content = b "[package]
25+ let content = "[package]
2726name = \" exercises\"
2827version = \" 0.1.0\"
2928edition = \" 2024\" \
3029\n
3130[workspace]
3231resolver = \" 3\"
33- members = [\" adder\" ]" ;
32+ members = [\n workspace_members]"
33+ . replace ( "workspace_members" , & all_slugs) ;
3434
3535 let filename = path. join ( "Cargo.toml" ) ;
3636 let mut file = File :: create ( filename) ?;
37- file. write_all ( content) ?;
37+ file. write_all ( content. as_bytes ( ) ) ?;
3838
3939 Ok ( ( ) )
4040}
4141
42- fn write_toml ( path : & Path , slug : & String , libraries : String ) -> std:: io:: Result < ( ) > {
42+ fn write_toml ( path : & Path , slug : & String , libraries : & String ) -> std:: io:: Result < ( ) > {
4343 // exercise Cargo.toml template
4444 let content = "[package]
4545name = \" package_name\"
@@ -56,7 +56,7 @@ edition = \"2024\"\n
5656 Ok ( ( ) )
5757}
5858
59- fn write_exercise ( path : & Path , template : String ) -> std:: io:: Result < ( ) > {
59+ fn write_exercise ( path : & Path , template : & String ) -> std:: io:: Result < ( ) > {
6060 let src_dir = path. join ( "src" ) ;
6161 fs:: create_dir_all ( & src_dir) ?;
6262 let filename = src_dir. join ( "lib.rs" ) ;
@@ -79,17 +79,17 @@ fn write_exercise(path: &Path, template: String) -> std::io::Result<()> {
7979
8080fn write_markdown (
8181 path : & Path ,
82- name : String ,
83- description : String ,
84- level : String ,
85- author : String ,
82+ name : & String ,
83+ description : & String ,
84+ level : & String ,
85+ author : & String ,
8686) -> std:: io:: Result < ( ) > {
8787 // exercise markdown template
8888 let content = "# package_name
8989
90- Level: package_level
91- Author: package_author\n
92- Instructions:
90+ - Level: package_level
91+ - Author: package_author\n
92+ ## Instructions
9393package_description\n "
9494 . replace ( "package_name" , name. as_str ( ) )
9595 . replace ( "package_description" , description. as_str ( ) )
@@ -127,34 +127,38 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
127127
128128 // extract the exercises from the response
129129 let bites: Vec < Bite > = response. json ( ) ?;
130- println ! ( "I found {:#?} exercises!" , bites. len( ) ) ;
130+ println ! ( "{:#?} exercises found !" , bites. len( ) ) ;
131131 println ! ( ) ;
132132
133133 // make sure the base path (exercises) exists
134134 fs:: create_dir_all ( & base_path) ?;
135135
136- for bite in bites {
136+ for bite in & bites {
137137 print ! ( "{:#?}" , bite. name) ;
138- let slug = bite. slug ;
138+ let slug = & bite. slug ;
139139 let exercise_path = & base_path. join ( & bite. level ) . join ( & slug) ;
140140
141141 // make sure the exercise directory exists
142142 fs:: create_dir_all ( & exercise_path) ?;
143143 // re-write/update the toml and md files but make a backup copy of the
144144 // exercise file if it exists, in case it was already solved
145- write_toml ( & exercise_path, & slug, bite. libraries ) ?;
145+ write_toml ( & exercise_path, & slug, & bite. libraries ) ?;
146146 write_markdown (
147147 & exercise_path,
148- bite. name ,
149- bite. description ,
150- bite. level ,
151- bite. author ,
148+ & bite. name ,
149+ & bite. description ,
150+ & bite. level ,
151+ & bite. author ,
152152 ) ?;
153- write_exercise ( & exercise_path, bite. template ) ?;
153+ write_exercise ( & exercise_path, & bite. template ) ?;
154154 println ! ( " ✅" ) ;
155155 }
156156
157- write_root_toml ( & base_path) ?;
157+ let all_slugs = bites
158+ . iter ( )
159+ . map ( |bite| String :: from ( " \" " ) + bite. slug . clone ( ) . as_str ( ) + "\" ,\n " )
160+ . collect :: < String > ( ) ;
161+ write_root_toml ( & base_path, all_slugs) ?;
158162
159163 Ok ( ( ) )
160164}
0 commit comments