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
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
//! Symbols enumerating rows and columns of character tables.

use std::cmp::Ordering;
use std::collections::{HashMap, VecDeque};
use std::error::Error;
use std::fmt;
use std::hash::{Hash, Hasher};
use std::str::FromStr;

use counter::Counter;
use derive_builder::Builder;
use indexmap::IndexMap;
use itertools::Itertools;
use phf::phf_map;
use regex::Regex;
use serde::{Deserialize, Serialize};

/// Symbols for Frobenius--Schur classifications of irreducible representations.
pub static FROBENIUS_SCHUR_SYMBOLS: phf::Map<i8, &'static str> = phf_map! {
    1i8 => "r",
    0i8 => "c",
    -1i8 => "q",
};

// =================
// Trait definitions
// =================

// ------------------
// MathematicalSymbol
// ------------------

/// Trait for general mathematical symbols. See [`GenericSymbol`] for the definitions of the
/// parts.
pub trait MathematicalSymbol: Clone + Hash + Eq + fmt::Display {
    /// The main part of the symbol.
    fn main(&self) -> String;

    /// The pre-superscript part of the symbol.
    fn presuper(&self) -> String;

    /// The pre-subscript part of the symbol.
    fn presub(&self) -> String;

    /// The post-superscript part of the symbol.
    fn postsuper(&self) -> String;

    /// The post-subscript part of the symbol.
    fn postsub(&self) -> String;

    /// The prefactor part of the symbol.
    fn prefactor(&self) -> String;

    /// The postfactor part of the symbol.
    fn postfactor(&self) -> String;

    /// The multiplicity of the symbol which can have different meanings depending on the exact
    /// nature of the mathematical symbol.
    fn multiplicity(&self) -> Option<usize>;
}

// -----------------
// LinearSpaceSymbol
// -----------------

/// Trait for symbols describing linear spaces.
pub trait LinearSpaceSymbol: MathematicalSymbol + FromStr {
    /// The dimensionality of the linear space.
    fn dimensionality(&self) -> usize;

    /// Sets the dimensionality of the linear space for the symbol.
    ///
    /// # Arguments
    ///
    /// * `dim` - The dimensionality to be set.
    ///
    /// # Returns
    ///
    /// Returns `true` if the dimensionality has been successfully set.
    fn set_dimensionality(&mut self, dim: usize) -> bool;
}

// --------------------------
// ReducibleLinearSpaceSymbol
// --------------------------

// ~~~~~~~~~~~~~~~~
// Trait definition
// ~~~~~~~~~~~~~~~~

/// Trait for symbols describing reducible linear spaces.
pub trait ReducibleLinearSpaceSymbol: LinearSpaceSymbol
where
    Self::Subspace: LinearSpaceSymbol + PartialOrd,
{
    /// The type of the subspace symbols.
    type Subspace;

    /// Constructs [`Self`] from constituting subspace symbols and their multiplicities.
    fn from_subspaces(subspaces: &[(Self::Subspace, usize)]) -> Self;

    /// Returns the constituting subspace symbols and their multiplicities.
    fn subspaces(&self) -> Vec<(&Self::Subspace, &usize)>;

    // ----------------
    // Provided methods
    // ----------------

    /// Returns an iterator containing sorted references to the constituting symbols.
    ///
    /// # Panics
    ///
    /// Panics if the constituting symbols cannot be ordered.
    #[must_use]
    fn sorted_subspaces(&self) -> Vec<(&Self::Subspace, &usize)> {
        self.subspaces()
            .iter()
            .sorted_by(|(a, _), (b, _)| {
                a.partial_cmp(b)
                    .unwrap_or_else(|| panic!("`{a}` and `{b}` cannot be compared."))
            })
            .cloned()
            .collect::<Vec<_>>()
    }
}

// ~~~~~~~~~~~~~~~~~~~~~~
// Blanket implementation
// ~~~~~~~~~~~~~~~~~~~~~~

