Skip to content

Removed dead code (again) that I (accidentally) added (again) #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 41 additions & 54 deletions src/file_parser/codefile.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::io::{Read, Write};
use std::{io::{Read, Write}, path::PathBuf};
use super::language::*;

pub struct CodeFile {
Expand All @@ -18,25 +18,10 @@ impl CodeFile {
continue
};
let path = file.path();
let Some(file_name) = path.file_name().and_then(|filename| filename.to_str()) else {
// Non-UTF-8 name
continue
};
let Some(extension) = path.extension().and_then(|ext| ext.to_str()) else {
// A hidden file (like .gitignore), or a file with no '.', or a file with weird non-UTF-8 extension.
continue
};
let Some(language) = Language::from_str(extension) else {
// Unsupported language
continue;
};
let Some(valid_file) = Self::is_valid_file(&path) else {continue};
let file_name = valid_file.0;
code_file = Some(valid_file.1);

code_file = Some(
CodeFile {
language, path: path.clone(), question_title: String::new(), code: String::new()
}
);

if file_name.starts_with("main") {
break;
}
Expand Down Expand Up @@ -65,10 +50,32 @@ impl CodeFile {
"Failed to read file {}",
code_file.path.display()
));

let (question_title, parsed_code) = Self::parse_code(&code);
code_file.question_title = question_title;
code_file.code = parsed_code;
code_file
}

fn is_valid_file<'a>(path: &'a std::path::PathBuf) -> Option<(&'a str, Self)> {
let file_name = path.file_name().and_then(|filename| filename.to_str())?;
let extension = path.extension().and_then(|ext| ext.to_str())?;
let language = Language::from_str(extension)?;

Some(
(file_name, CodeFile {
language, path: path.clone(), question_title: String::new(), code: String::new()
})
)
}

fn parse_code(code: &str) -> (String, String) {
let question_title: String;
let parsed_code: String;
let start = code
.find("#LCSTART")
.map(|idx| idx +
// This returning None the user
// This returning None means the user
// wants to submit a practically empty file,
// but hey we don't judge!
code[idx..].find('\n').unwrap_or(0))
Expand All @@ -78,51 +85,31 @@ impl CodeFile {
if let Some(problem) = code.find("leetcode.com/problems/") {
let problem = (&code[problem..]).split_whitespace().next().unwrap();
let problem = problem.split('/').skip(2).next().unwrap();
code_file.question_title = problem.to_string();
question_title = problem.to_string();
} else {
println!("No leetcode problem found in the code file. Please add the problem link in the code file using comments.");
// terminate with error
std::process::exit(1);
}
code_file.code = code[start..end].to_string();
code_file
parsed_code = code[start..end].to_string();

(question_title, parsed_code)
}

pub fn from_file(path: String) -> Self {
let extension = path.split('.').last().unwrap();
let language = Language::from_str(extension).expect("File extension not supported");
let path = PathBuf::from(path);
let (_, mut valid_file) = Self::is_valid_file(&path).expect("Improper filename or the language is not supported");
let file = std::fs::File::open(&path);
let Ok(mut file) = file else{
println!("Error while opening file {}", &path );
let Ok(mut file) = file else {
eprintln!("Error while opening file {}", path.display());
std::process::exit(1);
};
let mut code = String::new();
file.read_to_string(&mut code)
.expect(&format!("Failed to read file {}", &path));
let start = code
.find("#LCSTART")
.map(|idx| idx +
// This returning None the user
// wants to submit a practically empty file,
// but hey we don't judge!
code[idx..].find('\n').unwrap_or(0))
.unwrap_or(0);

let end = code.find("#LCEND").unwrap_or(code.len());
if let Some(problem) = code.find("leetcode.com/problems/") {
let problem = (&code[problem..]).split_whitespace().next().unwrap();
let problem = problem.split('/').into_iter().rev().skip(1).next().unwrap();
let question_title = problem.to_string();
Self {
language,
path: std::path::PathBuf::from(path),
question_title,
code: code[start..end].to_string(),
}
} else {
println!("No leetcode problem found in the code file. Please add the problem link in the code file using comments.");
// terminate with error
std::process::exit(1);
}
.expect(&format!("Failed to read file {}", path.display()));
let (question_title, parsed_code) = Self::parse_code(&code);
valid_file.question_title = question_title;
valid_file.code = parsed_code;
valid_file
}
}
}