Skip to content

Refactor #14

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 8 commits into from
Jun 5, 2023
Merged
Show file tree
Hide file tree
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
18 changes: 13 additions & 5 deletions src/args.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::path::PathBuf;

use clap::{Parser, Subcommand};

#[derive(Parser)]
Expand All @@ -18,24 +20,28 @@ pub enum Commands {
RunCustom {
/// Testcases to run
testcases: String,
#[arg(short, long)]
/// File to execute
filename: Option<String>,
file: Option<PathBuf>,
},
#[command(visible_alias = "-r")]
Run {
#[arg(short, long)]
/// File to execute with default testcases
filename: Option<String>,
file: Option<PathBuf>,
},
/// Submits code to LeetCode
#[command(visible_alias = "-fs")]
FastSubmit {
#[arg(short, long)]
/// File to submit
filename: Option<String>,
file: Option<PathBuf>,
},
#[command(visible_alias = "-s")]
Submit {
#[arg(short, long)]
/// File to submit
filename: Option<String>,
file: Option<PathBuf>,
},
/// Save a question as HTML
#[command(visible_alias = "-q")]
Expand All @@ -52,8 +58,10 @@ pub enum Commands {
pub enum Execute {
#[command(visible_alias = "-t")]
Testcases {
#[arg(short, long)]
/// File to run
filename: Option<String>,
file: Option<PathBuf>,
#[arg(short, long)]
/// Testcases to run
testcases: Option<String>,
},
Expand Down
27 changes: 15 additions & 12 deletions src/file_parser/codefile.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use eyre::Result;

use super::language::*;
use std::path::PathBuf;
use std::{
path::{Path, PathBuf},
str::FromStr,
};

pub struct CodeFile {
pub language: Language,
Expand All @@ -22,8 +25,8 @@ impl Default for CodeFile {
}

impl CodeFile {
pub fn from_file(path: &str) -> Result<Self> {
let path = PathBuf::from(&path);
pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Self> {
let path = PathBuf::from(path.as_ref());
let (_file_name, mut code_file) =
Self::is_valid_file(&path).ok_or_else(|| eyre::eyre!("Invalid file"))?;
let code = std::fs::read_to_string(&path)?;
Expand All @@ -36,9 +39,9 @@ impl CodeFile {
Ok(code_file)
}

pub fn from_dir() -> Result<Self> {
pub fn from_dir<P: AsRef<Path>>(path: P) -> Result<Self> {
let mut code_file: Option<CodeFile> = None;
for file in std::fs::read_dir(".")?.filter_map(|f| f.ok()) {
for file in std::fs::read_dir(path.as_ref())?.filter_map(|f| f.ok()) {
let path = file.path();
if let Some((file_name, code_file_)) = Self::is_valid_file(&path) {
code_file = Some(code_file_);
Expand All @@ -60,16 +63,16 @@ impl CodeFile {
Ok(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)?;
fn is_valid_file<'a, P: AsRef<Path>>(path: &'a P) -> Option<(&'a str, Self)> {
let extension = path.as_ref().extension().and_then(|ext| ext.to_str())?;

Some((
file_name,
path.as_ref()
.file_name()
.and_then(|filename| filename.to_str())?,
CodeFile {
language,
path: path.clone(),
language: Language::from_str(extension).ok()?,
path: path.as_ref().into(),
question_title: String::new(),
code: String::new(),
},
Expand Down
108 changes: 36 additions & 72 deletions src/file_parser/language.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#[derive(Default, Clone, Copy)]
use std::{fmt, str::FromStr};

#[derive(Debug, Default, Clone, Copy)]
pub enum Language {
#[default]
Rust,
Expand All @@ -20,75 +22,42 @@ pub enum Language {
Elixir,
Dart,
}
impl Language {
pub fn from_str(input: &str) -> Option<Language> {
match input {
"rs" => Some(Language::Rust),
"py" => Some(Language::Python3),
"cpp" => Some(Language::Cpp),
"java" => Some(Language::Java),
"c" => Some(Language::C),
"js" => Some(Language::Javascript),
"go" => Some(Language::Go),
"kt" => Some(Language::Kotlin),
"swift" => Some(Language::Swift),
"ts" => Some(Language::Typescript),
"cs" => Some(Language::Csharp),
"rb" => Some(Language::Ruby),
"scala" => Some(Language::Scala),
"php" => Some(Language::PHP),
"rkt" => Some(Language::Racket),
"erl" => Some(Language::Erlang),
"ex" => Some(Language::Elixir),
"dart" => Some(Language::Dart),
_ => None,
}
}
pub fn to_str(&self) -> &str {
match self {
Language::Rust => "rust",
Language::Python3 => "python3",
Language::Cpp => "cpp",
Language::Java => "java",
Language::C => "c",
Language::Javascript => "javascript",
Language::Go => "golang",
Language::Kotlin => "kotlin",
Language::Swift => "swift",
Language::Typescript => "typescript",
Language::Csharp => "csharp",
Language::Ruby => "ruby",
Language::Scala => "scala",
Language::PHP => "php",
Language::Racket => "racket",
Language::Erlang => "erlang",
Language::Elixir => "elixir",
Language::Dart => "dart",
}

impl fmt::Display for Language {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&format!("{:?}", self).to_lowercase())
}
pub(crate) fn from_slug(input: &str) -> Option<Language> {
match input.to_lowercase().as_str() {
"rust" => Some(Language::Rust),
"python3" => Some(Language::Python3),
"cpp" => Some(Language::Cpp),
"java" => Some(Language::Java),
"c" => Some(Language::C),
"javascript" => Some(Language::Javascript),
"golang" => Some(Language::Go),
"kotlin" => Some(Language::Kotlin),
"swift" => Some(Language::Swift),
"typescript" => Some(Language::Typescript),
"csharp" => Some(Language::Csharp),
"ruby" => Some(Language::Ruby),
"scala" => Some(Language::Scala),
"php" => Some(Language::PHP),
"racket" => Some(Language::Racket),
"erlang" => Some(Language::Erlang),
"elixir" => Some(Language::Elixir),
"dart" => Some(Language::Dart),
_ => None,
}

impl FromStr for Language {
type Err = eyre::ErrReport;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_ref() {
"rs" | "rust" => Ok(Language::Rust),
"py" | "python" | "python3" => Ok(Language::Python3),
"cpp" => Ok(Language::Cpp),
"java" => Ok(Language::Java),
"c" => Ok(Language::C),
"js" | "javascript" => Ok(Language::Javascript),
"go" | "golang" => Ok(Language::Go),
"kt" | "kotlin" => Ok(Language::Kotlin),
"swift" => Ok(Language::Swift),
"ts" | "typescript" => Ok(Language::Typescript),
"cs" | "csharp" => Ok(Language::Csharp),
"rb" | "ruby" => Ok(Language::Ruby),
"scala" => Ok(Language::Scala),
"php" => Ok(Language::PHP),
"rkt" | "racket" => Ok(Language::Racket),
"erl" | "erlang" => Ok(Language::Erlang),
"ex" | "elixer" => Ok(Language::Elixir),
"dart" => Ok(Language::Dart),
_ => Err(eyre::eyre!("Unknown language")),
}
}
}

impl Language {
pub fn extension(&self) -> &str {
match self {
Language::Rust => "rs",
Expand All @@ -111,12 +80,7 @@ impl Language {
Language::Dart => "dart",
}
}
pub fn to_string(&self) -> String {
self.to_str().to_string()
}
}

impl Language {
pub(crate) fn inline_comment_start(&self) -> &str {
use Language::*;
match self {
Expand Down
39 changes: 10 additions & 29 deletions src/handlers/execution.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::fmt;

use colored::Colorize;
use serde::Deserialize;

Expand Down Expand Up @@ -67,8 +69,8 @@ pub struct LimitExceeded {
pub state: String,
}

impl std::fmt::Display for LimitExceeded {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
impl fmt::Display for LimitExceeded {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let seperator = "-------------------------------";
write!(
f,
Expand All @@ -81,8 +83,8 @@ impl std::fmt::Display for LimitExceeded {
}
}

impl std::fmt::Display for CompileError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
impl fmt::Display for CompileError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let seperator = "-------------------------------";
write!(
f,
Expand All @@ -95,8 +97,8 @@ impl std::fmt::Display for CompileError {
}
}

impl std::fmt::Display for RuntimeError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
impl fmt::Display for RuntimeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let seperator = "-------------------------------";
write!(
f,
Expand All @@ -114,8 +116,8 @@ impl std::fmt::Display for RuntimeError {
}
}

impl std::fmt::Display for Success {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
impl fmt::Display for Success {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let seperator = "-------------------------------";
let part1 = format!(
"{}\n\n",
Expand Down Expand Up @@ -195,25 +197,4 @@ impl Success {
pub fn is_correct(&self) -> bool {
self.correct_answer
}
pub fn display(&self) {
println!("{}", self);
}
}

impl LimitExceeded {
pub fn display(&self) {
println!("{}", self);
}
}

impl CompileError {
pub fn display(&self) {
println!("{}", self);
}
}

impl RuntimeError {
pub fn display(&self) {
println!("{}", self);
}
}
Loading