impl<R> MathematicalSymbol for R
where
    R: ReducibleLinearSpaceSymbol,
{
    /// The main part of the symbol.
    fn main(&self) -> String {
        self.subspaces()
            .iter()
            .map(|(irrep, &mult)| {
                format!(
                    "{}{irrep}",
                    if mult != 1 {
                        mult.to_string()
                    } else {
                        String::new()
                    }
                )
            })
            .join(" ⊕ ")
    }

    /// The pre-superscript part of the symbol, which is always empty.
    fn presuper(&self) -> String {
        String::new()
    }

    fn presub(&self) -> String {
        String::new()
    }

    /// The post-superscript part of the symbol, which is always empty.
    fn postsuper(&self) -> String {
        String::new()
    }

    /// The post-subscript part of the symbol, which is always empty.
    fn postsub(&self) -> String {
        String::new()
    }

    /// The prefactor part of the symbol, which is always `"1"`.
    fn prefactor(&self) -> String {
        "1".to_string()
    }

    /// The postfactor part of the symbol, which is always empty.
    fn postfactor(&self) -> String {
        String::new()
    }

    /// The multiplicity of the symbol, which is always `"1"`.
    fn multiplicity(&self) -> Option<usize> {
        Some(1)
    }
}

impl<R> LinearSpaceSymbol for R
where
    R: ReducibleLinearSpaceSymbol,
{
    fn dimensionality(&self) -> usize {
        self.subspaces()
            .iter()
            .map(|(symbol, &mult)| symbol.dimensionality() * mult)
            .sum()
    }

    fn set_dimensionality(&mut self, _: usize) -> bool {
        log::error!("The dimensionality of `{self}` cannot be set.");
        false
    }
}

// ----------------
// CollectionSymbol
// ----------------

/// Trait for symbols describing collections of objects such as conjugacy classes.
pub trait CollectionSymbol: MathematicalSymbol {
    /// The type of the elements in the collection.
    type CollectionElement;

    /// Constructs a collection symbol from a string and one or more representative collection
    /// elements.
    ///
    /// # Arguments
    ///
    /// * `symstr` - A string to be parsed. See [`GenericSymbol::from_str`] for more information.
    /// * `reps` - An optional vector of one or more representative collection elements.
    ///
    /// # Errors
    ///
    /// Returns an error if `symstr` cannot be parsed.
    fn from_reps(
        symstr: &str,
        reps: Option<Vec<Self::CollectionElement>>,
    ) -> Result<Self, GenericSymbolParsingError>;

    /// The first representative element of the collection.
    fn representative(&self) -> Option<&Self::CollectionElement>;

    /// All representative elements of the collection.
    fn representatives(&self) -> Option<&Vec<Self::CollectionElement>>;

    /// The size of the collection which is given by the number of representative elements
    /// multiplied by the multiplicity of the symbol. If no representative elements exist, then the
    /// size is taken to be the multiplicity of the symbol itself.
    fn size(&self) -> usize {
        self.multiplicity().unwrap_or_else(|| {
            panic!(
                "Unable to deduce the multiplicity of the class from the prefactor {}.",
                self.prefactor()
            )
        }) * self
            .representatives()
            .map(|reps| reps.len())
            .expect("No representatives found.")
    }
}

// =======
// Structs
// =======

// -------------
// GenericSymbol
// -------------

// ~~~~~~~~~~~~~~~~~
// Struct definition
// ~~~~~~~~~~~~~~~~~

/// Structure to handle generic mathematical symbols.
///
/// Each generic symbol has the format
///
/// ```math
/// \textrm{prefactor}
/// \ ^{\textrm{presuper}}_{\textrm{presub}}
/// \ \textrm{main}
/// \ ^{\textrm{postsuper}}_{\textrm{postsub}}
/// \ \textrm{postfactor}.
/// ```
#[derive(Builder, Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Serialize, Deserialize)]
pub struct GenericSymbol {
    /// The main part of the symbol.
    main: String,

    /// The pre-superscript part of the symbol.
    #[builder(default = "String::new()")]
    presuper: String,

    /// The pre-subscript part of the symbol.
    #[builder(default = "String::new()")]
    presub: String,

    /// The post-superscript part of the symbol.
    #[builder(default = "String::new()")]
    postsuper: String,

    /// The post-subscript part of the symbol.
    #[builder(default = "String::new()")]
    postsub: String,

    /// The prefactor part of the symbol.
    #[builder(default = "String::new()")]
    prefactor: String,

    /// The postfactor part of the symbol.
    #[builder(default = "String::new()")]
    postfactor: String,
}

