Skip to content

Commit

Permalink
current state of oxidecomputer#621 for cargo-typify
Browse files Browse the repository at this point in the history
  • Loading branch information
LimpidCrypto committed Dec 23, 2024
1 parent 4b6c4c3 commit 256dff9
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 3 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions cargo-typify/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ pub struct CliArgs {
value_parser = ["generate", "allow", "deny"]
)]
unknown_crates: Option<String>,

#[arg(short = 'D', long, default_value = "false")]
distinct_definitions: bool,
}

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

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")?;
Expand Down Expand Up @@ -202,6 +207,7 @@ mod tests {
crates: vec![],
map_type: None,
unknown_crates: Default::default(),
distinct_definitions: false,
};

assert_eq!(args.output_path(), None);
Expand All @@ -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")));
Expand All @@ -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")));
Expand All @@ -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!(
Expand All @@ -269,6 +278,7 @@ mod tests {
crates: vec![],
map_type: None,
unknown_crates: Default::default(),
distinct_definitions: false,
};

assert!(args.use_builder());
Expand All @@ -285,6 +295,7 @@ mod tests {
crates: vec![],
map_type: None,
unknown_crates: Default::default(),
distinct_definitions: false,
};

assert!(!args.use_builder());
Expand All @@ -301,6 +312,7 @@ mod tests {
crates: vec![],
map_type: None,
unknown_crates: Default::default(),
distinct_definitions: false,
};

assert!(args.use_builder());
Expand Down
6 changes: 4 additions & 2 deletions cargo-typify/tests/outputs/help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Options:

-o, --output <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 <CRATES>
Expand All @@ -29,9 +29,11 @@ Options:

--unknown-crates <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')

Expand Down
1 change: 1 addition & 0 deletions typify-impl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
2 changes: 2 additions & 0 deletions typify-impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -1217,6 +1218,7 @@ fn fetch_external_definitions(
.collect::<Vec<_>>(); // 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: {}",
Expand Down
6 changes: 6 additions & 0 deletions typify-impl/tests/test_external_references.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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");
}
2 changes: 1 addition & 1 deletion typify-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 256dff9

Please sign in to comment.