diff --git a/Cargo.lock b/Cargo.lock index fd0c3940..b2b4fb10 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1539,6 +1539,7 @@ dependencies = [ "serde_json", "syn 2.0.90", "thiserror 2.0.9", + "typify-macro", "unicode-ident", "uuid", ] diff --git a/cargo-typify/src/lib.rs b/cargo-typify/src/lib.rs index 101453d3..277adef4 100644 --- a/cargo-typify/src/lib.rs +++ b/cargo-typify/src/lib.rs @@ -57,6 +57,9 @@ pub struct CliArgs { value_parser = ["generate", "allow", "deny"] )] unknown_crates: Option, + + #[arg(short = 'D', long, default_value = "false")] + distinct_definitions: bool, } impl CliArgs { @@ -170,6 +173,8 @@ pub fn convert(args: &CliArgs) -> Result { } let mut type_space = TypeSpace::new(&settings); + type_space.with_path(&args.input); + type_space.distinct_defs(args.distinct_definitions); type_space .add_root_schema(schema) .wrap_err("Schema conversion failed")?; @@ -202,6 +207,7 @@ mod tests { crates: vec![], map_type: None, unknown_crates: Default::default(), + distinct_definitions: false, }; assert_eq!(args.output_path(), None); @@ -218,6 +224,7 @@ mod tests { crates: vec![], map_type: None, unknown_crates: Default::default(), + distinct_definitions: false, }; assert_eq!(args.output_path(), Some(PathBuf::from("some_file.rs"))); @@ -234,6 +241,7 @@ mod tests { crates: vec![], map_type: None, unknown_crates: Default::default(), + distinct_definitions: false, }; assert_eq!(args.output_path(), Some(PathBuf::from("input.rs"))); @@ -250,6 +258,7 @@ mod tests { crates: vec![], map_type: Some("::std::collections::BTreeMap".to_string()), unknown_crates: Default::default(), + distinct_definitions: false, }; assert_eq!( @@ -269,6 +278,7 @@ mod tests { crates: vec![], map_type: None, unknown_crates: Default::default(), + distinct_definitions: false, }; assert!(args.use_builder()); @@ -285,6 +295,7 @@ mod tests { crates: vec![], map_type: None, unknown_crates: Default::default(), + distinct_definitions: false, }; assert!(!args.use_builder()); @@ -301,6 +312,7 @@ mod tests { crates: vec![], map_type: None, unknown_crates: Default::default(), + distinct_definitions: false, }; assert!(args.use_builder()); diff --git a/cargo-typify/tests/outputs/help.txt b/cargo-typify/tests/outputs/help.txt index 9daedee2..59a019e3 100644 --- a/cargo-typify/tests/outputs/help.txt +++ b/cargo-typify/tests/outputs/help.txt @@ -18,7 +18,7 @@ Options: -o, --output The output file to write to. If not specified, the input file name will be used with a `.rs` extension. - + If `-` is specified, the output will be written to stdout. --crate @@ -29,9 +29,11 @@ Options: --unknown-crates Specify the policy unknown crates found in schemas with the x-rust-type extension - + [possible values: generate, allow, deny] + -D, --distinct-definitions + -h, --help Print help (see a summary with '-h') diff --git a/typify-impl/Cargo.toml b/typify-impl/Cargo.toml index ccf6c9b9..5bbab720 100644 --- a/typify-impl/Cargo.toml +++ b/typify-impl/Cargo.toml @@ -33,3 +33,4 @@ schema = "0.1.0" schemars = { version = "0.8.21", features = ["uuid1", "impl_json_schema"] } syn = { version = "2.0.90", features = ["full", "extra-traits", "visit-mut"] } uuid = "1.11.0" +typify-macro = { path = "../typify-macro" } diff --git a/typify-impl/src/lib.rs b/typify-impl/src/lib.rs index c8faedfc..a610fb00 100644 --- a/typify-impl/src/lib.rs +++ b/typify-impl/src/lib.rs @@ -864,6 +864,7 @@ impl TypeSpace { // recursively fetch external references from definitions let mut external_references = BTreeMap::new(); + dbg!(&self.file_path); for (_, def) in &defs { fetch_external_definitions( &schema, @@ -1217,6 +1218,7 @@ fn fetch_external_definitions( .collect::>(); // Process the fragment part of the reference let relpath = diff_paths(reff.path().as_str(), id.path().parent_or_empty().as_str()).unwrap(); // Determine the relative path + dbg!(&base_path); let file_path = base_path.parent().unwrap().join(&relpath); // Construct the file path let content = std::fs::read_to_string(&file_path).expect(&format!( "Failed to open input file: {}", diff --git a/typify-impl/tests/test_external_references.rs b/typify-impl/tests/test_external_references.rs index bf100562..d515c8d3 100644 --- a/typify-impl/tests/test_external_references.rs +++ b/typify-impl/tests/test_external_references.rs @@ -3,6 +3,7 @@ use std::fs::File; use std::io::BufReader; use std::path::Path; use typify_impl::TypeSpace; +use typify_macro::import_types; #[test] fn test_external_references() { @@ -22,3 +23,8 @@ fn test_external_references() { expectorate::assert_contents("tests/external_references.out", fmt.as_str()); } + +#[test] +fn test_external_references_from_macro() { + import_types!(schema = "tests/external_references.json"); +} diff --git a/typify-macro/src/lib.rs b/typify-macro/src/lib.rs index 3262c1ac..45621cd5 100644 --- a/typify-macro/src/lib.rs +++ b/typify-macro/src/lib.rs @@ -59,7 +59,7 @@ mod token_utils; /// - `replace`: optional map from definition name to a replacement type. This /// may be used to skip generation of the named type and use a existing Rust /// type. -/// +/// /// - `convert`: optional map from a JSON schema type defined in `$defs` to a /// replacement type. This may be used to skip generation of the schema and /// use an existing Rust type.