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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
//
// iso - implementations of data types related to common iso standards
// Copyright (c) 2021 superwhiskers <whiskerdev@protonmail.com>
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

//

#![allow(clippy::cognitive_complexity)]
#![warn(clippy::cargo_common_metadata)]
#![warn(clippy::dbg_macro)]
#![warn(clippy::explicit_deref_methods)]
#![warn(clippy::filetype_is_file)]
#![warn(clippy::imprecise_flops)]
#![warn(clippy::large_stack_arrays)]
#![warn(clippy::todo)]
#![warn(clippy::unimplemented)]
#![deny(clippy::await_holding_lock)]
#![deny(clippy::cast_lossless)]
#![deny(clippy::clone_on_ref_ptr)]
#![deny(clippy::doc_markdown)]
#![deny(clippy::empty_enum)]
#![deny(clippy::enum_glob_use)]
#![deny(clippy::exit)]
#![deny(clippy::explicit_into_iter_loop)]
#![deny(clippy::explicit_iter_loop)]
#![deny(clippy::fallible_impl_from)]
#![deny(clippy::inefficient_to_string)]
#![deny(clippy::large_digit_groups)]
#![deny(clippy::wildcard_dependencies)]
#![deny(clippy::wildcard_imports)]
#![deny(clippy::unused_self)]
#![deny(clippy::single_match_else)]
#![deny(clippy::option_option)]
#![deny(clippy::mut_mut)]
#![feature(proc_macro_diagnostic)]

use proc_macro::{Diagnostic, Level, TokenStream};
use proc_macro2::{Literal, Span, TokenStream as TokenStream2};
use quote::{quote, ToTokens};
use serde::Deserialize;
use std::{
    collections::HashMap,
    convert::{TryFrom, TryInto},
    env::var,
    fmt::Debug,
    fs::File,
    io::{BufRead, BufReader},
    path::{Path, PathBuf},
    result::Result as StdResult,
};
use syn::{
    parse::{Parse, ParseStream, Result},
    parse_macro_input, Ident, LitStr, Token,
};

//TODO(superwhiskers):
//   - refactor the source code to not be so repetitive
//   - give proper diagnostics and handle errors well (no `.unwrap()`)
//   - add documentation comments

/// A structure representing ISO country code entries
#[derive(Deserialize)]
#[serde(rename_all = "kebab-case")]
struct CountryEntry {
    name: String,
    alpha_2: String,
    alpha_3: String,
    country_code: String,
}

/// An enumeration over the supported ISO language code formats aas well as the name of the language
#[derive(PartialEq, Eq, Hash, Clone)]
enum LanguageTableEntryKey {
    Iso639_3,
    Iso639_2b,
    Iso639_2t,
    Iso639_1,
    Name,
}

impl LanguageTableEntryKey {
    fn as_standard_code(&self) -> Option<&'static str> {
        Some(match &self {
            Self::Iso639_3 => "639-3",
            Self::Iso639_2b => "639-2b",
            Self::Iso639_2t => "639-2t",
            Self::Iso639_1 => "639-1",
            _ => return None,
        })
    }
}

impl TryFrom<String> for LanguageTableEntryKey {
    type Error = &'static str;

    fn try_from(string: String) -> StdResult<Self, Self::Error> {
        Ok(match string.to_lowercase().as_ref() {
            "iso639_3" => Self::Iso639_3,
            "iso639_2b" => Self::Iso639_2b,
            "iso639_2t" => Self::Iso639_2t,
            "iso639_1" => Self::Iso639_1,
            "name" => Self::Name,
            _ => return Err("unable to find a matching variant"),
        })
    }
}

impl TryInto<&'static str> for LanguageTableEntryKey {
    type Error = &'static str;

    fn try_into(self) -> StdResult<&'static str, Self::Error> {
        Ok(match &self {
            Self::Iso639_3 => "Iso639_3",
            Self::Iso639_2b => "Iso639_2b",
            Self::Iso639_2t => "Iso639_2t",
            Self::Iso639_1 => "Iso639_1",
            _ => return Err("unable to find a matching string"),
        })
    }
}

