1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
use Ident;
/// Converts a rust identifier to a typescript identifier.
/// Convert an arbitrary name to a valid Typescript field name.
///
/// If the name contains special characters it will be wrapped in quotes.
// /// Parse all `#[ts(..)]` attributes from the given slice.
// pub fn parse_attrs<'a, A>(attrs: &'a [Attribute]) -> Result<impl Iterator<Item = A>>
// where
// A: TryFrom<&'a Attribute, Error = Error>,
// {
// Ok(attrs
// .iter()
// .filter(|a| a.path.is_ident("ts"))
// .map(A::try_from)
// .collect::<Result<Vec<A>>>()?
// .into_iter())
// }
// /// Parse all `#[serde(..)]` attributes from the given slice.
// #[cfg(feature = "serde-compat")]
// #[allow(unused)]
// pub fn parse_serde_attrs<'a, A: TryFrom<&'a Attribute, Error = Error>>(
// attrs: &'a [Attribute],
// ) -> impl Iterator<Item = A> {
// attrs
// .iter()
// .filter(|a| a.path.is_ident("serde"))
// .flat_map(|attr| match A::try_from(attr) {
// Ok(attr) => Some(attr),
// Err(_) => {
// use quote::ToTokens;
// warning::print_warning(
// "failed to parse serde attribute",
// format!("{}", attr.to_token_stream()),
// "ts-rs failed to parse this attribute. It will be ignored.",
// )
// .unwrap();
// None
// }
// })
// .collect::<Vec<_>>()
// .into_iter()
// }
// #[cfg(feature = "serde-compat")]
// mod warning {
// use std::{fmt::Display, io::Write};
// use termcolor::{BufferWriter, Color, ColorChoice, ColorSpec, WriteColor};
// // Sadly, it is impossible to raise a warning in a proc macro.
// // This function prints a message which looks like a compiler warning.
// pub fn print_warning(
// title: impl Display,
// content: impl Display,
// note: impl Display,
// ) -> std::io::Result<()> {
// let make_color = |color: Color, bold: bool| {
// let mut spec = ColorSpec::new();
// spec.set_fg(Some(color)).set_bold(bold).set_intense(true);
// spec
// };
// let yellow_bold = make_color(Color::Yellow, true);
// let white_bold = make_color(Color::White, true);
// let white = make_color(Color::White, false);
// let blue = make_color(Color::Blue, true);
// let writer = BufferWriter::stderr(ColorChoice::Auto);
// let mut buffer = writer.buffer();
// buffer.set_color(&yellow_bold)?;
// write!(&mut buffer, "warning")?;
// buffer.set_color(&white_bold)?;
// writeln!(&mut buffer, ": {}", title)?;
// buffer.set_color(&blue)?;
// writeln!(&mut buffer, " | ")?;
// write!(&mut buffer, " | ")?;
// buffer.set_color(&white)?;
// writeln!(&mut buffer, "{}", content)?;
// buffer.set_color(&blue)?;
// writeln!(&mut buffer, " | ")?;
// write!(&mut buffer, " = ")?;
// buffer.set_color(&white_bold)?;
// write!(&mut buffer, "note: ")?;
// buffer.set_color(&white)?;
// writeln!(&mut buffer, "{}", note)?;
// writer.print(&buffer)
// }
// }