@@ -135,6 +135,30 @@ pub fn get_language_from_extension(
135135 }
136136}
137137
138+ pub fn get_extension_from_language (
139+ lang : & leetcoderustapi:: ProgrammingLanguage ,
140+ ) -> String {
141+ match lang {
142+ leetcoderustapi:: ProgrammingLanguage :: CPP => "cpp" . to_string ( ) ,
143+ leetcoderustapi:: ProgrammingLanguage :: Java => "java" . to_string ( ) ,
144+ leetcoderustapi:: ProgrammingLanguage :: Python => "py" . to_string ( ) ,
145+ leetcoderustapi:: ProgrammingLanguage :: Python3 => "py" . to_string ( ) ,
146+ leetcoderustapi:: ProgrammingLanguage :: C => "c" . to_string ( ) ,
147+ leetcoderustapi:: ProgrammingLanguage :: CSharp => "cs" . to_string ( ) ,
148+ leetcoderustapi:: ProgrammingLanguage :: JavaScript => "js" . to_string ( ) ,
149+ leetcoderustapi:: ProgrammingLanguage :: TypeScript => "ts" . to_string ( ) ,
150+ leetcoderustapi:: ProgrammingLanguage :: Ruby => "rb" . to_string ( ) ,
151+ leetcoderustapi:: ProgrammingLanguage :: Swift => "swift" . to_string ( ) ,
152+ leetcoderustapi:: ProgrammingLanguage :: Go => "go" . to_string ( ) ,
153+ leetcoderustapi:: ProgrammingLanguage :: Bash => "sh" . to_string ( ) ,
154+ leetcoderustapi:: ProgrammingLanguage :: Scala => "scala" . to_string ( ) ,
155+ leetcoderustapi:: ProgrammingLanguage :: Kotlin => "kt" . to_string ( ) ,
156+ leetcoderustapi:: ProgrammingLanguage :: Rust => "rs" . to_string ( ) ,
157+ leetcoderustapi:: ProgrammingLanguage :: PHP => "php" . to_string ( ) ,
158+ _ => panic ! ( "Unsupported language: {lang:?}" ) ,
159+ }
160+ }
161+
138162pub fn spin_the_spinner ( message : & str ) -> spinners:: Spinner {
139163 spinners:: Spinner :: new ( spinners:: Spinners :: Dots12 , message. to_string ( ) )
140164}
@@ -174,30 +198,6 @@ pub fn prompt_for_language(
174198 }
175199}
176200
177- pub fn get_extension_from_language (
178- lang : & leetcoderustapi:: ProgrammingLanguage ,
179- ) -> String {
180- match lang {
181- leetcoderustapi:: ProgrammingLanguage :: CPP => "cpp" . to_string ( ) ,
182- leetcoderustapi:: ProgrammingLanguage :: Java => "java" . to_string ( ) ,
183- leetcoderustapi:: ProgrammingLanguage :: Python => "py" . to_string ( ) ,
184- leetcoderustapi:: ProgrammingLanguage :: Python3 => "py" . to_string ( ) ,
185- leetcoderustapi:: ProgrammingLanguage :: C => "c" . to_string ( ) ,
186- leetcoderustapi:: ProgrammingLanguage :: CSharp => "cs" . to_string ( ) ,
187- leetcoderustapi:: ProgrammingLanguage :: JavaScript => "js" . to_string ( ) ,
188- leetcoderustapi:: ProgrammingLanguage :: TypeScript => "ts" . to_string ( ) ,
189- leetcoderustapi:: ProgrammingLanguage :: Ruby => "rb" . to_string ( ) ,
190- leetcoderustapi:: ProgrammingLanguage :: Swift => "swift" . to_string ( ) ,
191- leetcoderustapi:: ProgrammingLanguage :: Go => "go" . to_string ( ) ,
192- leetcoderustapi:: ProgrammingLanguage :: Bash => "sh" . to_string ( ) ,
193- leetcoderustapi:: ProgrammingLanguage :: Scala => "scala" . to_string ( ) ,
194- leetcoderustapi:: ProgrammingLanguage :: Kotlin => "kt" . to_string ( ) ,
195- leetcoderustapi:: ProgrammingLanguage :: Rust => "rs" . to_string ( ) ,
196- leetcoderustapi:: ProgrammingLanguage :: PHP => "php" . to_string ( ) ,
197- _ => panic ! ( "Unsupported language: {lang:?}" ) ,
198- }
199- }
200-
201201pub fn prefix_code ( file_content : & str , lang : & ProgrammingLanguage ) -> String {
202202 let prefix = match lang {
203203 ProgrammingLanguage :: Rust => "pub struct Solution;\n \n " . to_string ( ) ,
@@ -241,3 +241,100 @@ pub fn difficulty_color(difficulty: &str) -> colored::ColoredString {
241241 _ => "Unknown" . normal ( ) ,
242242 }
243243}
244+
245+ /// Preprocesses file content before sending to LeetCode by removing local
246+ /// compilation helpers
247+ pub fn preprocess_code (
248+ content : & str , language : & ProgrammingLanguage ,
249+ ) -> String {
250+ match language {
251+ ProgrammingLanguage :: Rust => preprocess_rust_content ( content) ,
252+ // For other languages, return as-is for now
253+ _ => content. to_string ( ) ,
254+ }
255+ }
256+
257+ /// Removes pub struct Solution; from the top of the file
258+ fn preprocess_rust_content ( content : & str ) -> String {
259+ let n = delete_line_content ( content, "pub struct Solution;" ) ;
260+ remove_main ( & n)
261+ }
262+ fn remove_main ( content : & str ) -> String {
263+ let mut c = vec ! [ ] ;
264+
265+ for line in content. lines ( ) {
266+ if line. contains ( "fn main() {" ) {
267+ break ;
268+ }
269+ c. push ( line) ;
270+ }
271+ c. join ( "\n " )
272+ }
273+
274+ fn delete_line_content ( content : & str , target : & str ) -> String {
275+ content
276+ . lines ( )
277+ . filter ( |line| line. trim ( ) != target)
278+ . collect :: < Vec < _ > > ( )
279+ . join ( "\n " )
280+ }
281+
282+ /// Find the nearest Cargo project root (directory containing Cargo.toml)
283+ /// starting from `start_dir` and walking up.
284+ fn find_manifest_dir ( start_dir : & Path ) -> Option < PathBuf > {
285+ for dir in start_dir. ancestors ( ) {
286+ let candidate = dir. join ( "Cargo.toml" ) ;
287+ if candidate. is_file ( ) {
288+ return Some ( dir. to_path_buf ( ) ) ;
289+ }
290+ }
291+ None
292+ }
293+
294+ /// Runs local compilation check before sending to LeetCode
295+ pub async fn run_local_check (
296+ path_to_file : & str , language : & ProgrammingLanguage ,
297+ ) -> io:: Result < String > {
298+ use std:: process:: Command ;
299+
300+ match language {
301+ ProgrammingLanguage :: Rust => {
302+ let file_path = Path :: new ( path_to_file) ;
303+
304+ // If within a Cargo project, run `cargo check` at the project root
305+ if let Some ( parent) = file_path. parent ( ) {
306+ if let Some ( manifest_dir) = find_manifest_dir ( parent) {
307+ let output = Command :: new ( "cargo" )
308+ . args ( [ "check" , "--quiet" ] )
309+ . current_dir ( & manifest_dir)
310+ . output ( ) ?;
311+
312+ if !output. status . success ( ) {
313+ let stderr = String :: from_utf8_lossy ( & output. stderr ) ;
314+ return Ok ( format ! ( "❌ Local check failed:\n {stderr}" ) ) ;
315+ }
316+
317+ return Ok ( "✅ Local compilation passed!" . to_string ( ) ) ;
318+ }
319+ }
320+
321+ // Fallback: compile the single file directly with rustc
322+ let output = Command :: new ( "rustc" )
323+ . args ( [
324+ "--edition=2021" ,
325+ "--emit=metadata" ,
326+ "--crate-type=bin" ,
327+ path_to_file,
328+ ] )
329+ . output ( ) ?;
330+
331+ if !output. status . success ( ) {
332+ let stderr = String :: from_utf8_lossy ( & output. stderr ) ;
333+ return Ok ( format ! ( "❌ Compilation failed:\n {stderr}" ) ) ;
334+ }
335+
336+ Ok ( "✅ Local compilation passed!" . to_string ( ) )
337+ } ,
338+ _ => Ok ( format ! ( "⚠️ Local check not implemented for {language:?}" , ) ) ,
339+ }
340+ }
0 commit comments