#[derive(PartialEq, Eq, Hash, Clone)]
enum CountryIdentifierKey {
    Alpha2,
    Alpha3,
    Numeric,
    Name,
}

impl CountryIdentifierKey {
    fn as_standard_code(&self) -> Option<&'static str> {
        Some(match &self {
            Self::Alpha2 => "3166-1 alpha-2",
            Self::Alpha3 => "3166-1 alpha-3",
            Self::Numeric => "3166-1 numeric",
            _ => return None,
        })
    }
}

impl TryFrom<String> for CountryIdentifierKey {
    type Error = &'static str;

    fn try_from(string: String) -> StdResult<Self, Self::Error> {
        Ok(match string.to_lowercase().as_ref() {
            "iso3166_1_alpha_2" => Self::Alpha2,
            "iso3166_1_alpha_3" => Self::Alpha3,
            "iso3166_1_numeric" => Self::Numeric,
            "name" => Self::Name,
            _ => return Err("unable to find a matching variant"),
        })
    }
}

impl TryInto<&'static str> for CountryIdentifierKey {
    type Error = &'static str;

    fn try_into(self) -> StdResult<&'static str, Self::Error> {
        Ok(match &self {
            Self::Alpha2 => "Iso3166_1_alpha_2",
            Self::Alpha3 => "Iso3166_1_alpha_3",
            _ => return Err("unable to find a matching string"),
        })
    }
}

fn parse_country_codes(dataset: &Path) -> Option<Vec<CountryEntry>> {
    let country_reader = BufReader::new(match File::open(dataset) {
        Ok(file) => file,
        Err(e) => {
            Diagnostic::new(
                Level::Error,
                format!(
                    "Unable to load the country code dataset, {}",
                    dataset.as_os_str().to_string_lossy()
                ),
            )
            .note(format!("{}", e))
            .emit();
            return None;
        }
    });

    Some(match serde_json::from_reader(country_reader) {
        Ok(parsed) => parsed,
        Err(e) => {
            Diagnostic::new(
                Level::Error,
                format!(
                    "Unable to parse the country code dataset, {}",
                    dataset.as_os_str().to_string_lossy()
                ),
            )
            .note(format!("{}", e))
            .emit();
            return None;
        }
    })
}

fn parse_language_table(table: &Path) -> Option<Vec<HashMap<LanguageTableEntryKey, String>>> {
    let table_reader = BufReader::new(match File::open(table) {
        Ok(file) => file,
        Err(e) => {
            Diagnostic::new(
                Level::Error,
                format!(
                    "Unable to load the language table, {}",
                    table.as_os_str().to_string_lossy()
                ),
            )
            .note(format!("{}", e))
            .emit();
            return None;
        }
    });

    Some(
        table_reader
            .lines()
            .skip(1)
            .filter_map(|raw_line| {
                let line = match &raw_line {
                    Ok(s) => s,
                    Err(_) => return None,
                }
                .split('\t')
                .collect::<Vec<&str>>();

                let mut entry = HashMap::new();
                entry.insert(LanguageTableEntryKey::Iso639_3, line[0].to_string());
                if line[1].len() == 3 {
                    entry.insert(LanguageTableEntryKey::Iso639_2b, line[1].to_string());
                }
                if line[2].len() == 3 {
                    entry.insert(LanguageTableEntryKey::Iso639_2t, line[2].to_string());
                }
                if line[3].len() == 2 {
                    entry.insert(LanguageTableEntryKey::Iso639_1, line[3].to_string());
                }
                entry.insert(LanguageTableEntryKey::Name, line[6].to_string());

                Some(entry)
            })
            .collect(),
    )
}

fn parse_language_table_from_environment() -> Option<Vec<HashMap<LanguageTableEntryKey, String>>> {
    let mut language_table_path = PathBuf::from(var("CARGO_MANIFEST_DIR").unwrap());
    language_table_path.push("assets/language.tab");
    parse_language_table(&language_table_path)
}

fn parse_country_codes_from_environment() -> Option<Vec<CountryEntry>> {
    let mut country_codes_path = PathBuf::from(var("CARGO_MANIFEST_DIR").unwrap());
    country_codes_path.push("assets/country.json");
    parse_country_codes(&country_codes_path)
}

// note: the second parameter of each tuple is `true` if a string or integer is being worked with
struct GenerationInput<K>
where
    K: TryFrom<String>,
    K::Error: Debug,
{
    enumeration: Option<String>,
    match_against: Option<TokenStream2>,
    lhs: (K, bool),
    rhs: Option<(K, bool)>,
}

impl<K> Parse for GenerationInput<K>
where
    K: TryFrom<String>,
    K::Error: Debug,
{
    fn parse(input: ParseStream) -> Result<Self> {
        let keyword = input.lookahead1();
        let enumeration = if keyword.peek(Token![enum]) {
            input.parse::<Token![enum]>()?;
            let enumeration_name = input.parse::<Ident>()?.to_string();
            input.parse::<Token![:]>()?;
            Some(enumeration_name)
        } else {
            None
        };
        let match_against = if keyword.peek(Token![match]) {
            input.parse::<Token![match]>()?;
            let match_against = input.lookahead1();
            let match_against = if match_against.peek(Token![&]) {
                input.parse::<Token![&]>()?;
                input.parse::<Token![self]>()?;
                Some(quote! { &self })
            } else if match_against.peek(Ident) {
                Some(input.parse::<Ident>()?.to_token_stream())
            } else {
                None
            };
            input.parse::<Token![:]>()?;
            match_against
        } else {
            None
        };
        let lhs = input.lookahead1();
        let lhs = if lhs.peek(Ident) {
            (
                input.parse::<Ident>()?.to_string().try_into().unwrap(),
                false,
            )
        } else if lhs.peek(LitStr) {
            (input.parse::<LitStr>()?.value().try_into().unwrap(), true)
        } else {
            return Err(lhs.error());
        };
        let token = input.lookahead1();
        let rhs = if token.peek(Token![=>]) {
            input.parse::<Token![=>]>()?;
            let rhs = input.lookahead1();
            Some(if rhs.peek(Ident) {
                (
                    input.parse::<Ident>()?.to_string().try_into().unwrap(),
                    false,
                )
            } else if rhs.peek(LitStr) {
                (input.parse::<LitStr>()?.value().try_into().unwrap(), true)
            } else {
                return Err(rhs.error());
            })
        } else {
            None
        };

        Ok(GenerationInput {
            enumeration,
            match_against,
            lhs,
            rhs,
        })
    }
}

fn ascii_formatter(string: &mut str) {
    if let Some(start) = string.get_mut(0..1) {
        start.make_ascii_uppercase();
    }
    if let Some(remainder) = string.get_mut(1..) {
        remainder.make_ascii_lowercase();
    }
}

