|
1 | 1 | // TODO: CLEAN ALL THIS SHIT UP WHAT THE FUCK IS THIS
|
2 | 2 |
|
3 | 3 | use crate::bail;
|
4 |
| -use owo_colors::{OwoColorize, Stream, Style}; |
| 4 | +use owo_colors::{OwoColorize, Style}; |
5 | 5 | use proc_macro2::TokenStream;
|
6 | 6 | use quote::{format_ident, quote};
|
7 | 7 | use std::{
|
8 |
| - borrow::Cow, |
9 |
| - fmt::Display, |
10 |
| - io::{self, Write as _}, |
| 8 | + borrow::Cow, env, fmt::Display, io::{self, Write as _} |
11 | 9 | };
|
12 | 10 | use syn::{DataEnum, DataStruct, DataUnion, DeriveInput, Lit};
|
13 | 11 |
|
14 |
| -fn print_warning(title: impl Display, content: impl Display) -> io::Result<()> { |
15 |
| - let mut sink = io::stderr(); |
| 12 | +const DISABLE_WARNINGS_VAR: &str = "SHUT_UP_CW_SCHEMA_DERIVE"; |
16 | 13 |
|
17 |
| - macro_rules! apply_style { |
18 |
| - ($style:ident => $content:expr) => {{ |
19 |
| - //$content.if_supports_color(Stream::Stderr, |txt| txt.style($style)) |
20 |
| - $content.style($style) |
21 |
| - }}; |
| 14 | +fn print_warning(title: impl Display, content: impl Display) -> io::Result<()> { |
| 15 | + if let Ok("1") = env::var(DISABLE_WARNINGS_VAR).as_deref() { |
| 16 | + return Ok(()); |
22 | 17 | }
|
23 | 18 |
|
| 19 | + let mut sink = io::stderr(); |
| 20 | + |
24 | 21 | let bold_yellow = Style::new().bold().yellow();
|
25 | 22 | let bold = Style::new().bold();
|
26 | 23 | let blue = Style::new().blue();
|
27 | 24 |
|
28 |
| - write!(sink, "{}", apply_style!(bold_yellow => "warning"))?; |
29 |
| - writeln!(sink, "{}", apply_style!(bold => format_args!(": {title}")))?; |
| 25 | + write!(sink, "{}", "warning".style(bold_yellow))?; |
| 26 | + writeln!( |
| 27 | + sink, |
| 28 | + "{}", |
| 29 | + format_args!("({}): {title}", env!("CARGO_PKG_NAME")).style(bold) |
| 30 | + )?; |
30 | 31 |
|
31 |
| - writeln!(sink, "{}", apply_style!(blue => " | "))?; |
32 |
| - write!(sink, "{}", apply_style!(blue => " | "))?; |
| 32 | + writeln!(sink, "{}", " | ".style(blue))?; |
| 33 | + write!(sink, "{}", " | ".style(blue))?; |
33 | 34 | writeln!(sink, "{content}")?;
|
34 | 35 |
|
| 36 | + writeln!(sink, "{}", " | ".style(blue))?; |
| 37 | + writeln!(sink, "{}", " | ".style(blue))?; |
| 38 | + |
| 39 | + write!(sink, "{}", " = ".style(blue))?; |
| 40 | + write!(sink, "{}", "note: ".style(bold))?; |
| 41 | + writeln!(sink, "set `{DISABLE_WARNINGS_VAR}=1` to silence this warning")?; |
| 42 | + |
35 | 43 | Ok(())
|
36 | 44 | }
|
37 | 45 |
|
@@ -272,7 +280,12 @@ where
|
272 | 280 | C: Fn(&syn::Ident) -> syn::Ident,
|
273 | 281 | {
|
274 | 282 | fields.named.iter().map(move |field| {
|
275 |
| - let name = converter(field.ident.as_ref().unwrap()); |
| 283 | + let field_options = SerdeFieldOptions::parse(&field.attrs)?; |
| 284 | + |
| 285 | + let name = field_options |
| 286 | + .rename |
| 287 | + .map(|lit_str| format_ident!("{}", lit_str.value())) |
| 288 | + .unwrap_or_else(|| converter(field.ident.as_ref().unwrap())); |
276 | 289 | let description = normalize_option(extract_documentation(&field.attrs)?);
|
277 | 290 | let field_ty = &field.ty;
|
278 | 291 |
|
@@ -325,7 +338,12 @@ fn expand_enum(mut meta: ContainerMeta, input: DataEnum) -> syn::Result<TokenStr
|
325 | 338 | syn::Fields::Unit => quote! { #crate_path::EnumValue::Unit },
|
326 | 339 | };
|
327 | 340 |
|
328 |
| - let variant_name = converter(&variant.ident); |
| 341 | + let field_options = SerdeFieldOptions::parse(&variant.attrs)?; |
| 342 | + |
| 343 | + let variant_name = field_options |
| 344 | + .rename |
| 345 | + .map(|lit_str| format_ident!("{}", lit_str.value())) |
| 346 | + .unwrap_or_else(|| converter(&variant.ident)); |
329 | 347 | let description = normalize_option(extract_documentation(&variant.attrs)?);
|
330 | 348 |
|
331 | 349 | let expanded = quote! {
|
|
0 commit comments