academic_journals/
lib.rs

1mod journal;
2
3use crate::journal::Journal;
4use lazy_static::lazy_static;
5use std::collections::HashMap;
6use std::sync::Arc;
7
8/// Static binary data for journal records, embedded at compile time.
9static JOURNAL_DATA: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/generated_journals.bin"));
10
11lazy_static! {
12        /// A vector of `Arc<Journal>` objects, containing shared references to deserialized journals.
13        ///
14        /// # Panics
15        /// Panics if deserialization of journals fails.
16    static ref JOURNALS: Vec<Arc<Journal>> = {
17        let journals: Vec<Journal> =
18            bincode::deserialize(JOURNAL_DATA).expect("Failed to deserialize journals");
19        journals.into_iter().map(Arc::new).collect()
20    };
21        /// A hash map mapping journal full names to their `Arc<Journal>` objects.
22    static ref FULL_NAME_TO_RECORD: HashMap<String, Arc<Journal>> = {
23        JOURNALS
24            .iter()
25            .map(|journal| (journal.name.clone(), Arc::clone(journal)))
26            .collect()
27    };
28        /// A hash map mapping journal abbreviations to their full names.
29    static ref ABBREVIATION_TO_FULL_NAME: HashMap<String, String> = {
30        JOURNALS
31            .iter()
32            .flat_map(|journal| {
33                let name = &journal.name;
34                [
35                    journal.abbr_1.as_deref(),
36                    journal.abbr_2.as_deref(),
37                    journal.abbr_3.as_deref(),
38                ]
39                .into_iter()
40                .flatten()
41                .map(move |abbr| (abbr.to_string(), name.clone()))
42            })
43            .collect()
44    };
45}
46
47/// Retrieves the first available abbreviation for a given journal full name.
48///
49/// # Arguments
50/// * `full_name` - The full name of the journal.
51///
52/// # Returns
53/// Returns `Some(String)` containing the abbreviation if found, otherwise `None`.
54///
55/// # Examples
56/// ```
57/// let abbreviation = academic_journals::get_abbreviation("Journal of Rust Studies").unwrap();
58/// println!("Abbreviation: {}", abbreviation);
59/// ```
60pub fn get_abbreviation(full_name: &str) -> Option<String> {
61    FULL_NAME_TO_RECORD.get(full_name).and_then(|journal| {
62        journal
63            .abbr_1
64            .as_ref()
65            .or(journal.abbr_2.as_ref())
66            .or(journal.abbr_3.as_ref())
67            .cloned()
68    })
69}
70
71/// Retrieves the full name for a given journal abbreviation.
72///
73/// # Arguments
74/// * `abbreviation` - The abbreviation of the journal.
75///
76/// # Returns
77/// Returns `Some(String)` containing the full name if found, otherwise `None`.
78///
79/// # Examples
80/// ```
81/// let full_name = academic_journals::get_full_name("JRS").unwrap();
82/// println!("Full name: {}", full_name);
83/// ```
84pub fn get_full_name(abbreviation: &str) -> Option<String> {
85    ABBREVIATION_TO_FULL_NAME.get(abbreviation).cloned()
86}