Skip to content

Commit 81a4c3f

Browse files
committed
improved pack feature
1 parent 898328f commit 81a4c3f

File tree

3 files changed

+79
-42
lines changed

3 files changed

+79
-42
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "leetcode-runner-cli"
3-
version = "0.2.2"
3+
version = "0.2.5"
44
edition = "2021"
55

66
[dependencies]

src/main.rs

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::args::Cli;
22
use crate::file_parser::codefile::CodeFile;
3-
use crate::utils::{execute_testcases, submit};
3+
use crate::utils::{execute_testcases, pack, submit};
44

55
use args::Commands;
66
use clap::Parser;
@@ -90,47 +90,8 @@ fn main() -> Result<()> {
9090
bail!("Aborting submission due to failed testcase(s)".red().bold());
9191
}
9292
}
93-
Some(Commands::Pack { file }) => {
94-
let code_file = if let Some(path) = file {
95-
CodeFile::from_file(&path)?
96-
} else {
97-
CodeFile::from_dir(".")?
98-
};
99-
let question = lc.question_content(&code_file.question_title)?;
93+
Some(Commands::Pack { file }) => pack(&lc, file)?,
10094

101-
// create a directory if it doesn't exists with name of question
102-
// create a README.md file with the question description
103-
// create a file with the code
104-
std::fs::create_dir_all(&code_file.question_title.replace(' ', ""))?;
105-
106-
std::fs::write(
107-
format!(
108-
"{}/main.{}",
109-
&code_file.question_title,
110-
code_file.language.extension()
111-
),
112-
code_file.code,
113-
)?;
114-
115-
// dont create readme if it exists
116-
if let Ok(mut readme_file) = std::fs::OpenOptions::new()
117-
.create_new(true)
118-
.write(true)
119-
.open(format!("{}/{}", &code_file.question_title, GIT_README))
120-
{
121-
println!(
122-
"You can write your notes about question in {}/README.md",
123-
&code_file.question_title
124-
);
125-
std::io::Write::write_all(
126-
&mut readme_file,
127-
format!("# {}\n", code_file.question_title).as_bytes(),
128-
)?;
129-
std::io::Write::write_all(&mut readme_file, question.content.as_bytes())?;
130-
} else {
131-
println!("{} already exists, skipping creation.", GIT_README);
132-
};
133-
}
13495
None => {}
13596
};
13697

src/utils.rs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::path::Path;
22

3+
use crate::GIT_README;
34
use crate::file_parser::codefile::CodeFile;
45
use crate::handlers::leetcode::{Authorized, LeetCode};
56
use crate::handlers::utils::{ExecutionResult, SubmissionResult};
@@ -52,3 +53,78 @@ pub(crate) fn submit(lc: &LeetCode<Authorized>, code_file: CodeFile) -> Result<(
5253

5354
Ok(())
5455
}
56+
57+
pub(crate) fn pack(lc: &LeetCode<Authorized>, file: Option<std::path::PathBuf>) -> Result<()> {
58+
let code_file = if let Some(path) = file {
59+
CodeFile::from_file(&path)?
60+
} else {
61+
CodeFile::from_dir(".")?
62+
};
63+
let question = lc.question_content(&code_file.question_title)?;
64+
65+
// create a directory if it doesn't exists with name of question
66+
// create a README.md file with the question description
67+
// create a file with the code
68+
std::fs::create_dir_all(&code_file.question_title.replace(' ', ""))?;
69+
70+
std::fs::write(
71+
format!(
72+
"{}/main.{}",
73+
&code_file.question_title,
74+
code_file.language.extension()
75+
),
76+
code_file.code,
77+
)?;
78+
79+
// dont create readme if it exists
80+
if let Ok(mut readme_file) = std::fs::OpenOptions::new()
81+
.create_new(true)
82+
.write(true)
83+
.open(format!("{}/{}", &code_file.question_title, GIT_README))
84+
{
85+
println!(
86+
"You can write your notes about question in {}/README.md",
87+
&code_file.question_title
88+
);
89+
std::io::Write::write_all(
90+
&mut readme_file,
91+
format!("# {}\n", code_file.question_title).as_bytes(),
92+
)?;
93+
std::io::Write::write_all(&mut readme_file, question.content.as_bytes())?;
94+
} else {
95+
println!("{} already exists, skipping creation.", GIT_README);
96+
};
97+
98+
let mut root_readme_file = std::fs::OpenOptions::new()
99+
.create(true)
100+
.read(true)
101+
.write(true)
102+
.open(GIT_README)?;
103+
if root_readme_file.metadata()?.len() == 0 {
104+
std::io::Write::write_all(&mut root_readme_file, "# LeetCode Solutions\n\n".as_bytes())?;
105+
std::io::Write::write_all(
106+
&mut root_readme_file,
107+
format!(
108+
"- [{title}]({title}/main.{ex})\n",
109+
ex = code_file.language.extension(),
110+
title = code_file.question_title,
111+
)
112+
.as_bytes(),
113+
)?;
114+
} else {
115+
let mut contents = String::new();
116+
std::io::Read::read_to_string(&mut root_readme_file, &mut contents)?;
117+
if !contents.contains(&code_file.question_title) {
118+
std::io::Write::write_all(
119+
&mut root_readme_file,
120+
format!(
121+
"- [{title}]({title}/main.{ex})\n",
122+
ex = code_file.language.extension(),
123+
title = code_file.question_title,
124+
)
125+
.as_bytes(),
126+
)?;
127+
}
128+
}
129+
Ok(())
130+
}

0 commit comments

Comments
 (0)