fix: decl file generator panics

This commit is contained in:
Shunsuke Shibayama 2025-01-22 02:45:16 +09:00
parent b52b017c37
commit 814c46996d
1 changed files with 13 additions and 15 deletions

View File

@ -25,11 +25,11 @@ pub struct DeclFileGenerator {
} }
impl 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 (timestamp, hash) = {
let metadata = std::fs::metadata(path).unwrap(); let metadata = std::fs::metadata(path)?;
let dummy_hash = metadata.len(); let dummy_hash = metadata.len();
(metadata.modified().unwrap(), dummy_hash) (metadata.modified()?, dummy_hash)
}; };
let status = PylyzerStatus { let status = PylyzerStatus {
status, status,
@ -38,16 +38,16 @@ impl DeclFileGenerator {
hash, hash,
}; };
let code = format!("{status}\n"); let code = format!("{status}\n");
Self { Ok(Self {
filename: path filename: path
.file_name() .file_name()
.unwrap() .unwrap_or_default()
.to_string_lossy() .to_string_lossy()
.replace(".py", ".d.er"), .replace(".py", ".d.er"),
namespace: "".to_string(), namespace: "".to_string(),
imported: Set::new(), imported: Set::new(),
code, code,
} })
} }
pub fn gen_decl_er(mut self, hir: &HIR) -> DeclFile { pub fn gen_decl_er(mut self, hir: &HIR) -> DeclFile {
@ -209,11 +209,11 @@ impl DeclFileGenerator {
} }
} }
fn dump_decl_er(path: &NormalizedPathBuf, hir: &HIR, status: CheckStatus) { fn dump_decl_er(path: &NormalizedPathBuf, hir: &HIR, status: CheckStatus) -> std::io::Result<()> {
let decl_gen = DeclFileGenerator::new(path, status); let decl_gen = DeclFileGenerator::new(path, status)?;
let file = decl_gen.gen_decl_er(hir); let file = decl_gen.gen_decl_er(hir);
let Some(dir) = path.parent().and_then(|p| p.canonicalize().ok()) else { let Some(dir) = path.parent().and_then(|p| p.canonicalize().ok()) else {
return; return Ok(());
}; };
let cache_dir = dir.join("__pycache__"); let cache_dir = dir.join("__pycache__");
if !cache_dir.exists() { if !cache_dir.exists() {
@ -221,19 +221,17 @@ fn dump_decl_er(path: &NormalizedPathBuf, hir: &HIR, status: CheckStatus) {
} }
let path = cache_dir.join(file.filename); let path = cache_dir.join(file.filename);
if !path.exists() { if !path.exists() {
let _f = File::create(&path); File::create(&path)?;
} }
let Ok(f) = File::options().write(true).open(path) else { let f = File::options().write(true).open(path)?;
return;
};
let mut f = BufWriter::new(f); 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) { pub fn dump_decl_package(modules: &SharedModuleCache) {
for (path, module) in modules.raw_iter() { for (path, module) in modules.raw_iter() {
if let Some(hir) = module.hir.as_ref() { if let Some(hir) = module.hir.as_ref() {
dump_decl_er(path, hir, module.status); let _ = dump_decl_er(path, hir, module.status);
} }
} }
} }