Skip to content

Commit 92cf0d7

Browse files
authored
Merge pull request #14 from nozwock/refactor
Refactor
2 parents 258045b + 0603cb0 commit 92cf0d7

File tree

10 files changed

+148
-208
lines changed

10 files changed

+148
-208
lines changed

src/args.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::path::PathBuf;
2+
13
use clap::{Parser, Subcommand};
24

35
#[derive(Parser)]
@@ -18,24 +20,28 @@ pub enum Commands {
1820
RunCustom {
1921
/// Testcases to run
2022
testcases: String,
23+
#[arg(short, long)]
2124
/// File to execute
22-
filename: Option<String>,
25+
file: Option<PathBuf>,
2326
},
2427
#[command(visible_alias = "-r")]
2528
Run {
29+
#[arg(short, long)]
2630
/// File to execute with default testcases
27-
filename: Option<String>,
31+
file: Option<PathBuf>,
2832
},
2933
/// Submits code to LeetCode
3034
#[command(visible_alias = "-fs")]
3135
FastSubmit {
36+
#[arg(short, long)]
3237
/// File to submit
33-
filename: Option<String>,
38+
file: Option<PathBuf>,
3439
},
3540
#[command(visible_alias = "-s")]
3641
Submit {
42+
#[arg(short, long)]
3743
/// File to submit
38-
filename: Option<String>,
44+
file: Option<PathBuf>,
3945
},
4046
/// Save a question as HTML
4147
#[command(visible_alias = "-q")]
@@ -52,8 +58,10 @@ pub enum Commands {
5258
pub enum Execute {
5359
#[command(visible_alias = "-t")]
5460
Testcases {
61+
#[arg(short, long)]
5562
/// File to run
56-
filename: Option<String>,
63+
file: Option<PathBuf>,
64+
#[arg(short, long)]
5765
/// Testcases to run
5866
testcases: Option<String>,
5967
},

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;
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)?;
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/file_parser/language.rs

Lines changed: 36 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
#[derive(Default, Clone, Copy)]
1+
use std::{fmt, str::FromStr};
2+
3+
#[derive(Debug, Default, Clone, Copy)]
24
pub enum Language {
35
#[default]
46
Rust,
@@ -20,75 +22,42 @@ pub enum Language {
2022
Elixir,
2123
Dart,
2224
}
23-
impl Language {
24-
pub fn from_str(input: &str) -> Option<Language> {
25-
match input {
26-
"rs" => Some(Language::Rust),
27-
"py" => Some(Language::Python3),
28-
"cpp" => Some(Language::Cpp),
29-
"java" => Some(Language::Java),
30-
"c" => Some(Language::C),
31-
"js" => Some(Language::Javascript),
32-
"go" => Some(Language::Go),
33-
"kt" => Some(Language::Kotlin),
34-
"swift" => Some(Language::Swift),
35-
"ts" => Some(Language::Typescript),
36-
"cs" => Some(Language::Csharp),
37-
"rb" => Some(Language::Ruby),
38-
"scala" => Some(Language::Scala),
39-
"php" => Some(Language::PHP),
40-
"rkt" => Some(Language::Racket),
41-
"erl" => Some(Language::Erlang),
42-
"ex" => Some(Language::Elixir),
43-
"dart" => Some(Language::Dart),
44-
_ => None,
45-
}
46-
}
47-
pub fn to_str(&self) -> &str {
48-
match self {
49-
Language::Rust => "rust",
50-
Language::Python3 => "python3",
51-
Language::Cpp => "cpp",
52-
Language::Java => "java",
53-
Language::C => "c",
54-
Language::Javascript => "javascript",
55-
Language::Go => "golang",
56-
Language::Kotlin => "kotlin",
57-
Language::Swift => "swift",
58-
Language::Typescript => "typescript",
59-
Language::Csharp => "csharp",
60-
Language::Ruby => "ruby",
61-
Language::Scala => "scala",
62-
Language::PHP => "php",
63-
Language::Racket => "racket",
64-
Language::Erlang => "erlang",
65-
Language::Elixir => "elixir",
66-
Language::Dart => "dart",
67-
}
25+
26+
impl fmt::Display for Language {
27+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28+
f.write_str(&format!("{:?}", self).to_lowercase())
6829
}
69-
pub(crate) fn from_slug(input: &str) -> Option<Language> {
70-
match input.to_lowercase().as_str() {
71-
"rust" => Some(Language::Rust),
72-
"python3" => Some(Language::Python3),
73-
"cpp" => Some(Language::Cpp),
74-
"java" => Some(Language::Java),
75-
"c" => Some(Language::C),
76-
"javascript" => Some(Language::Javascript),
77-
"golang" => Some(Language::Go),
78-
"kotlin" => Some(Language::Kotlin),
79-
"swift" => Some(Language::Swift),
80-
"typescript" => Some(Language::Typescript),
81-
"csharp" => Some(Language::Csharp),
82-
"ruby" => Some(Language::Ruby),
83-
"scala" => Some(Language::Scala),
84-
"php" => Some(Language::PHP),
85-
"racket" => Some(Language::Racket),
86-
"erlang" => Some(Language::Erlang),
87-
"elixir" => Some(Language::Elixir),
88-
"dart" => Some(Language::Dart),
89-
_ => None,
30+
}
31+
32+
impl FromStr for Language {
33+
type Err = eyre::ErrReport;
34+
35+
fn from_str(s: &str) -> Result<Self, Self::Err> {
36+
match s.to_lowercase().as_ref() {
37+
"rs" | "rust" => Ok(Language::Rust),
38+
"py" | "python" | "python3" => Ok(Language::Python3),
39+
"cpp" => Ok(Language::Cpp),
40+
"java" => Ok(Language::Java),
41+
"c" => Ok(Language::C),
42+
"js" | "javascript" => Ok(Language::Javascript),
43+
"go" | "golang" => Ok(Language::Go),
44+
"kt" | "kotlin" => Ok(Language::Kotlin),
45+
"swift" => Ok(Language::Swift),
46+
"ts" | "typescript" => Ok(Language::Typescript),
47+
"cs" | "csharp" => Ok(Language::Csharp),
48+
"rb" | "ruby" => Ok(Language::Ruby),
49+
"scala" => Ok(Language::Scala),
50+
"php" => Ok(Language::PHP),
51+
"rkt" | "racket" => Ok(Language::Racket),
52+
"erl" | "erlang" => Ok(Language::Erlang),
53+
"ex" | "elixer" => Ok(Language::Elixir),
54+
"dart" => Ok(Language::Dart),
55+
_ => Err(eyre::eyre!("Unknown language")),
9056
}
9157
}
58+
}
59+
60+
impl Language {
9261
pub fn extension(&self) -> &str {
9362
match self {
9463
Language::Rust => "rs",
@@ -111,12 +80,7 @@ impl Language {
11180
Language::Dart => "dart",
11281
}
11382
}
114-
pub fn to_string(&self) -> String {
115-
self.to_str().to_string()
116-
}
117-
}
11883

119-
impl Language {
12084
pub(crate) fn inline_comment_start(&self) -> &str {
12185
use Language::*;
12286
match self {

src/handlers/execution.rs

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::fmt;
2+
13
use colored::Colorize;
24
use serde::Deserialize;
35

@@ -67,8 +69,8 @@ pub struct LimitExceeded {
6769
pub state: String,
6870
}
6971

70-
impl std::fmt::Display for LimitExceeded {
71-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
72+
impl fmt::Display for LimitExceeded {
73+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
7274
let seperator = "-------------------------------";
7375
write!(
7476
f,
@@ -81,8 +83,8 @@ impl std::fmt::Display for LimitExceeded {
8183
}
8284
}
8385

84-
impl std::fmt::Display for CompileError {
85-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
86+
impl fmt::Display for CompileError {
87+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
8688
let seperator = "-------------------------------";
8789
write!(
8890
f,
@@ -95,8 +97,8 @@ impl std::fmt::Display for CompileError {
9597
}
9698
}
9799

98-
impl std::fmt::Display for RuntimeError {
99-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
100+
impl fmt::Display for RuntimeError {
101+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
100102
let seperator = "-------------------------------";
101103
write!(
102104
f,
@@ -114,8 +116,8 @@ impl std::fmt::Display for RuntimeError {
114116
}
115117
}
116118

117-
impl std::fmt::Display for Success {
118-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
119+
impl fmt::Display for Success {
120+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
119121
let seperator = "-------------------------------";
120122
let part1 = format!(
121123
"{}\n\n",
@@ -195,25 +197,4 @@ impl Success {
195197
pub fn is_correct(&self) -> bool {
196198
self.correct_answer
197199
}
198-
pub fn display(&self) {
199-
println!("{}", self);
200-
}
201-
}
202-
203-
impl LimitExceeded {
204-
pub fn display(&self) {
205-
println!("{}", self);
206-
}
207-
}
208-
209-
impl CompileError {
210-
pub fn display(&self) {
211-
println!("{}", self);
212-
}
213-
}
214-
215-
impl RuntimeError {
216-
pub fn display(&self) {
217-
println!("{}", self);
218-
}
219200
}

0 commit comments

Comments
 (0)