• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pulibrary / bibdata / c41dd2ab-dc4c-4404-9348-696a969af050

18 Aug 2025 01:20PM UTC coverage: 92.477% (-0.08%) from 92.556%
c41dd2ab-dc4c-4404-9348-696a969af050

push

circleci

christinach
Support format id structure
as a string and as an object

related to: https://github.com/pulibrary/bibdata/issues/2828#issuecomment-3192808622

38 of 41 new or added lines in 2 files covered. (92.68%)

6 existing lines in 1 file now uncovered.

7019 of 7590 relevant lines covered (92.48%)

425.22 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.08
/lib/bibdata_rs/src/ephemera/ephemera_folder/format.rs
1
use crate::solr;
2
use serde::Deserialize;
3

4
use serde::de::Deserializer;
5
use serde::Serialize;
6

7
#[derive(Copy, Clone, Debug, Serialize, PartialEq)]
8
pub struct Format {
9
    pub pref_label: Option<solr::FormatFacet>,
10
}
11

12
impl<'de> Deserialize<'de> for Format {
13
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
41✔
14
    where
41✔
15
        D: Deserializer<'de>,
41✔
16
    {
17
        #[derive(Deserialize)]
18
        struct FormatNestedId {
19
            #[serde(rename = "exact_match")]
20
            exact_match: Option<serde_json::Value>,
21
        }
22

23
        let raw = FormatNestedId::deserialize(deserializer)?;
41✔
24
        let mut facet = None;
41✔
25
        if let Some(exact_match) = raw.exact_match {
41✔
26
            if let Some(id_val) = exact_match.get("@id") {
41✔
27
                if id_val.is_string() {
41✔
28
                    let _id = id_val.as_str().unwrap();
1✔
29
                    facet = Some(solr::FormatFacet::Book);
1✔
30
                } else if id_val.is_object() {
40✔
31
                    if let Some(inner_id) = id_val.get("@id") {
40✔
32
                        let _id = inner_id.as_str().unwrap();
40✔
33

40✔
34
                        facet = Some(solr::FormatFacet::Book);
40✔
35
                    }
40✔
NEW
36
                }
×
NEW
37
            }
×
NEW
38
        }
×
39
        Ok(Format { pref_label: facet })
41✔
40
    }
41✔
41
}
42

43
#[cfg(test)]
44
mod tests {
45
    use super::*;
46

47
    #[test]
48
    fn it_can_parse_format_from_the_json_ld() {
1✔
49
        let json_ld = r#"[
1✔
50
            {
1✔
51
              "@id": "https://figgy-staging.princeton.edu/catalog/5dbeae15-f46a-4411-a385-ce6df02231dc",
1✔
52
              "@type": "skos:Concept",
1✔
53
              "pref_label": "Pamphlets",
1✔
54
              "in_scheme": {
1✔
55
                "@id": "https://figgy.princeton.edu/ns/ephemeraGenres",
1✔
56
                "@type": "skos:ConceptScheme",
1✔
57
                "pref_label": "Ephemera Genres"
1✔
58
              },
1✔
59
              "exact_match": {
1✔
60
                "@id": {
1✔
61
                  "@id": "http://id.loc.gov/vocabulary/graphicMaterials/tgm001221"
1✔
62
                }
1✔
63
              }
1✔
64
            }
1✔
65
          ]"#;
1✔
66
        let formats: Vec<Format> = serde_json::from_str(json_ld).unwrap();
1✔
67
        assert_eq!(formats[0].pref_label, Some(solr::FormatFacet::Book));
1✔
68
    }
1✔
69

70
    #[test]
71
    fn it_can_parse_format_from_the_json_ld_when_exact_match_has_one_id() {
1✔
72
        let json_ld = r#"[
1✔
73
            {
1✔
74
              "@id": "https://figgy-staging.princeton.edu/catalog/5dbeae15-f46a-4411-a385-ce6df02231dc",
1✔
75
              "@type": "skos:Concept",
1✔
76
              "pref_label": "Pamphlets",
1✔
77
              "in_scheme": {
1✔
78
                "@id": "https://figgy.princeton.edu/ns/ephemeraGenres",
1✔
79
                "@type": "skos:ConceptScheme",
1✔
80
                "pref_label": "Ephemera Genres"
1✔
81
              },
1✔
82
              "exact_match": {
1✔
83
                  "@id": "http://id.loc.gov/vocabulary/graphicMaterials/tgm001221"
1✔
84
              }
1✔
85
            }
1✔
86
          ]"#;
1✔
87
        let formats: Vec<Format> = serde_json::from_str(json_ld).unwrap();
1✔
88
        assert_eq!(formats[0].pref_label, Some(solr::FormatFacet::Book));
1✔
89
    }
1✔
90
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc