Skip to content

Commit

Permalink
fix: decl file generator panics
Browse files Browse the repository at this point in the history
  • Loading branch information
mtshiba committed Jan 21, 2025
1 parent b52b017 commit 814c469
Showing 1 changed file with 13 additions and 15 deletions.
28 changes: 13 additions & 15 deletions crates/py2erg/gen_decl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ pub struct DeclFileGenerator {
}

impl DeclFileGenerator {
pub fn new(path: &NormalizedPathBuf, status: CheckStatus) -> Self {
pub fn new(path: &NormalizedPathBuf, status: CheckStatus) -> std::io::Result<Self> {
let (timestamp, hash) = {
let metadata = std::fs::metadata(path).unwrap();
let metadata = std::fs::metadata(path)?;
let dummy_hash = metadata.len();
(metadata.modified().unwrap(), dummy_hash)
(metadata.modified()?, dummy_hash)
};
let status = PylyzerStatus {
status,
Expand All @@ -38,16 +38,16 @@ impl DeclFileGenerator {
hash,
};
let code = format!("{status}\n");
Self {
Ok(Self {
filename: path
.file_name()
.unwrap()
.unwrap_or_default()
.to_string_lossy()
.replace(".py", ".d.er"),
namespace: "".to_string(),
imported: Set::new(),
code,
}
})
}

pub fn gen_decl_er(mut self, hir: &HIR) -> DeclFile {
Expand Down Expand Up @@ -209,31 +209,29 @@ impl DeclFileGenerator {
}
}

fn dump_decl_er(path: &NormalizedPathBuf, hir: &HIR, status: CheckStatus) {
let decl_gen = DeclFileGenerator::new(path, status);
fn dump_decl_er(path: &NormalizedPathBuf, hir: &HIR, status: CheckStatus) -> std::io::Result<()> {
let decl_gen = DeclFileGenerator::new(path, status)?;
let file = decl_gen.gen_decl_er(hir);
let Some(dir) = path.parent().and_then(|p| p.canonicalize().ok()) else {
return;
return Ok(());
};
let cache_dir = dir.join("__pycache__");
if !cache_dir.exists() {
let _ = create_dir_all(&cache_dir);
}
let path = cache_dir.join(file.filename);
if !path.exists() {
let _f = File::create(&path);
File::create(&path)?;
}
let Ok(f) = File::options().write(true).open(path) else {
return;
};
let f = File::options().write(true).open(path)?;
let mut f = BufWriter::new(f);
let _ = f.write_all(file.code.as_bytes());
f.write_all(file.code.as_bytes())
}

pub fn dump_decl_package(modules: &SharedModuleCache) {
for (path, module) in modules.raw_iter() {
if let Some(hir) = module.hir.as_ref() {
dump_decl_er(path, hir, module.status);
let _ = dump_decl_er(path, hir, module.status);
}
}
}

0 comments on commit 814c469

Please sign in to comment.