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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
use row::Row;
use std::path::PathBuf;
fn main() {
let raw_csv = include_str!("list.csv"); // list of pnp ids
let rdr = csv::Reader::from_reader(raw_csv.as_bytes());
// make a list of "arms" for the giant match statement
let mut match_arms = Vec::new();
// a list of array entries
let mut array_entries = Vec::new();
// we keep track of the longest company name
let mut max_len = 0_usize;
// we'll add a new match line for each row of csv
for row in rdr.into_deserialize::<Row>().flatten() {
// destructure
let Row {
company,
id,
_approved_on_date,
} = row;
// replace weird "non-break space" characters
let company = company.replace('\u{a0}', " ");
let id = id.replace('\u{a0}', ""); // EVEN THE IDS HAVE IT??? see: `AMS`
// trim the value
let company = company.trim();
// add to the list of const match arms
match_arms.push(format!("\"{id}\" => Some(\"{company}\"),"));
// and to the list of array entries
array_entries.push(format!("(\"{id}\", \"{company}\"),"));
// check if we're the longest company so far
if company.len() > max_len {
max_len = company.len();
}
}
// make sure the list isn't empty
if match_arms.is_empty() {
panic!("Didn't detect any entries in the given file.");
}
// remove consecutive duplicates (i'm looking at you, DemoPad Software Ltd!)
match_arms.dedup_by(|a, b| *a == *b);
#[cfg(feature = "array")]
array_entries.dedup_by(|a, b| *a == *b);
// grab the length of the list (used to make array constant)
let num_of_entries = match_arms.len();
let array = if cfg!(feature = "array") {
// create array
let mut s =
format!("pub(crate) const _ALL_COMPANIES: [(&str, &str); {num_of_entries}] = [");
// each entry goes here
for entry in array_entries {
s.push_str(" ");
s.push_str(&entry);
s.push('\n');
}
// close array
s.push_str("];");
s
} else {
String::new()
};
// this is the final output we'll write to the file.
let output = {
let mut s = String::new();
// start with non-list stuff
s.push_str(&format!(
"pub(crate) const _MAX_LEN: usize = {max_len};
pub(crate) const _NUM_OF_ENTRIES: usize = {num_of_entries};
pub(crate) fn _company_from_pnp_id(id: &str) -> Option<&'static str> {{
match id {{",
));
// add the list (match)
for arm in match_arms {
s.push_str(" ");
s.push_str(&arm);
s.push('\n');
}
// add none arm
s.push_str(" _ => None,\n");
// close func
s.push_str(" }\n}\n\n");
// add array
s.push_str(&array);
// return total file
s
};
// the file should be at `$OUT_DIR/pnpid/___pnpid.rs`
let build_dir = std::env::var("OUT_DIR").expect("out_dir should be defined during builds");
let output_path = PathBuf::from(build_dir).join("pnpid");
// write it!
_ = std::fs::create_dir_all(&output_path); // make the dir
std::fs::write(output_path.join("___pnpid.rs"), output)
.expect("we have perms to write here. because we're compiling rn...");
}
mod row {
use core::marker::PhantomData;
use serde::de::Error as _;
#[derive(Debug)]
pub struct Row {
pub company: String,
pub id: String,
pub _approved_on_date: String,
}
#[doc(hidden)]
#[allow(
non_upper_case_globals,
unused_attributes,
unused_qualifications,
clippy::absolute_paths
)]
const _: () = {
#[allow(unused_extern_crates, clippy::useless_attribute)]
#[automatically_derived]
impl<'de> serde::Deserialize<'de> for Row {
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where
__D: serde::Deserializer<'de>,
{
#[allow(non_camel_case_types)]
#[doc(hidden)]
enum __Field {
__field0,
__field1,
__field2,
__ignore,
}
#[doc(hidden)]
struct __FieldVisitor;
#[automatically_derived]
impl<'de> serde::de::Visitor<'de> for __FieldVisitor {
type Value = __Field;
fn expecting(
&self,
__formatter: &mut core::fmt::Formatter,
) -> core::fmt::Result {
core::fmt::Formatter::write_str(__formatter, "field identifier")
}
fn visit_u64<__E>(self, __value: u64) -> Result<Self::Value, __E>
where
__E: serde::de::Error,
{
match __value {
0u64 => Ok(__Field::__field0),
1u64 => Ok(__Field::__field1),
2u64 => Ok(__Field::__field2),
_ => Ok(__Field::__ignore),
}
}
fn visit_str<__E>(self, __value: &str) -> Result<Self::Value, __E>
where
__E: serde::de::Error,
{
match __value {
"Company" => Ok(__Field::__field0),
"PNP ID" => Ok(__Field::__field1),
"Approved On Date" => Ok(__Field::__field2),
_ => Ok(__Field::__ignore),
}
}
fn visit_bytes<__E>(self, __value: &[u8]) -> Result<Self::Value, __E>
where
__E: serde::de::Error,
{
match __value {
b"Company" => Ok(__Field::__field0),
b"PNP ID" => Ok(__Field::__field1),
b"Approved On Date" => Ok(__Field::__field2),
_ => Ok(__Field::__ignore),
}
}
}
#[automatically_derived]
impl<'de> serde::Deserialize<'de> for __Field {
#[inline]
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where
__D: serde::Deserializer<'de>,
{
serde::Deserializer::deserialize_identifier(__deserializer, __FieldVisitor)
}
}
#[doc(hidden)]
struct __Visitor<'de> {
marker: PhantomData<Row>,
lifetime: PhantomData<&'de ()>,
}
#[automatically_derived]
impl<'de> serde::de::Visitor<'de> for __Visitor<'de> {
type Value = Row;
fn expecting(
&self,
__formatter: &mut core::fmt::Formatter,
) -> core::fmt::Result {
core::fmt::Formatter::write_str(__formatter, "struct Row")
}
#[inline]
fn visit_seq<__A>(self, mut __seq: __A) -> Result<Self::Value, __A::Error>
where
__A: serde::de::SeqAccess<'de>,
{
let __field0 =
match serde::de::SeqAccess::next_element::<String>(&mut __seq)? {
Some(__value) => __value,
None => {
return Err(serde::de::Error::invalid_length(
0usize,
&"struct Row with 3 elements",
));
}
};
let __field1 =
match serde::de::SeqAccess::next_element::<String>(&mut __seq)? {
Some(__value) => __value,
None => {
return Err(serde::de::Error::invalid_length(
1usize,
&"struct Row with 3 elements",
));
}
};
let __field2 =
match serde::de::SeqAccess::next_element::<String>(&mut __seq)? {
Some(__value) => __value,
None => {
return Err(serde::de::Error::invalid_length(
2usize,
&"struct Row with 3 elements",
));
}
};
Ok(Row {
company: __field0,
id: __field1,
_approved_on_date: __field2,
})
}
#[inline]
fn visit_map<__A>(self, mut __map: __A) -> Result<Self::Value, __A::Error>
where
__A: serde::de::MapAccess<'de>,
{
let mut __field0: Option<String> = None;
let mut __field1: Option<String> = None;
let mut __field2: Option<String> = None;
while let Some(__key) =
serde::de::MapAccess::next_key::<__Field>(&mut __map)?
{
match __key {
__Field::__field0 => {
if Option::is_some(&__field0) {
return Err(
<__A::Error as serde::de::Error>::duplicate_field(
"Company",
),
);
}
__field0 = Some(serde::de::MapAccess::next_value::<String>(
&mut __map,
)?);
}
__Field::__field1 => {
if Option::is_some(&__field1) {
return Err(
<__A::Error as serde::de::Error>::duplicate_field(
"PNP ID",
),
);
}
__field1 = Some(serde::de::MapAccess::next_value::<String>(
&mut __map,
)?);
}
__Field::__field2 => {
if Option::is_some(&__field2) {
return Err(
<__A::Error as serde::de::Error>::duplicate_field(
"Approved On Date",
),
);
}
__field2 = Some(serde::de::MapAccess::next_value::<String>(
&mut __map,
)?);
}
_ => {
let _ = serde::de::MapAccess::next_value::<
serde::de::IgnoredAny,
>(&mut __map)?;
}
}
}
let __field0 = match __field0 {
Some(__field0) => __field0,
None => Err(__A::Error::missing_field("Company"))?,
};
let __field1 = match __field1 {
Some(__field1) => __field1,
None => Err(__A::Error::missing_field("PNP ID"))?,
};
let __field2 = match __field2 {
Some(__field2) => __field2,
None => Err(__A::Error::missing_field("Approved On Date"))?,
};
Ok(Row {
company: __field0,
id: __field1,
_approved_on_date: __field2,
})
}
}
#[doc(hidden)]
const FIELDS: &[&str] = &["Company", "PNP ID", "Approved On Date"];
serde::Deserializer::deserialize_struct(
__deserializer,
"Row",
FIELDS,
__Visitor {
marker: PhantomData::<Row>,
lifetime: PhantomData,
},
)
}
}
};
}