Skip to content

Commit 9e971d6

Browse files
committed
Add rename option and option to mute warnings
1 parent 00865e0 commit 9e971d6

File tree

2 files changed

+36
-132
lines changed

2 files changed

+36
-132
lines changed

packages/cw-schema-derive/src/expand.rs

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,45 @@
11
// TODO: CLEAN ALL THIS SHIT UP WHAT THE FUCK IS THIS
22

33
use crate::bail;
4-
use owo_colors::{OwoColorize, Stream, Style};
4+
use owo_colors::{OwoColorize, Style};
55
use proc_macro2::TokenStream;
66
use quote::{format_ident, quote};
77
use std::{
8-
borrow::Cow,
9-
fmt::Display,
10-
io::{self, Write as _},
8+
borrow::Cow, env, fmt::Display, io::{self, Write as _}
119
};
1210
use syn::{DataEnum, DataStruct, DataUnion, DeriveInput, Lit};
1311

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";
1613

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(());
2217
}
2318

19+
let mut sink = io::stderr();
20+
2421
let bold_yellow = Style::new().bold().yellow();
2522
let bold = Style::new().bold();
2623
let blue = Style::new().blue();
2724

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+
)?;
3031

31-
writeln!(sink, "{}", apply_style!(blue => " | "))?;
32-
write!(sink, "{}", apply_style!(blue => " | "))?;
32+
writeln!(sink, "{}", " | ".style(blue))?;
33+
write!(sink, "{}", " | ".style(blue))?;
3334
writeln!(sink, "{content}")?;
3435

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+
3543
Ok(())
3644
}
3745

@@ -272,7 +280,12 @@ where
272280
C: Fn(&syn::Ident) -> syn::Ident,
273281
{
274282
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()));
276289
let description = normalize_option(extract_documentation(&field.attrs)?);
277290
let field_ty = &field.ty;
278291

@@ -325,7 +338,12 @@ fn expand_enum(mut meta: ContainerMeta, input: DataEnum) -> syn::Result<TokenStr
325338
syn::Fields::Unit => quote! { #crate_path::EnumValue::Unit },
326339
};
327340

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));
329347
let description = normalize_option(extract_documentation(&variant.attrs)?);
330348

331349
let expanded = quote! {

packages/cw-schema/tests/basic.rs

Lines changed: 1 addition & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -1,118 +1,4 @@
1-
use cw_schema::{EnumCase, Node, NodeType, Schema, SchemaV1, StructProperty};
2-
3-
#[test]
4-
fn roundtrip() {
5-
/*
6-
let schema_struct = Schema::V1(SchemaV1 {
7-
root: Node {
8-
name: Some("root".into()),
9-
description: Some("root node".into()),
10-
optional: false,
11-
value: NodeContent::Concrete(NodeType::Object {
12-
properties: vec![
13-
StructProperty {
14-
name: "foo".into(),
15-
description: Some("foo property".into()),
16-
value: Node {
17-
name: None,
18-
description: None,
19-
optional: false,
20-
value: NodeContent::Concrete(NodeType::String),
21-
},
22-
},
23-
StructProperty {
24-
name: "bar".into(),
25-
description: Some("bar property".into()),
26-
value: Node {
27-
name: None,
28-
description: None,
29-
optional: false,
30-
value: NodeContent::Concrete(NodeType::Integer {
31-
signed: false,
32-
precision: 64,
33-
}),
34-
},
35-
},
36-
StructProperty {
37-
name: "union".into(),
38-
description: Some("union property".into()),
39-
value: Node {
40-
name: None,
41-
description: None,
42-
optional: false,
43-
value: NodeContent::OneOf {
44-
one_of: vec![
45-
Node {
46-
name: None,
47-
description: None,
48-
optional: true,
49-
value: NodeContent::Concrete(NodeType::String),
50-
},
51-
Node {
52-
name: None,
53-
description: None,
54-
optional: false,
55-
value: NodeContent::Concrete(NodeType::Integer {
56-
signed: true,
57-
precision: 128,
58-
}),
59-
},
60-
],
61-
},
62-
},
63-
},
64-
StructProperty {
65-
name: "tagged_union".into(),
66-
description: Some("tagged union property".into()),
67-
value: Node {
68-
name: None,
69-
description: None,
70-
optional: false,
71-
value: NodeContent::Concrete(NodeType::Enum {
72-
discriminator: Some("type".into()),
73-
cases: vec![
74-
EnumCase {
75-
name: "string".into(),
76-
description: Some("string case".into()),
77-
discriminator_value: None,
78-
value: Some(Node {
79-
name: None,
80-
description: None,
81-
optional: true,
82-
value: NodeContent::Concrete(NodeType::String),
83-
}),
84-
},
85-
EnumCase {
86-
name: "number".into(),
87-
description: Some("number case".into()),
88-
discriminator_value: None,
89-
value: Some(Node {
90-
name: None,
91-
description: None,
92-
optional: false,
93-
value: NodeContent::Concrete(NodeType::Integer {
94-
signed: false,
95-
precision: 8,
96-
}),
97-
}),
98-
},
99-
],
100-
}),
101-
},
102-
},
103-
],
104-
}),
105-
},
106-
});
107-
108-
let schema = serde_json::to_string(&schema_struct).unwrap();
109-
110-
pretty_assertions::assert_eq!(
111-
schema_struct,
112-
serde_json::from_str::<Schema>(&schema).unwrap()
113-
);
114-
*/
115-
}
1+
use cw_schema::Schema;
1162

1173
#[test]
1184
fn can_decode_example() {

0 commit comments

Comments
 (0)