#[proc_macro]
pub fn country_identifiers_from_table(tokens: TokenStream) -> TokenStream {
    let country_codes = parse_country_codes_from_environment().unwrap();
    let GenerationInput {
        enumeration,
        match_against,
        lhs,
        rhs,
    } = parse_macro_input!(tokens as GenerationInput<CountryIdentifierKey>);

    let mut rows: Vec<proc_macro2::TokenStream> = Vec::new();
    for codes in country_codes {
        match (&lhs, &rhs) {
            ((lhs_key, true), None) => {
                let lhs = Literal::string(match &lhs_key {
                    CountryIdentifierKey::Alpha2 => &codes.alpha_2,
                    CountryIdentifierKey::Alpha3 => &codes.alpha_3,
                    CountryIdentifierKey::Numeric => {
                        panic!("numeric identifiers cannot be used alone")
                    }
                    CountryIdentifierKey::Name => panic!("names cannot be used alone"),
                });
                rows.push(quote! {
                    #lhs
                });
            }
            ((lhs_key, false), None) => {
                let mut lhs_string = match &lhs_key {
                    CountryIdentifierKey::Alpha2 => codes.alpha_2,
                    CountryIdentifierKey::Alpha3 => codes.alpha_3,
                    CountryIdentifierKey::Numeric => {
                        panic!("numeric identifiers cannot be used as an identifier")
                    }
                    CountryIdentifierKey::Name => panic!("names cannot be used as an identifier"),
                };
                ascii_formatter(&mut lhs_string);
                let lhs = Ident::new(&lhs_string, Span::call_site());
                rows.push(quote! {
                    #lhs
                });
            }
            ((lhs_key, true), Some((rhs_key, true))) => {
                let lhs = match &lhs_key {
                    CountryIdentifierKey::Alpha2 => Literal::string(&codes.alpha_2),
                    CountryIdentifierKey::Alpha3 => Literal::string(&codes.alpha_3),
                    CountryIdentifierKey::Numeric => Literal::u16_unsuffixed(codes.country_code.parse().unwrap()),
                    CountryIdentifierKey::Name => Literal::string(&codes.name),
                };
                let rhs = match &rhs_key {
                    CountryIdentifierKey::Alpha2 => Literal::string(&codes.alpha_2),
                    CountryIdentifierKey::Alpha3 => Literal::string(&codes.alpha_3),
                    CountryIdentifierKey::Numeric => Literal::u16_unsuffixed(codes.country_code.parse().unwrap()),
                    CountryIdentifierKey::Name => Literal::string(&codes.name),
                };
                rows.push(quote! {
                    #lhs => #rhs
                });
            }
            ((lhs_key, false), Some((rhs_key, true))) => {
                let mut lhs_string = match &lhs_key {
                    CountryIdentifierKey::Alpha2 => codes.alpha_2.clone(),
                    CountryIdentifierKey::Alpha3 => codes.alpha_3.clone(),
                    CountryIdentifierKey::Numeric => {
                        panic!("numeric identifiers cannot be used as an identifier")
                    }
                    CountryIdentifierKey::Name => panic!("names cannot be used as an identifier"),
                };
                ascii_formatter(&mut lhs_string);
                let lhs = Ident::new(&lhs_string, Span::call_site());
                let lhs_path = Ident::new(lhs_key.clone().try_into().unwrap(), Span::call_site());
                let rhs = match &rhs_key {
                    CountryIdentifierKey::Alpha2 => Literal::string(&codes.alpha_2),
                    CountryIdentifierKey::Alpha3 => Literal::string(&codes.alpha_3),
                    CountryIdentifierKey::Numeric => Literal::u16_unsuffixed(codes.country_code.parse().unwrap()),
                    CountryIdentifierKey::Name => Literal::string(&codes.name),
                };
                rows.push(quote! {
                    #lhs_path::#lhs => #rhs
                });
            }
            ((lhs_key, true), Some((rhs_key, false))) => {
                let lhs = match &lhs_key {
                    CountryIdentifierKey::Alpha2 => Literal::string(&codes.alpha_2),
                    CountryIdentifierKey::Alpha3 => Literal::string(&codes.alpha_3),
                    CountryIdentifierKey::Numeric => Literal::u16_unsuffixed(codes.country_code.parse().unwrap()),
                    CountryIdentifierKey::Name => Literal::string(&codes.name),
                };
                let mut rhs_string = match &rhs_key {
                    CountryIdentifierKey::Alpha2 => codes.alpha_2.clone(),
                    CountryIdentifierKey::Alpha3 => codes.alpha_3.clone(),
                    CountryIdentifierKey::Numeric => {
                        panic!("numeric identifiers cannot be used as an identifier")
                    }
                    CountryIdentifierKey::Name => panic!("names cannot be used as an identifier"),
                };
                ascii_formatter(&mut rhs_string);
                let rhs = Ident::new(&rhs_string, Span::call_site());
                let rhs_path = Ident::new(rhs_key.clone().try_into().unwrap(), Span::call_site());
                rows.push(quote! {
                    #lhs => Some(#rhs_path::#rhs)
                });
            }
            ((lhs_key, false), Some((rhs_key, false))) => {
                let mut lhs_string = match &lhs_key {
                    CountryIdentifierKey::Alpha2 => codes.alpha_2.clone(),
                    CountryIdentifierKey::Alpha3 => codes.alpha_3.clone(),
                    CountryIdentifierKey::Numeric => {
                        panic!("numeric identifiers cannot be used as an identifier")
                    }
                    CountryIdentifierKey::Name => panic!("names cannot be used as an identifier"),
                };
                ascii_formatter(&mut lhs_string);
                let lhs = Ident::new(&lhs_string, Span::call_site());
                let lhs_path = Ident::new(lhs_key.clone().try_into().unwrap(), Span::call_site());
                let mut rhs_string = match &rhs_key {
                    CountryIdentifierKey::Alpha2 => codes.alpha_2.clone(),
                    CountryIdentifierKey::Alpha3 => codes.alpha_3.clone(),
                    CountryIdentifierKey::Numeric => {
                        panic!("numeric identifiers cannot be used as an identifier")
                    }
                    CountryIdentifierKey::Name => panic!("names cannot be used as an identifier"),
                };
                ascii_formatter(&mut rhs_string);
                let rhs = Ident::new(&rhs_string, Span::call_site());
                let rhs_path = Ident::new(rhs_key.clone().try_into().unwrap(), Span::call_site());

                // we don't need optionals here because there's always an alpha3 and numeric code for every alpha2 and friends
                rows.push(quote! {
                    #lhs_path::#lhs => #rhs_path::#rhs
                });
            }
        }
    }

    return TokenStream::from(if let Some(enumeration_name) = enumeration {
        let enumeration_name = Ident::new(&enumeration_name, Span::call_site());
        let iso_code = lhs.0.as_standard_code();
        if let Some(iso_code) = iso_code {
            quote! {
                /// Enumeration over all possible ISO
                #[doc = #iso_code]
                /// country codes
                #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
                #[cfg_attr(feature = "serde", serde(rename_all = "UPPERCASE"))]
                #[derive(Debug, Hash, Copy, Clone, Eq, PartialEq)]
                pub enum #enumeration_name {
                    #(#rows),*
                }
            }
        } else {
            quote! {
                compile_error!("the selected key to generate an enumeration from does not have a corresponding iso standard")
            }
        }
    } else if let Some(match_against) = match_against {
        if lhs.1 {
            quote! {
                match #match_against {
                    #(#rows),*,
                    _ => None,
                }
            }
        } else {
            quote! {
                match #match_against {
                    #(#rows),*
                }
            }
        }
    } else {
        quote! {
            compile_error!("not enough information was provided");
        }
    });
}

