1- use std:: io;
1+ use std:: {
2+ io,
3+ path:: {
4+ Path ,
5+ PathBuf ,
6+ } ,
7+ } ;
28
39use colored:: Colorize ;
410use leetcoderustapi:: {
@@ -39,19 +45,14 @@ impl LeetcodeApiRunner {
3945 } )
4046 }
4147
42- pub async fn get_problem_info ( & self , id : u32 ) -> io:: Result < String > {
43- let pb = self . api . set_problem_by_id ( id) . await . unwrap ( ) ;
48+ pub async fn get_problem_info ( & self , id : u32 ) -> io:: Result < ( ) > {
49+ let pb = self . api . set_problem_by_id ( id) . await ? ;
4450
45- let title = pb. description ( ) . unwrap ( ) . name . bold ( ) . cyan ( ) ;
46- let difficulty = match pb. difficulty ( ) . as_str ( ) {
47- "Easy" => "Easy" . green ( ) ,
48- "Medium" => "Medium" . yellow ( ) ,
49- "Hard" => "Hard" . red ( ) ,
50- _ => "Unknown" . normal ( ) ,
51- } ;
52- let description = html2text ( & pb. description ( ) . unwrap ( ) . content ) ;
51+ let title = pb. description ( ) ?. name . bold ( ) . cyan ( ) ;
52+ let difficulty = difficulty_color ( & pb. difficulty ( ) ) ;
53+ let description = html2text ( & pb. description ( ) ?. content ) ;
5354
54- Ok ( format ! ( "{} {}: {}\n {}" , id, difficulty, title, description) )
55+ Ok ( println ! ( "{} {}: {}\n {}" , id, difficulty, title, description) )
5556 }
5657
5758 /// Fetches the problem name by its ID.
@@ -76,54 +77,48 @@ impl LeetcodeApiRunner {
7677
7778 pub async fn start_problem (
7879 & self , id : u32 , lang : ProgrammingLanguage ,
79- ) -> io:: Result < String > {
80+ ) -> io:: Result < ( ) > {
8081 let pb = self . api . set_problem_by_id ( id) . await ?;
81- let pb_desc = pb. description ( ) . unwrap ( ) ;
82+ let pb_desc = pb. description ( ) ? ;
8283 let pb_name = pb_desc. name . replace ( " " , "_" ) ;
8384 let md_desc = html2md:: parse_html ( & pb_desc. content ) ;
84- let pb_dir = self . prepare_problem_directory ( id, & pb_name, & lang) ?;
85+ let ( pb_dir, src_dir) =
86+ self . prepare_problem_dir ( id, & pb_name, & lang) ?;
8587
8688 let mut starter_code = self . get_starter_code ( & lang, & pb) ?;
8789 starter_code = inject_default_return_value ( & starter_code, & lang) ;
8890
89- let test_data =
90- LeetcodeReadmeParser :: new ( & md_desc) . parse ( ) . map_err ( |e| {
91- io:: Error :: new ( io:: ErrorKind :: InvalidData , e. to_string ( ) )
92- } ) ?;
93- let tests = TestGenerator :: new ( & starter_code, test_data)
94- . run ( & lang)
95- . map_err ( |e| io:: Error :: new ( io:: ErrorKind :: Other , e. to_string ( ) ) ) ?;
91+ let test_data = LeetcodeReadmeParser :: new ( & md_desc) . parse ( ) ?;
92+ let tests = TestGenerator :: new ( & starter_code, test_data) . run ( & lang) ?;
9693
9794 let mut file_content = format ! ( "{}\n \n {}" , starter_code, tests) ;
9895 file_content = prefix_code ( & file_content, & lang) ;
9996 file_content = postfix_code ( & file_content, & lang) ;
10097 write_readme ( & pb_dir, id, & pb_name, & md_desc) ?;
101- let src_dir = pb_dir. join ( "src" ) ;
102- ensure_directory_exists ( & src_dir) ?;
10398 write_to_file ( & src_dir, & get_file_name ( & lang) , & file_content) ?;
10499
105- Ok ( format ! (
106- "Problem {}: {} has been created at {} with the {} programming \
107- language.",
100+ Ok ( println ! (
101+ "{}: {} created at {} in {}." ,
108102 id,
109- pb_name,
103+ pb_name. green ( ) . bold ( ) ,
110104 pb_dir. display( ) ,
111105 language_to_string( & lang)
112106 ) )
113107 }
114108
115109 /// Prepares the problem directory.
116- fn prepare_problem_directory (
110+ fn prepare_problem_dir (
117111 & self , id : u32 , pb_name : & str , language : & ProgrammingLanguage ,
118- ) -> io:: Result < std :: path :: PathBuf > {
112+ ) -> io:: Result < ( PathBuf , PathBuf ) > {
119113 let leetcode_dir = self . rcs . resolve_leetcode_dir ( ) ?;
120114 let problem_dir = leetcode_dir. join ( format ! ( "{}_{}" , id, pb_name) ) ;
115+ let src_dir = problem_dir. join ( "src" ) ;
116+
121117 ensure_directory_exists ( & problem_dir) ?;
118+ ensure_directory_exists ( & src_dir) ?;
122119
123- // Initialize language-specific project structure
124120 self . initialize_language_project ( & problem_dir, pb_name, language) ?;
125-
126- Ok ( problem_dir)
121+ Ok ( ( problem_dir, src_dir) )
127122 }
128123
129124 /// Generates starter code for the specified programming language.
@@ -154,41 +149,32 @@ impl LeetcodeApiRunner {
154149 }
155150
156151 pub async fn test_response (
157- & self , id : u32 , path_to_file : String ,
158- ) -> io:: Result < String > {
159- let problem_info = self . api . set_problem_by_id ( id) . await . unwrap ( ) ;
152+ & self , id : u32 , path_to_file : & String ,
153+ ) -> io:: Result < ( ) > {
154+ let problem_info = self . api . set_problem_by_id ( id) . await ? ;
160155 let file_content = std:: fs:: read_to_string ( & path_to_file)
161156 . expect ( "Unable to read the file" ) ;
162157 let language = get_language_from_extension ( & path_to_file) ;
163158
164- let test_response = problem_info
165- . send_test ( language, & file_content)
166- . await
167- . unwrap ( ) ;
168- Ok ( format ! ( "Test response for problem {}: {:#?}" , id, test_response) )
159+ let test_res = problem_info. send_test ( language, & file_content) . await ?;
160+ Ok ( println ! ( "Test response for problem {}: {:?}" , id, test_res) )
169161 }
170162
171163 pub async fn submit_response (
172- & self , id : u32 , path_to_file : String ,
173- ) -> io:: Result < String > {
174- let problem_info = self . api . set_problem_by_id ( id) . await . unwrap ( ) ;
164+ & self , id : u32 , path_to_file : & String ,
165+ ) -> io:: Result < ( ) > {
166+ let pb = self . api . set_problem_by_id ( id) . await ? ;
175167 let file_content = std:: fs:: read_to_string ( & path_to_file)
176168 . expect ( "Unable to read the file" ) ;
177169 let language = get_language_from_extension ( & path_to_file) ;
178170
179- let test_response = problem_info
180- . send_subm ( language, & file_content)
181- . await
182- . unwrap ( ) ;
183- Ok ( format ! (
184- "Here's your submit response for problem {}: {:#?}" ,
185- id, test_response
186- ) )
171+ let sub_res = pb. send_subm ( language, & file_content) . await ?;
172+ Ok ( println ! ( "{}: submit result {:?}" , id, sub_res) )
187173 }
188174
189175 /// Initializes language-specific project structure.
190176 fn initialize_language_project (
191- & self , problem_dir : & std :: path :: Path , pb_name : & str ,
177+ & self , problem_dir : & Path , pb_name : & str ,
192178 language : & ProgrammingLanguage ,
193179 ) -> io:: Result < ( ) > {
194180 use std:: process:: Command ;
0 commit comments