Skip to content

Commit 87e5ffb

Browse files
authored
Merge pull request #3 from VivekYadav7272/main
Removed dead code (again) that I (accidentally) added (again)
2 parents 540e344 + f5d1b2d commit 87e5ffb

File tree

1 file changed

+41
-54
lines changed

1 file changed

+41
-54
lines changed

src/file_parser/codefile.rs

Lines changed: 41 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::io::{Read, Write};
1+
use std::{io::{Read, Write}, path::PathBuf};
22
use super::language::*;
33

44
pub struct CodeFile {
@@ -18,25 +18,10 @@ impl CodeFile {
1818
continue
1919
};
2020
let path = file.path();
21-
let Some(file_name) = path.file_name().and_then(|filename| filename.to_str()) else {
22-
// Non-UTF-8 name
23-
continue
24-
};
25-
let Some(extension) = path.extension().and_then(|ext| ext.to_str()) else {
26-
// A hidden file (like .gitignore), or a file with no '.', or a file with weird non-UTF-8 extension.
27-
continue
28-
};
29-
let Some(language) = Language::from_str(extension) else {
30-
// Unsupported language
31-
continue;
32-
};
21+
let Some(valid_file) = Self::is_valid_file(&path) else {continue};
22+
let file_name = valid_file.0;
23+
code_file = Some(valid_file.1);
3324

34-
code_file = Some(
35-
CodeFile {
36-
language, path: path.clone(), question_title: String::new(), code: String::new()
37-
}
38-
);
39-
4025
if file_name.starts_with("main") {
4126
break;
4227
}
@@ -65,10 +50,32 @@ impl CodeFile {
6550
"Failed to read file {}",
6651
code_file.path.display()
6752
));
53+
54+
let (question_title, parsed_code) = Self::parse_code(&code);
55+
code_file.question_title = question_title;
56+
code_file.code = parsed_code;
57+
code_file
58+
}
59+
60+
fn is_valid_file<'a>(path: &'a std::path::PathBuf) -> Option<(&'a str, Self)> {
61+
let file_name = path.file_name().and_then(|filename| filename.to_str())?;
62+
let extension = path.extension().and_then(|ext| ext.to_str())?;
63+
let language = Language::from_str(extension)?;
64+
65+
Some(
66+
(file_name, CodeFile {
67+
language, path: path.clone(), question_title: String::new(), code: String::new()
68+
})
69+
)
70+
}
71+
72+
fn parse_code(code: &str) -> (String, String) {
73+
let question_title: String;
74+
let parsed_code: String;
6875
let start = code
6976
.find("#LCSTART")
7077
.map(|idx| idx +
71-
// This returning None the user
78+
// This returning None means the user
7279
// wants to submit a practically empty file,
7380
// but hey we don't judge!
7481
code[idx..].find('\n').unwrap_or(0))
@@ -78,51 +85,31 @@ impl CodeFile {
7885
if let Some(problem) = code.find("leetcode.com/problems/") {
7986
let problem = (&code[problem..]).split_whitespace().next().unwrap();
8087
let problem = problem.split('/').skip(2).next().unwrap();
81-
code_file.question_title = problem.to_string();
88+
question_title = problem.to_string();
8289
} else {
8390
println!("No leetcode problem found in the code file. Please add the problem link in the code file using comments.");
8491
// terminate with error
8592
std::process::exit(1);
8693
}
87-
code_file.code = code[start..end].to_string();
88-
code_file
94+
parsed_code = code[start..end].to_string();
95+
96+
(question_title, parsed_code)
8997
}
9098

9199
pub fn from_file(path: String) -> Self {
92-
let extension = path.split('.').last().unwrap();
93-
let language = Language::from_str(extension).expect("File extension not supported");
100+
let path = PathBuf::from(path);
101+
let (_, mut valid_file) = Self::is_valid_file(&path).expect("Improper filename or the language is not supported");
94102
let file = std::fs::File::open(&path);
95-
let Ok(mut file) = file else{
96-
println!("Error while opening file {}", &path );
103+
let Ok(mut file) = file else {
104+
eprintln!("Error while opening file {}", path.display());
97105
std::process::exit(1);
98106
};
99107
let mut code = String::new();
100108
file.read_to_string(&mut code)
101-
.expect(&format!("Failed to read file {}", &path));
102-
let start = code
103-
.find("#LCSTART")
104-
.map(|idx| idx +
105-
// This returning None the user
106-
// wants to submit a practically empty file,
107-
// but hey we don't judge!
108-
code[idx..].find('\n').unwrap_or(0))
109-
.unwrap_or(0);
110-
111-
let end = code.find("#LCEND").unwrap_or(code.len());
112-
if let Some(problem) = code.find("leetcode.com/problems/") {
113-
let problem = (&code[problem..]).split_whitespace().next().unwrap();
114-
let problem = problem.split('/').into_iter().rev().skip(1).next().unwrap();
115-
let question_title = problem.to_string();
116-
Self {
117-
language,
118-
path: std::path::PathBuf::from(path),
119-
question_title,
120-
code: code[start..end].to_string(),
121-
}
122-
} else {
123-
println!("No leetcode problem found in the code file. Please add the problem link in the code file using comments.");
124-
// terminate with error
125-
std::process::exit(1);
126-
}
109+
.expect(&format!("Failed to read file {}", path.display()));
110+
let (question_title, parsed_code) = Self::parse_code(&code);
111+
valid_file.question_title = question_title;
112+
valid_file.code = parsed_code;
113+
valid_file
127114
}
128-
}
115+
}

0 commit comments

Comments
 (0)