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 diff --git a/typify/tests/compile-fail/malformed-schema.rs b/typify/tests/compile-fail/malformed-schema.rs new file mode 100644 index 00000000..b7a31fdb --- /dev/null +++ b/typify/tests/compile-fail/malformed-schema.rs @@ -0,0 +1,6 @@ +use typify::import_types; + +// Cargo.toml exists in trybuild's generated crate but is not valid JSON. +import_types!("Cargo.toml"); + +fn main() {} diff --git a/typify/tests/compile-fail/malformed-schema.stderr b/typify/tests/compile-fail/malformed-schema.stderr new file mode 100644 index 00000000..14eaee79 --- /dev/null +++ b/typify/tests/compile-fail/malformed-schema.stderr @@ -0,0 +1,5 @@ +error: couldn't parse file Cargo.toml: invalid type: sequence, expected struct RootSchema at line 1 column 1 + --> tests/compile-fail/malformed-schema.rs:4:15 + | +4 | import_types!("Cargo.toml"); + | ^^^^^^^^^^^^ diff --git a/typify/tests/compile_fail.rs b/typify/tests/compile_fail.rs new file mode 100644 index 00000000..1c61f8af --- /dev/null +++ b/typify/tests/compile_fail.rs @@ -0,0 +1,6 @@ +// Copyright 2026 Oxide Computer Company + +#[test] +fn test_compile_fail() { + trybuild::TestCases::new().compile_fail("tests/compile-fail/*.rs"); +}