|
| 1 | +use jtd_codegen::error::Error; |
1 | 2 | use jtd_codegen::target::{self, inflect, metadata};
|
2 | 3 | use jtd_codegen::Result;
|
3 | 4 | use lazy_static::lazy_static;
|
@@ -236,15 +237,52 @@ impl jtd_codegen::target::Target for Target {
|
236 | 237 | return Ok(Some(s.into()));
|
237 | 238 | }
|
238 | 239 |
|
| 240 | + let mut derives = vec!["Serialize", "Deserialize"]; |
| 241 | + |
| 242 | + if let Some(s) = metadata.get("rustCustomDerive").and_then(|v| v.as_str()) { |
| 243 | + derives.extend(s.split(",")); |
| 244 | + } |
| 245 | + |
239 | 246 | state
|
240 | 247 | .imports
|
241 | 248 | .entry("serde".into())
|
242 | 249 | .or_default()
|
243 | 250 | .extend(vec!["Deserialize".to_owned(), "Serialize".to_owned()]);
|
244 | 251 |
|
| 252 | + let mut custom_use = Vec::<&str>::new(); |
| 253 | + if let Some(s) = metadata.get("rustCustomUse").and_then(|v| v.as_str()) { |
| 254 | + custom_use.extend(s.split(";")); |
| 255 | + } |
| 256 | + for cu in custom_use { |
| 257 | + // custom::path::{import,export} or custom::path::single |
| 258 | + let mut use_imports = Vec::<&str>::new(); |
| 259 | + let mut path_parts = cu.split("::").collect::<Vec<&str>>(); |
| 260 | + let mut last_part = path_parts.pop().unwrap(); |
| 261 | + // If there are no path_parts or the last part was "", panic! |
| 262 | + if path_parts.len() < 1 || last_part.trim().len() < 1 { |
| 263 | + return Err(Error::Io(std::io::Error::new( |
| 264 | + std::io::ErrorKind::Other, |
| 265 | + format!("Invalid custom use statement: {:?}", cu), |
| 266 | + ))); |
| 267 | + } |
| 268 | + if last_part.starts_with('{') { |
| 269 | + // Strip the first/last chars and split |
| 270 | + last_part = &last_part[1..last_part.len() - 1]; |
| 271 | + use_imports.extend(last_part.split(",")) |
| 272 | + } else { |
| 273 | + // No, just push it into the imports list |
| 274 | + use_imports.push(last_part); |
| 275 | + } |
| 276 | + state |
| 277 | + .imports |
| 278 | + .entry(path_parts.join("::").into()) |
| 279 | + .or_default() |
| 280 | + .extend(use_imports.drain(..).map(|i| i.trim().to_owned())); |
| 281 | + } |
| 282 | + |
245 | 283 | writeln!(out)?;
|
246 | 284 | write!(out, "{}", description(&metadata, 0))?;
|
247 |
| - writeln!(out, "#[derive(Serialize, Deserialize)]")?; |
| 285 | + writeln!(out, "#[derive({})]", derives.join(", "))?; |
248 | 286 |
|
249 | 287 | if fields.is_empty() {
|
250 | 288 | writeln!(out, "pub struct {} {{}}", name)?;
|
|
0 commit comments