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

pulibrary / bibdata / 1b02b180-b09f-4a55-8a52-35af4d54976c

28 Aug 2025 01:47PM UTC coverage: 90.742% (+0.1%) from 90.597%
1b02b180-b09f-4a55-8a52-35af4d54976c

Pull #2906

circleci

christinach
Add method to change the thumbnail_url to a string
related to #2898
Pull Request #2906: Ephemera thumbnail

58 of 59 new or added lines in 6 files covered. (98.31%)

5 existing lines in 3 files now uncovered.

7714 of 8501 relevant lines covered (90.74%)

406.83 hits per line

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

88.33
/lib/bibdata_rs/src/languages.rs
1
pub mod iso_639_2b;
2
pub mod iso_639_3;
3
pub mod iso_639_5;
4

5
#[derive(Debug, PartialEq)]
6
pub struct Language {
7
    pub english_name: &'static str,
8
    pub two_letter_code: Option<&'static str>,
9
}
10

11
#[derive(Debug, PartialEq)]
12
pub struct Iso639_3Language {
13
    pub language: Language,
14
    pub macrolanguage_code: Option<&'static str>,
15
    pub iso_639_2b_code: Option<&'static str>,
16
}
17

18
impl Iso639_3Language {
19
    pub fn macrolanguage(&self) -> Option<Iso639_3Language> {
1✔
20
        self.macrolanguage_code
1✔
21
            .map(iso_639_3::from_iso_639_3_code)?
1✔
22
    }
1✔
23

24
    pub fn iso_639_2b_language(&self) -> Option<Language> {
2✔
25
        self.iso_639_2b_code.map(iso_639_2b::from_iso_639b_code)?
2✔
26
    }
2✔
27

28
    pub fn language_name(&self) -> &'static str {
2✔
29
        self.iso_639_2b_language()
2✔
30
            .map(|language_2b| language_2b.english_name)
2✔
31
            .unwrap_or(self.language.english_name)
2✔
32
    }
2✔
33
}
34

35
#[derive(Debug)]
36
pub struct NoSuchLanguageCode;
37

38
pub fn language_name(code: &str) -> Result<&'static str, NoSuchLanguageCode> {
3✔
39
    iso_639_3::from_iso_639_3_code(code)
3✔
40
        .map(|language3| language3.language_name())
3✔
41
        .or(iso_639_2b::from_iso_639b_code(code).map(|language2| language2.english_name))
3✔
42
        .ok_or(NoSuchLanguageCode)
3✔
43
}
3✔
44

45
// A wrapper for use in Ruby that uses owned strings
46
pub fn language_code_to_name(code: String) -> Option<String> {
×
47
    language_name(&code).ok().map(|name| name.to_owned())
×
48
}
×
49

50
pub fn is_valid_language_code(code: String) -> bool {
4✔
51
    if code.is_empty() {
4✔
52
        return false;
×
53
    }
4✔
54
    iso_639_2b::from_iso_639b_code(&code).is_some()
4✔
55
        || iso_639_5::from_iso_639_5_code(&code).is_some()
1✔
56
        || iso_639_3::from_iso_639_3_code(&code).is_some()
1✔
57
}
4✔
58

59
#[cfg(test)]
60
mod tests {
61
    use super::*;
62

63
    #[test]
64
    fn it_can_provide_english_name_of_iso_639_2b_language_code() {
1✔
65
        assert_eq!(
1✔
66
            iso_639_2b::from_iso_639b_code("mao").unwrap().english_name,
1✔
67
            "Maori"
68
        );
69
        assert_eq!(
1✔
70
            iso_639_2b::from_iso_639b_code("gla").unwrap().english_name,
1✔
71
            "Gaelic"
72
        );
73
    }
1✔
74

75
    #[test]
76
    fn it_can_find_the_macrolanguage_of_iso_639_3_language_code() {
1✔
77
        let wu_chinese = iso_639_3::from_iso_639_3_code("wuu").unwrap();
1✔
78
        assert_eq!(wu_chinese.language.english_name, "Wu Chinese");
1✔
79

80
        let macrolanguage = wu_chinese.macrolanguage().unwrap();
1✔
81
        assert_eq!(macrolanguage.language.english_name, "Chinese");
1✔
82
    }
1✔
83

84
    #[test]
85
    fn it_uses_iso_639_2b_name_if_available() {
1✔
86
        assert_eq!(language_name("spa").unwrap(), "Spanish"); // rather than "Castilian", the name in ISO 639-3
1✔
87
    }
1✔
88

89
    #[test]
90
    fn it_can_get_the_language_name_by_iso_639_3_or_iso_639_2b_code() {
1✔
91
        assert_eq!(language_name("ell").unwrap(), "Greek, Modern (1453-)"); // ell is the ISO 639-3 code
1✔
92
        assert_eq!(language_name("gre").unwrap(), "Greek, Modern (1453-)"); // ell is the ISO 639-2b code
1✔
93
    }
1✔
94

95
    #[test]
96
    fn it_can_validate_language_codes() {
1✔
97
        assert!(
1✔
98
            is_valid_language_code("per".to_owned()),
1✔
UNCOV
99
            "ISO 639-2b code is considered to be valid"
×
100
        );
101
        assert!(
1✔
102
            is_valid_language_code("grc".to_owned()),
1✔
UNCOV
103
            "ISO 639-3 code is considered to be valid"
×
104
        );
105
        assert!(
1✔
106
            is_valid_language_code("nah".to_owned()),
1✔
UNCOV
107
            "ISO 639-5 collective code is considered to be valid"
×
108
        );
109
        assert!(!is_valid_language_code("123".to_owned()), "Invalid code");
1✔
110
    }
1✔
111
}
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