Skip to content

Commit f95f2c4

Browse files
committed
Use Path where relevent
1 parent 0baadf5 commit f95f2c4

File tree

4 files changed

+29
-24
lines changed

4 files changed

+29
-24
lines changed

src/file_parser/codefile.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
use eyre::Result;
22

33
use super::language::*;
4-
use std::{path::PathBuf, str::FromStr};
4+
use std::{
5+
path::{Path, PathBuf},
6+
str::FromStr,
7+
};
58

69
pub struct CodeFile {
710
pub language: Language,
@@ -22,8 +25,8 @@ impl Default for CodeFile {
2225
}
2326

2427
impl CodeFile {
25-
pub fn from_file(path: &str) -> Result<Self> {
26-
let path = PathBuf::from(&path);
28+
pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Self> {
29+
let path = PathBuf::from(path.as_ref());
2730
let (_file_name, mut code_file) =
2831
Self::is_valid_file(&path).ok_or_else(|| eyre::eyre!("Invalid file"))?;
2932
let code = std::fs::read_to_string(&path)?;
@@ -36,9 +39,9 @@ impl CodeFile {
3639
Ok(code_file)
3740
}
3841

39-
pub fn from_dir() -> Result<Self> {
42+
pub fn from_dir<P: AsRef<Path>>(path: P) -> Result<Self> {
4043
let mut code_file: Option<CodeFile> = None;
41-
for file in std::fs::read_dir(".")?.filter_map(|f| f.ok()) {
44+
for file in std::fs::read_dir(path.as_ref())?.filter_map(|f| f.ok()) {
4245
let path = file.path();
4346
if let Some((file_name, code_file_)) = Self::is_valid_file(&path) {
4447
code_file = Some(code_file_);
@@ -60,16 +63,16 @@ impl CodeFile {
6063
Ok(code_file)
6164
}
6265

63-
fn is_valid_file<'a>(path: &'a std::path::PathBuf) -> Option<(&'a str, Self)> {
64-
let file_name = path.file_name().and_then(|filename| filename.to_str())?;
65-
let extension = path.extension().and_then(|ext| ext.to_str())?;
66-
let language = Language::from_str(extension).ok()?;
66+
fn is_valid_file<'a, P: AsRef<Path>>(path: &'a P) -> Option<(&'a str, Self)> {
67+
let extension = path.as_ref().extension().and_then(|ext| ext.to_str())?;
6768

6869
Some((
69-
file_name,
70+
path.as_ref()
71+
.file_name()
72+
.and_then(|filename| filename.to_str())?,
7073
CodeFile {
71-
language,
72-
path: path.clone(),
74+
language: Language::from_str(extension).ok()?,
75+
path: path.as_ref().into(),
7376
question_title: String::new(),
7477
code: String::new(),
7578
},

src/handlers/helpers.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::str::FromStr;
1+
use std::{path::Path, str::FromStr};
22

33
use colored::Colorize;
44
use eyre::Result;
@@ -75,9 +75,9 @@ pub(crate) struct BoilerPlateCode {
7575

7676
use super::super::file_parser::language::Language;
7777
impl BoilerPlateCode {
78-
pub(crate) fn save_code(&self, filename: &str, title_slug: &str) -> Result<()> {
78+
pub(crate) fn save_code<P: AsRef<Path>>(&self, file_path: P, title_slug: &str) -> Result<()> {
7979
let language = Language::from_str(&self.langSlug)?;
80-
let mut file = std::fs::File::create(filename)?;
80+
let mut file = std::fs::File::create(file_path)?;
8181
let comment = format!(
8282
" {} #LCEND https://leetcode.com/problems/{}/",
8383
language.inline_comment_start(),

src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ fn main() -> Result<()> {
6767
testcases,
6868
filename,
6969
}) => {
70-
_ = execute_testcases(filename, Some(testcases), &lc)?;
70+
_ = execute_testcases(filename, Some(&testcases), &lc)?;
7171
// bail if `is_correct == false`?
7272
}
7373
Some(Commands::Run { filename }) => {
@@ -77,7 +77,7 @@ fn main() -> Result<()> {
7777
let code_file = if let Some(path) = filename {
7878
CodeFile::from_file(&path)?
7979
} else {
80-
CodeFile::from_dir()?
80+
CodeFile::from_dir(".")?
8181
};
8282

8383
submit(&lc, code_file)?;

src/utils.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
1+
use std::path::Path;
2+
13
use crate::file_parser::codefile::CodeFile;
24
use crate::handlers::leetcode::{Authorized, LeetCode};
35
use crate::handlers::utils::{ExecutionResult, SubmissionResult};
46

57
use eyre::{bail, Result};
68

79
/// The first element of the return tuple indicates whether the answer is correct.
8-
pub(crate) fn execute_testcases(
9-
filename: Option<String>,
10-
testcases: Option<String>,
10+
pub(crate) fn execute_testcases<P: AsRef<Path>>(
11+
file_path: Option<P>,
12+
testcases: Option<&str>,
1113
lc: &LeetCode<Authorized>,
1214
) -> Result<(bool, CodeFile)> {
13-
let code_file = if let Some(path) = filename {
14-
CodeFile::from_file(&path)?
15+
let code_file = if let Some(path) = file_path {
16+
CodeFile::from_file(path)?
1517
} else {
16-
CodeFile::from_dir()?
18+
CodeFile::from_dir(".")?
1719
};
1820

1921
match testcases {
2022
Some(testcases) => {
21-
let data_input = std::fs::read_to_string(&testcases)?;
23+
let data_input = std::fs::read_to_string(testcases)?;
2224

2325
match lc.execute(&code_file, data_input)? {
2426
ExecutionResult::Success(result) => {

0 commit comments

Comments
 (0)