impl GenericSymbol {
    fn builder() -> GenericSymbolBuilder {
        GenericSymbolBuilder::default()
    }

    /// Sets the main part of the symbol.
    pub(crate) fn set_main(&mut self, main: &str) {
        self.main = main.to_string();
    }

    /// Sets the pre-subscript part of the symbol.
    pub(crate) fn set_presub(&mut self, presub: &str) {
        self.presub = presub.to_string();
    }

    /// Sets the post-subscript part of the symbol.
    pub(crate) fn set_postsub(&mut self, postsub: &str) {
        self.postsub = postsub.to_string();
    }
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Trait implementation for GenericSymbol
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

impl MathematicalSymbol for GenericSymbol {
    fn main(&self) -> String {
        self.main.clone()
    }

    fn presuper(&self) -> String {
        self.presuper.clone()
    }

    fn presub(&self) -> String {
        self.presub.clone()
    }

    fn postsuper(&self) -> String {
        self.postsuper.clone()
    }

    fn postsub(&self) -> String {
        self.postsub.clone()
    }

    fn prefactor(&self) -> String {
        self.prefactor.clone()
    }

    fn postfactor(&self) -> String {
        self.postfactor.clone()
    }

    fn multiplicity(&self) -> Option<usize> {
        str::parse::<usize>(&self.prefactor).ok()
    }
}

impl FromStr for GenericSymbol {
    type Err = GenericSymbolParsingError;

    /// Parses a string representing a generic symbol.
    ///
    /// Some permissible generic symbols:
    ///
    /// ```text
    /// "T"
    /// "||T|_(2g)|"
    /// "|^(3)|T|_(2g)|"
    /// "12||C|^(2)_(5)|"
    /// "2||S|^(z)|(α)"
    /// ```
    fn from_str(symstr: &str) -> Result<Self, Self::Err> {
        let strs: Vec<&str> = symstr.split('|').collect();
        if strs.len() == 1 {
            Ok(Self::builder()
                .main(strs[0].to_string())
                .build()
                .unwrap_or_else(|_| {
                    panic!("Unable to construct a generic symbol from `{symstr}`.")
                }))
        } else if strs.len() == 5 {
            let prefacstr = strs[0];
            let prestr = strs[1];
            let mainstr = strs[2];
            let poststr = strs[3];
            let postfacstr = strs[4];

            let presuper_re = Regex::new(r"\^\((.*?)\)").expect("Regex pattern invalid.");
            let presuperstr = if let Some(cap) = presuper_re.captures(prestr) {
                cap.get(1)
                    .expect("Expected regex group cannot be captured.")
                    .as_str()
            } else {
                ""
            };

            let presub_re = Regex::new(r"_\((.*?)\)").expect("Regex pattern invalid.");
            let presubstr = if let Some(cap) = presub_re.captures(prestr) {
                cap.get(1)
                    .expect("Expected regex group cannot be captured.")
                    .as_str()
            } else {
                ""
            };

            let postsuper_re = Regex::new(r"\^\((.*?)\)").expect("Regex pattern invalid.");
            let postsuperstr = if let Some(cap) = postsuper_re.captures(poststr) {
                cap.get(1)
                    .expect("Expected regex group cannot be captured.")
                    .as_str()
            } else {
                ""
            };

            let postsub_re = Regex::new(r"_\((.*?)\)").expect("Regex pattern invalid.");
            let postsubstr = if let Some(cap) = postsub_re.captures(poststr) {
                cap.get(1)
                    .expect("Expected regex group cannot be captured.")
                    .as_str()
            } else {
                ""
            };

            Ok(Self::builder()
                .main(mainstr.to_string())
                .presuper(presuperstr.to_string())
                .presub(presubstr.to_string())
                .postsuper(postsuperstr.to_string())
                .postsub(postsubstr.to_string())
                .prefactor(prefacstr.to_string())
                .postfactor(postfacstr.to_string())
                .build()
                .unwrap_or_else(|_| {
                    panic!("Unable to construct a generic symbol from `{symstr}`.")
                }))
        } else {
            Err(GenericSymbolParsingError(format!(
                "`{symstr}` is not parsable."
            )))
        }
    }
}

impl fmt::Display for GenericSymbol {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let prefac_str = if self.prefactor() == "1" {
            String::new()
        } else {
            self.prefactor()
        };
        let presuper_str = if self.presuper().is_empty() {
            String::new()
        } else {
            format!("^({})", self.presuper())
        };
        let presub_str = if self.presub().is_empty() {
            String::new()
        } else {
            format!("_({})", self.presub())
        };
        let main_str = format!("|{}|", self.main());
        let postsuper_str = if self.postsuper().is_empty() {
            String::new()
        } else {
            format!("^({})", self.postsuper())
        };
        let postsub_str = if self.postsub().is_empty() {
            String::new()
        } else {
            format!("_({})", self.postsub())
        };
        let postfac_str = if self.postfactor() == "1" {
            String::new()
        } else {
            self.postfactor()
        };
        write!(
            f,
            "{prefac_str}{presuper_str}{presub_str}{main_str}{postsuper_str}{postsub_str}{postfac_str}",
        )
    }
}

