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

pulibrary / bibdata / 6641f0fa-3386-4dda-9066-fd8252689769

26 Aug 2025 07:21PM UTC coverage: 90.652% (-0.2%) from 90.806%
6641f0fa-3386-4dda-9066-fd8252689769

push

circleci

christinach
[Subject]Support random double id structure that is generated in figgy.
related to [#2639]

23 of 40 new or added lines in 2 files covered. (57.5%)

1 existing line in 1 file now uncovered.

7632 of 8419 relevant lines covered (90.65%)

395.97 hits per line

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

70.0
/lib/bibdata_rs/src/ephemera/ephemera_folder/subject.rs
1
use serde::{Deserialize, Deserializer, Serialize};
2
use serde_json::Value;
3

4
#[derive(Clone, Deserialize, Debug)]
5
pub struct Subject {
6
    pub exact_match: ExactMatch,
7
    #[serde(rename = "pref_label")]
8
    pub label: String,
9
}
10

11
#[derive(Clone, Serialize, Debug)]
12
pub struct ExactMatch {
13
    pub id: Id,
14
}
15
#[derive(Clone, Deserialize, Serialize, Debug)]
16
pub struct Id {
17
    #[serde(rename = "@id")]
18
    pub id: String,
19
}
20
impl Id {
21
    fn subject_ids(&self) -> anyhow::Result<Vec<String>> {
1✔
22
        if self.id.starts_with('[') {
1✔
NEW
23
            let v: Vec<String> = serde_json::from_str(&self.id)?;
×
NEW
24
            Ok(v)
×
25
        } else {
26
            Ok(vec![self.id.clone()])
1✔
27
        }
28
    }
1✔
29
}
30

31
impl ExactMatch {
32
    pub fn accepted_vocabulary(&self) -> bool {
1✔
33
        matches!(&self.id.subject_ids(), Ok(s) if s.iter().any(|url| {
1✔
34
                  url.starts_with("http://id.loc.gov")
1✔
NEW
35
                     || url.starts_with("https://homosaurus.org/")
×
36
        }))
1✔
37
    }
1✔
38
}
39

40
impl<'de> Deserialize<'de> for ExactMatch {
41
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
81✔
42
    where
81✔
43
        D: Deserializer<'de>,
81✔
44
    {
45
        let value = Value::deserialize(deserializer)?;
81✔
46
        if let Some(id_value) = value.get("@id") {
81✔
47
            if id_value.is_string() {
81✔
48
                return Ok(ExactMatch {
81✔
49
                    id: Id {
81✔
50
                        id: id_value.as_str().unwrap().to_string(),
81✔
51
                    },
81✔
52
                });
81✔
NEW
53
            } else if id_value.is_object() {
×
NEW
54
                if let Some(nested_id) = id_value.get("@id") {
×
NEW
55
                    if nested_id.is_string() {
×
NEW
56
                        return Ok(ExactMatch {
×
NEW
57
                            id: Id {
×
NEW
58
                                id: nested_id.as_str().unwrap().to_string(),
×
NEW
59
                            },
×
NEW
60
                        });
×
NEW
61
                    }
×
NEW
62
                }
×
NEW
63
            }
×
UNCOV
64
        }
×
NEW
65
        Err(serde::de::Error::custom(
×
NEW
66
            "Could not parse ExactMatch Subject id",
×
NEW
67
        ))
×
68
    }
81✔
69
}
70

71

72
#[cfg(test)]
73
mod tests {
74
    use super::*;
75

76
    #[test]
77
    fn it_can_parse_exact_match_from_the_json_ld() {
1✔
78
        let json_ld = r#"[
1✔
79
            {
1✔
80
                "@id": "https://figgy-staging.princeton.edu/catalog/abf14319-f4bd-48d3-b4ab-5369354cc4aa",
1✔
81
                "@type": "skos:Concept",
1✔
82
                "pref_label": "Music",
1✔
83
                "in_scheme": {
1✔
84
                    "@id": "https://figgy.princeton.edu/ns/lAESubjects/artsAndCulture",
1✔
85
                    "@type": "skos:ConceptScheme",
1✔
86
                    "pref_label": "Arts and culture"
1✔
87
                },
1✔
88
                "exact_match": {
1✔
89
                   "@id": "http://id.loc.gov/authorities/subjects/sh85088762"
1✔
90
                }
1✔
91
            }
1✔
92
        ]"#;
1✔
93
        let subject: Vec<Subject> = serde_json::from_str(json_ld).unwrap();
1✔
94
        assert_eq!(
1✔
95
            subject[0].exact_match.id.id,
1✔
96
            "http://id.loc.gov/authorities/subjects/sh85088762"
97
        );
98
        assert_eq!(subject[0].label, "Music")
1✔
99
    }
1✔
100
}
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