lib_ria/database/
catalog.rs

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
//! A RefractiveIndex.INFO catalog.
//!
//! A catalog is a hierarchical structure that contains information about the
//! location of material data within the database. Materials are identified by a
//! shelf, book, and page key.
use serde::{Deserialize, Serialize};

pub type Catalog = Vec<Shelf>;

#[derive(Serialize, Deserialize, Debug)]
#[serde(untagged)]
pub enum BookContent {
    Divider {
        #[serde(rename = "DIVIDER")]
        divider: String,
    },
    Page {
        #[serde(rename = "PAGE")]
        page: String,
        name: String,
        data: std::path::PathBuf,
        info: Option<std::path::PathBuf>,
    },

    // Special case for Hikari i-line glasses which have numbers as names
    PageNumberName {
        #[serde(rename = "PAGE")]
        page: u64,
        name: String,
        data: std::path::PathBuf,
        info: Option<std::path::PathBuf>,
    },
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(untagged)]
pub enum ShelfContent {
    Divider {
        #[serde(rename = "DIVIDER")]
        divider: String,
    },
    Book {
        #[serde(rename = "BOOK")]
        book: String,
        name: String,
        info: Option<std::path::PathBuf>,
        content: Vec<BookContent>,
    },
}

#[derive(Serialize, Deserialize, Debug)]
pub struct Shelf {
    #[serde(rename = "SHELF")]
    pub shelf: String,
    pub name: String,
    pub info: Option<std::path::PathBuf>,
    pub content: Vec<ShelfContent>,
}

#[cfg(test)]
mod tests {
    use super::*;

    fn yaml() -> &'static str {
        "
        - SHELF: main
          name: \"MAIN - simple inorganic materials\"
          content:
            - DIVIDER: \"Ag - Silver\"
            - BOOK: Ag
              name: \"Ag (Silver)\"
              info: \"main/Ag.html\"
              content:
                - DIVIDER: \"Experimental data: bulk, thick film\"
                - PAGE: Johnson
                  name: \"Johnson and Christy 1972: n,k 0.188–1.94 µm\"
                  data: \"main/Ag/Johnson.yml\"
                - PAGE: Choi
                  name: \"Choi et al. 2020: n,k 1.23–6.99 µm\"
                  data: \"main/Ag/Choi.yml\"
        "
    }

    #[test]
    fn test_deserialize_catalog() {
        let _: Catalog = serde_yaml::from_str(yaml()).unwrap();
    }
}