#[proc_macro]
pub fn language_identifiers_from_table(tokens: TokenStream) -> TokenStream {
    let table = parse_language_table_from_environment().unwrap();
    let GenerationInput {
        enumeration,
        match_against,
        lhs,
        rhs,
    } = parse_macro_input!(tokens as GenerationInput<LanguageTableEntryKey>);

    let mut rows: Vec<proc_macro2::TokenStream> = Vec::new();
    for table_entry in table {
        if table_entry.get(&lhs.0).is_none() {
            continue;
        }
        match (&lhs, &rhs) {
            ((lhs_table, true), None) => {
                let lhs = Literal::string(&table_entry[lhs_table]);
                rows.push(quote! {
                    #lhs
                });
            }
            ((lhs_table, false), None) => {
                let mut lhs_string = table_entry[lhs_table].clone();
                ascii_formatter(&mut lhs_string);
                let lhs: Ident = Ident::new(&lhs_string, Span::call_site());
                rows.push(quote! {
                    #lhs
                });
            }
            ((lhs_table, true), Some((rhs_table, true))) => {
                let lhs = Literal::string(&table_entry[lhs_table]);
                let rhs = Literal::string(&table_entry[rhs_table]);
                rows.push(quote! {
                    #lhs => #rhs
                });
            }
            ((lhs_table, false), Some((rhs_table, true))) => {
                let mut lhs_string = table_entry[lhs_table].clone();
                ascii_formatter(&mut lhs_string);
                let lhs = Ident::new(&lhs_string, Span::call_site());

                // while this technically isn't safe, trying to generate a literal for a name is impossible
                let lhs_path = Ident::new(lhs_table.clone().try_into().unwrap(), Span::call_site());

                let rhs = Literal::string(&table_entry[rhs_table]);
                rows.push(quote! {
                    #lhs_path::#lhs => #rhs
                })
            }
            ((lhs_table, true), Some((rhs_table, false))) => {
                let lhs = Literal::string(&table_entry[lhs_table]);
                if let Some(rhs) = table_entry.get(rhs_table) {
                    let mut rhs_string = rhs.clone();
                    ascii_formatter(&mut rhs_string);
                    let rhs = Ident::new(&rhs_string, Span::call_site());

                    // while this technically isn't safe, trying to generate a literal for a name is impossible
                    let rhs_path =
                        Ident::new(rhs_table.clone().try_into().unwrap(), Span::call_site());
                    rows.push(quote! {
                        #lhs => Some(#rhs_path::#rhs)
                    })
                } else {
                    rows.push(quote! {
                        #lhs => None
                    })
                }
            }
            ((lhs_table, false), Some((rhs_table, false))) => {
                let mut lhs_string = table_entry[lhs_table].clone();
                ascii_formatter(&mut lhs_string);
                let lhs = Ident::new(&lhs_string, Span::call_site());

                // while this technically isn't safe, trying to generate a literal for a name is impossible
                let lhs_path = Ident::new(lhs_table.clone().try_into().unwrap(), Span::call_site());
                if let Some(rhs) = table_entry.get(rhs_table) {
                    let mut rhs_string = rhs.clone();
                    ascii_formatter(&mut rhs_string);
                    let rhs = Ident::new(&rhs_string, Span::call_site());

                    // while this technically isn't safe, trying to generate a literal for a name is impossible
                    let rhs_path =
                        Ident::new(rhs_table.clone().try_into().unwrap(), Span::call_site());
                    rows.push(quote! {
                        #lhs_path::#lhs => Some(#rhs_path::#rhs)
                    })
                } else {
                    rows.push(quote! {
                        #lhs_path::#lhs => None
                    })
                }
            }
        }
    }

    return TokenStream::from(if let Some(enumeration_name) = enumeration {
        let enumeration_name = Ident::new(&enumeration_name, Span::call_site());
        let iso_code = lhs.0.as_standard_code();
        if let Some(iso_code) = iso_code {
            quote! {
                /// Enumeration over all possible ISO
                #[doc = #iso_code]
                /// language codes
                #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
                #[cfg_attr(feature = "serde", serde(rename_all = "lowercase"))]
                #[derive(Debug, Hash, Copy, Clone, Eq, PartialEq)]
                pub enum #enumeration_name {
                    #(#rows),*
                }
            }
        } else {
            quote! {
                compile_error!("the selected table column to generate an enumeration from does not have a corresponding iso standard")
            }
        }
    } else if let Some(match_against) = match_against {
        if lhs.1 {
            quote! {
                match #match_against {
                    #(#rows),*,
                    _ => None,
                }
            }
        } else {
            quote! {
                match #match_against {
                    #(#rows),*
                }
            }
        }
    } else {
        quote! {
            compile_error!("not enough information was provided");
        }
    });
}