From fa1acb7b8594b5b131ca5fa13a68f55f236e18be Mon Sep 17 00:00:00 2001 From: Daniel Eades Date: Sun, 12 Jul 2026 07:12:48 +0100 Subject: [PATCH] Report malformed schema JSON as a compile error instead of panicking Previously, import_types! unwrapped the serde_json parse result, so a syntax error in the schema file surfaced as an opaque "proc macro panicked" error. Map the parse error to a spanned syn::Error, matching the existing handling of file-open failures and preserving serde_json's line/column information. --- typify-macro/src/lib.rs | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/typify-macro/src/lib.rs b/typify-macro/src/lib.rs index 69b74026..fd3a2dbf 100644 --- a/typify-macro/src/lib.rs +++ b/typify-macro/src/lib.rs @@ -252,14 +252,19 @@ fn do_import_types(item: TokenStream) -> Result { let path = dir.join(schema.value()); - let root_schema: schemars::schema::RootSchema = - serde_json::from_reader(std::fs::File::open(&path).map_err(|e| { - syn::Error::new( - schema.span(), - format!("couldn't read file {}: {}", schema.value(), e), - ) - })?) - .unwrap(); + let file = std::fs::File::open(&path).map_err(|e| { + syn::Error::new( + schema.span(), + format!("couldn't read file {}: {}", schema.value(), e), + ) + })?; + + let root_schema: schemars::schema::RootSchema = serde_json::from_reader(file).map_err(|e| { + syn::Error::new( + schema.span(), + format!("couldn't parse file {}: {}", schema.value(), e), + ) + })?; let mut type_space = TypeSpace::new(&settings); type_space