Skip to content

Add support for rustCustomDerive and rustCustomUse metadata #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion crates/target_rust/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use jtd_codegen::error::Error;
use jtd_codegen::target::{self, inflect, metadata};
use jtd_codegen::Result;
use lazy_static::lazy_static;
Expand Down Expand Up @@ -234,15 +235,52 @@ impl jtd_codegen::target::Target for Target {
return Ok(Some(s.into()));
}

let mut derives = vec!["Serialize", "Deserialize"];

if let Some(s) = metadata.get("rustCustomDerive").and_then(|v| v.as_str()) {
derives.extend(s.split(","));
}

state
.imports
.entry("serde".into())
.or_default()
.extend(vec!["Deserialize".to_owned(), "Serialize".to_owned()]);

let mut custom_use = Vec::<&str>::new();
if let Some(s) = metadata.get("rustCustomUse").and_then(|v| v.as_str()) {
custom_use.extend(s.split(";"));
}
for cu in custom_use {
// custom::path::{import,export} or custom::path::single
let mut use_imports = Vec::<&str>::new();
let mut path_parts = cu.split("::").collect::<Vec<&str>>();
let mut last_part = path_parts.pop().unwrap();
// If there are no path_parts or the last part was "", panic!
if path_parts.len() < 1 || last_part.trim().len() < 1 {
return Err(Error::Io(std::io::Error::new(
std::io::ErrorKind::Other,
format!("Invalid custom use statement: {:?}", cu),
)));
}
if last_part.starts_with('{') {
// Strip the first/last chars and split
last_part = &last_part[1..last_part.len() - 1];
use_imports.extend(last_part.split(","))
} else {
// No, just push it into the imports list
use_imports.push(last_part);
}
state
.imports
.entry(path_parts.join("::").into())
.or_default()
.extend(use_imports.drain(..).map(|i| i.trim().to_owned()));
}

writeln!(out)?;
write!(out, "{}", description(&metadata, 0))?;
writeln!(out, "#[derive(Serialize, Deserialize)]")?;
writeln!(out, "#[derive({})]", derives.join(", "))?;

if fields.is_empty() {
writeln!(out, "pub struct {} {{}}", name)?;
Expand Down