|
1 | 1 | use std::path::Path; |
2 | 2 |
|
| 3 | +use crate::GIT_README; |
3 | 4 | use crate::file_parser::codefile::CodeFile; |
4 | 5 | use crate::handlers::leetcode::{Authorized, LeetCode}; |
5 | 6 | use crate::handlers::utils::{ExecutionResult, SubmissionResult}; |
@@ -52,3 +53,78 @@ pub(crate) fn submit(lc: &LeetCode<Authorized>, code_file: CodeFile) -> Result<( |
52 | 53 |
|
53 | 54 | Ok(()) |
54 | 55 | } |
| 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