#[derive(Debug, Clone)]
pub struct GenericSymbolParsingError(pub String);

impl fmt::Display for GenericSymbolParsingError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Generic symbol parsing error: {}.", self.0)
    }
}

impl Error for GenericSymbolParsingError {}

// ----------------
// DecomposedSymbol
// ----------------

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Trait implementation for DecomposedSymbol
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

/// Structure to handle symbols consisting of multiple sub-symbols.
#[derive(Builder, Debug, Clone, Eq, Serialize, Deserialize)]
pub struct DecomposedSymbol<S>
where
    S: LinearSpaceSymbol + PartialOrd,
{
    symbols: IndexMap<S, usize>,
}

impl<S> DecomposedSymbol<S>
where
    S: LinearSpaceSymbol + PartialOrd,
{
    fn builder() -> DecomposedSymbolBuilder<S> {
        DecomposedSymbolBuilder::<S>::default()
    }

    /// Parses a string representing a decomposed symbol. See [`Self::from_str`] for more
    /// information.
    ///
    /// # Arguments
    ///
    /// * `symstr` - A string to be parsed to give a decomposed symbol.
    ///
    /// # Errors
    ///
    /// Errors when the string cannot be parsed as a decomposed symbol.
    pub fn new(symstr: &str) -> Result<Self, DecomposedSymbolBuilderError> {
        Self::from_str(symstr)
    }
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Trait implementation for DecomposedSymbol
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

impl<S> ReducibleLinearSpaceSymbol for DecomposedSymbol<S>
where
    S: LinearSpaceSymbol + PartialOrd,
{
    type Subspace = S;

    fn from_subspaces(irreps: &[(Self::Subspace, usize)]) -> Self {
        Self::builder()
            .symbols(
                irreps
                    .iter()
                    .filter(|(_, mult)| *mult != 0)
                    .cloned()
                    .collect::<IndexMap<_, _>>(),
            )
            .build()
            .expect("Unable to construct a decomposed symbol from a slice of symbols.")
    }

    fn subspaces(&self) -> Vec<(&Self::Subspace, &usize)> {
        self.symbols.iter().collect::<Vec<_>>()
    }
}

impl<S> FromStr for DecomposedSymbol<S>
where
    S: LinearSpaceSymbol + PartialOrd + FromStr,
{
    type Err = DecomposedSymbolBuilderError;

    /// Parses a string representing a decomposed symbol. A valid string representing a
    /// decomposed symbol is one consisting of one or more valid symbol strings, separated by a `+`
    /// character.
    ///
    /// # Arguments
    ///
    /// * `symstr` - A string to be parsed to give a decomposed symbol.
    ///
    /// # Returns
    ///
    /// A [`Result`] wrapping the constructed decomposed symbol.
    ///
    /// # Panics
    ///
    /// Panics when unable to construct a decomposed symbol from the specified string.
    ///
    /// # Errors
    ///
    /// Errors when the string cannot be parsed.
    fn from_str(symstr: &str) -> Result<Self, Self::Err> {
        let re = Regex::new(r"(\d?)(.*)").expect("Regex pattern invalid.");
        let symbols = symstr
            .split('⊕')
            .map(|irrep_str| {
                let cap = re
                    .captures(irrep_str.trim())
                    .unwrap_or_else(|| panic!("{irrep_str} does not fit the expected pattern."));
                let mult_str = cap
                    .get(1)
                    .expect("Unable to parse the multiplicity of the irrep.")
                    .as_str();
                let mult = if mult_str.is_empty() {
                    1
                } else {
                    str::parse::<usize>(mult_str)
                        .unwrap_or_else(|_| panic!("`{mult_str}` is not a positive integer."))
                };
                let irrep = cap.get(2).expect("Unable to parse the irrep.").as_str();
                (
                    S::from_str(irrep)
                        .unwrap_or_else(|_| panic!("Unable to parse {irrep} as a valid symbol.")),
                    mult,
                )
            })
            .collect::<IndexMap<_, _>>();
        Self::builder().symbols(symbols).build()
    }
}

impl<S> Hash for DecomposedSymbol<S>
where
    S: LinearSpaceSymbol + PartialOrd,
{
    fn hash<H: Hasher>(&self, state: &mut H) {
        for symbol in self.sorted_subspaces() {
            symbol.hash(state);
        }
    }
}

impl<S> PartialOrd for DecomposedSymbol<S>
where
    S: LinearSpaceSymbol + PartialOrd,
{
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        let self_subspaces = self.sorted_subspaces();
        let other_subspaces = other.sorted_subspaces();
        self_subspaces.partial_cmp(&other_subspaces)
    }
}

