|
1 | 1 | use colored::Colorize; |
| 2 | +use eyre::Result; |
2 | 3 | use serde::{Deserialize, Serialize}; |
3 | 4 |
|
4 | 5 | #[derive(Debug, Serialize)] |
@@ -72,39 +73,29 @@ pub(crate) struct BoilerPlateCode { |
72 | 73 |
|
73 | 74 | use super::super::file_parser::language::Language; |
74 | 75 | impl BoilerPlateCode { |
75 | | - pub(crate) fn save_code(&self, filename: &str, title_slug: &str) { |
76 | | - let language = Language::from_slug(&self.langSlug).unwrap_or_else(|| { |
77 | | - eprintln!("Error: Unable to identify language of code file!"); |
78 | | - std::process::exit(1); |
79 | | - }); |
80 | | - let Ok(mut file) = std::fs::File::create(filename) else{ |
81 | | - eprintln!("Error: Unable to create file"); |
82 | | - std::process::exit(1); |
83 | | - }; |
| 76 | + pub(crate) fn save_code(&self, filename: &str, title_slug: &str) -> Result<()> { |
| 77 | + let language = Language::from_slug(&self.langSlug) |
| 78 | + .ok_or_else(|| eyre::eyre!("Unable to identify language of code file!"))?; |
| 79 | + let mut file = std::fs::File::create(filename)?; |
84 | 80 | let comment = format!( |
85 | 81 | " {} #LCEND https://leetcode.com/problems/{}/", |
86 | 82 | language.inline_comment_start(), |
87 | 83 | title_slug.to_lowercase().trim().replace(" ", "-") |
88 | 84 | ); |
| 85 | + |
89 | 86 | // write code into file along with the comment |
90 | | - if let Err(_) = std::io::Write::write_all(&mut file, self.code.as_bytes()) { |
91 | | - eprintln!("Error: Unable to write code into file"); |
92 | | - std::process::exit(1); |
93 | | - } |
94 | | - if let Err(_) = std::io::Write::write_all(&mut file, comment.as_bytes()) { |
95 | | - eprintln!("Error: Unable to write code into file"); |
96 | | - std::process::exit(1); |
97 | | - } |
| 87 | + std::io::Write::write_all(&mut file, self.code.as_bytes())?; |
| 88 | + std::io::Write::write_all(&mut file, comment.as_bytes())?; |
| 89 | + |
| 90 | + Ok(()) |
98 | 91 | } |
99 | 92 | pub(crate) fn is_supported(&self) -> bool { |
100 | 93 | Language::from_slug(&self.langSlug).is_some() |
101 | 94 | } |
102 | | - pub(crate) fn extension(&self) -> String { |
103 | | - let language = Language::from_slug(&self.langSlug).unwrap_or_else(|| { |
104 | | - eprintln!("Error: Unable to identify language of code file!"); |
105 | | - std::process::exit(1); |
106 | | - }); |
107 | | - language.extension().to_owned() |
| 95 | + pub(crate) fn extension(&self) -> Result<String> { |
| 96 | + let language = Language::from_slug(&self.langSlug) |
| 97 | + .ok_or_else(|| eyre::eyre!("Unable to identify language of code file!"))?; |
| 98 | + Ok(language.extension().to_owned()) |
108 | 99 | } |
109 | 100 | } |
110 | 101 |
|
|
0 commit comments