Skip to content

Commit 814c469

Browse files
committed
fix: decl file generator panics
1 parent b52b017 commit 814c469

File tree

1 file changed

+13
-15
lines changed

1 file changed

+13
-15
lines changed

crates/py2erg/gen_decl.rs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ pub struct DeclFileGenerator {
2525
}
2626

2727
impl DeclFileGenerator {
28-
pub fn new(path: &NormalizedPathBuf, status: CheckStatus) -> Self {
28+
pub fn new(path: &NormalizedPathBuf, status: CheckStatus) -> std::io::Result<Self> {
2929
let (timestamp, hash) = {
30-
let metadata = std::fs::metadata(path).unwrap();
30+
let metadata = std::fs::metadata(path)?;
3131
let dummy_hash = metadata.len();
32-
(metadata.modified().unwrap(), dummy_hash)
32+
(metadata.modified()?, dummy_hash)
3333
};
3434
let status = PylyzerStatus {
3535
status,
@@ -38,16 +38,16 @@ impl DeclFileGenerator {
3838
hash,
3939
};
4040
let code = format!("{status}\n");
41-
Self {
41+
Ok(Self {
4242
filename: path
4343
.file_name()
44-
.unwrap()
44+
.unwrap_or_default()
4545
.to_string_lossy()
4646
.replace(".py", ".d.er"),
4747
namespace: "".to_string(),
4848
imported: Set::new(),
4949
code,
50-
}
50+
})
5151
}
5252

5353
pub fn gen_decl_er(mut self, hir: &HIR) -> DeclFile {
@@ -209,31 +209,29 @@ impl DeclFileGenerator {
209209
}
210210
}
211211

212-
fn dump_decl_er(path: &NormalizedPathBuf, hir: &HIR, status: CheckStatus) {
213-
let decl_gen = DeclFileGenerator::new(path, status);
212+
fn dump_decl_er(path: &NormalizedPathBuf, hir: &HIR, status: CheckStatus) -> std::io::Result<()> {
213+
let decl_gen = DeclFileGenerator::new(path, status)?;
214214
let file = decl_gen.gen_decl_er(hir);
215215
let Some(dir) = path.parent().and_then(|p| p.canonicalize().ok()) else {
216-
return;
216+
return Ok(());
217217
};
218218
let cache_dir = dir.join("__pycache__");
219219
if !cache_dir.exists() {
220220
let _ = create_dir_all(&cache_dir);
221221
}
222222
let path = cache_dir.join(file.filename);
223223
if !path.exists() {
224-
let _f = File::create(&path);
224+
File::create(&path)?;
225225
}
226-
let Ok(f) = File::options().write(true).open(path) else {
227-
return;
228-
};
226+
let f = File::options().write(true).open(path)?;
229227
let mut f = BufWriter::new(f);
230-
let _ = f.write_all(file.code.as_bytes());
228+
f.write_all(file.code.as_bytes())
231229
}
232230

233231
pub fn dump_decl_package(modules: &SharedModuleCache) {
234232
for (path, module) in modules.raw_iter() {
235233
if let Some(hir) = module.hir.as_ref() {
236-
dump_decl_er(path, hir, module.status);
234+
let _ = dump_decl_er(path, hir, module.status);
237235
}
238236
}
239237
}

0 commit comments

Comments
 (0)