impl<S> fmt::Display for DecomposedSymbol<S>
where
    S: LinearSpaceSymbol + PartialOrd,
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.main())
    }
}

impl<S> PartialEq for DecomposedSymbol<S>
where
    S: LinearSpaceSymbol + PartialOrd,
{
    fn eq(&self, other: &Self) -> bool {
        let self_subspaces = self.sorted_subspaces();
        let other_subspaces = other.sorted_subspaces();
        self_subspaces == other_subspaces
    }
}

// ================
// Helper functions
// ================

/// Disambiguates linear-space labelling symbols that cannot be otherwise distinguished from rules.
///
/// This essentially appends appropriate roman subscripts to otherwise identical symbols.
///
/// # Arguments
///
/// * `raw_symbols` - An iterator of raw symbols, some of which might be identical.
///
/// # Returns
///
/// A vector of disambiguated symbols.
pub(crate) fn disambiguate_linspace_symbols<S>(
    raw_symbols: impl Iterator<Item = S> + Clone,
) -> Vec<S>
where
    S: LinearSpaceSymbol,
{
    let raw_symbol_count = raw_symbols.clone().collect::<Counter<S>>();
    let mut raw_symbols_to_full_symbols: HashMap<S, VecDeque<S>> = raw_symbol_count
        .iter()
        .map(|(raw_symbol, &duplicate_count)| {
            if duplicate_count == 1 {
                let mut symbols: VecDeque<S> = VecDeque::new();
                symbols.push_back(raw_symbol.clone());
                (raw_symbol.clone(), symbols)
            } else {
                let symbols: VecDeque<S> = (0..duplicate_count)
                    .map(|i| {
                        let mut new_symbol = S::from_str(&format!(
                            "|^({})_({})|{}|^({})_({}{})|",
                            raw_symbol.presuper(),
                            raw_symbol.presub(),
                            raw_symbol.main(),
                            raw_symbol.postsuper(),
                            i + 1,
                            raw_symbol.postsub(),
                        ))
                        .unwrap_or_else(|_| {
                            panic!(
                                "Unable to construct symmetry symbol `|^({})|{}|^({})_({}{})|`.",
                                raw_symbol.presuper(),
                                raw_symbol.main(),
                                raw_symbol.postsuper(),
                                i + 1,
                                raw_symbol.postsub(),
                            )
                        });
                        new_symbol.set_dimensionality(raw_symbol.dimensionality());
                        new_symbol
                    })
                    .collect();
                (raw_symbol.clone(), symbols)
            }
        })
        .collect();

    let symbols: Vec<S> = raw_symbols
        .map(|raw_symbol| {
            raw_symbols_to_full_symbols
                .get_mut(&raw_symbol)
                .unwrap_or_else(|| {
                    panic!(
                        "Unknown conversion of raw symbol `{}` to full symbol.",
                        &raw_symbol
                    )
                })
                .pop_front()
                .unwrap_or_else(|| {
                    panic!(
                        "No conversion to full symbol possible for `{}`",
                        &raw_symbol
                    )
                })
        })
        .collect();

    symbols
}