Skip to content

Commit 256dff9

Browse files
committed
current state of oxidecomputer#621 for cargo-typify
1 parent 4b6c4c3 commit 256dff9

File tree

7 files changed

+27
-3
lines changed

7 files changed

+27
-3
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cargo-typify/src/lib.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ pub struct CliArgs {
5757
value_parser = ["generate", "allow", "deny"]
5858
)]
5959
unknown_crates: Option<String>,
60+
61+
#[arg(short = 'D', long, default_value = "false")]
62+
distinct_definitions: bool,
6063
}
6164

6265
impl CliArgs {
@@ -170,6 +173,8 @@ pub fn convert(args: &CliArgs) -> Result<String> {
170173
}
171174

172175
let mut type_space = TypeSpace::new(&settings);
176+
type_space.with_path(&args.input);
177+
type_space.distinct_defs(args.distinct_definitions);
173178
type_space
174179
.add_root_schema(schema)
175180
.wrap_err("Schema conversion failed")?;
@@ -202,6 +207,7 @@ mod tests {
202207
crates: vec![],
203208
map_type: None,
204209
unknown_crates: Default::default(),
210+
distinct_definitions: false,
205211
};
206212

207213
assert_eq!(args.output_path(), None);
@@ -218,6 +224,7 @@ mod tests {
218224
crates: vec![],
219225
map_type: None,
220226
unknown_crates: Default::default(),
227+
distinct_definitions: false,
221228
};
222229

223230
assert_eq!(args.output_path(), Some(PathBuf::from("some_file.rs")));
@@ -234,6 +241,7 @@ mod tests {
234241
crates: vec![],
235242
map_type: None,
236243
unknown_crates: Default::default(),
244+
distinct_definitions: false,
237245
};
238246

239247
assert_eq!(args.output_path(), Some(PathBuf::from("input.rs")));
@@ -250,6 +258,7 @@ mod tests {
250258
crates: vec![],
251259
map_type: Some("::std::collections::BTreeMap".to_string()),
252260
unknown_crates: Default::default(),
261+
distinct_definitions: false,
253262
};
254263

255264
assert_eq!(
@@ -269,6 +278,7 @@ mod tests {
269278
crates: vec![],
270279
map_type: None,
271280
unknown_crates: Default::default(),
281+
distinct_definitions: false,
272282
};
273283

274284
assert!(args.use_builder());
@@ -285,6 +295,7 @@ mod tests {
285295
crates: vec![],
286296
map_type: None,
287297
unknown_crates: Default::default(),
298+
distinct_definitions: false,
288299
};
289300

290301
assert!(!args.use_builder());
@@ -301,6 +312,7 @@ mod tests {
301312
crates: vec![],
302313
map_type: None,
303314
unknown_crates: Default::default(),
315+
distinct_definitions: false,
304316
};
305317

306318
assert!(args.use_builder());

cargo-typify/tests/outputs/help.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Options:
1818

1919
-o, --output <OUTPUT>
2020
The output file to write to. If not specified, the input file name will be used with a `.rs` extension.
21-
21+
2222
If `-` is specified, the output will be written to stdout.
2323

2424
--crate <CRATES>
@@ -29,9 +29,11 @@ Options:
2929

3030
--unknown-crates <UNKNOWN_CRATES>
3131
Specify the policy unknown crates found in schemas with the x-rust-type extension
32-
32+
3333
[possible values: generate, allow, deny]
3434

35+
-D, --distinct-definitions
36+
3537
-h, --help
3638
Print help (see a summary with '-h')
3739

typify-impl/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,4 @@ schema = "0.1.0"
3333
schemars = { version = "0.8.21", features = ["uuid1", "impl_json_schema"] }
3434
syn = { version = "2.0.90", features = ["full", "extra-traits", "visit-mut"] }
3535
uuid = "1.11.0"
36+
typify-macro = { path = "../typify-macro" }

typify-impl/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -864,6 +864,7 @@ impl TypeSpace {
864864
// recursively fetch external references from definitions
865865
let mut external_references = BTreeMap::new();
866866

867+
dbg!(&self.file_path);
867868
for (_, def) in &defs {
868869
fetch_external_definitions(
869870
&schema,
@@ -1217,6 +1218,7 @@ fn fetch_external_definitions(
12171218
.collect::<Vec<_>>(); // Process the fragment part of the reference
12181219
let relpath =
12191220
diff_paths(reff.path().as_str(), id.path().parent_or_empty().as_str()).unwrap(); // Determine the relative path
1221+
dbg!(&base_path);
12201222
let file_path = base_path.parent().unwrap().join(&relpath); // Construct the file path
12211223
let content = std::fs::read_to_string(&file_path).expect(&format!(
12221224
"Failed to open input file: {}",

typify-impl/tests/test_external_references.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::fs::File;
33
use std::io::BufReader;
44
use std::path::Path;
55
use typify_impl::TypeSpace;
6+
use typify_macro::import_types;
67

78
#[test]
89
fn test_external_references() {
@@ -22,3 +23,8 @@ fn test_external_references() {
2223

2324
expectorate::assert_contents("tests/external_references.out", fmt.as_str());
2425
}
26+
27+
#[test]
28+
fn test_external_references_from_macro() {
29+
import_types!(schema = "tests/external_references.json");
30+
}

typify-macro/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ mod token_utils;
5959
/// - `replace`: optional map from definition name to a replacement type. This
6060
/// may be used to skip generation of the named type and use a existing Rust
6161
/// type.
62-
///
62+
///
6363
/// - `convert`: optional map from a JSON schema type defined in `$defs` to a
6464
/// replacement type. This may be used to skip generation of the schema and
6565
/// use an existing Rust type.

0 commit comments

Comments
 (0)