Skip to main content

bv_types/
subtyping.rs

1use crate::vocabulary;
2
3/// Returns true if `child` is the same type as `parent` or a descendant of it.
4pub fn is_subtype_of(child: &str, parent: &str) -> bool {
5    if child == parent {
6        return true;
7    }
8    let vocab = vocabulary::vocabulary();
9    let mut current = child;
10    loop {
11        match vocab.get(current).and_then(|d| d.parent.as_deref()) {
12            None => return false,
13            Some(p) if p == parent => return true,
14            Some(p) => current = p,
15        }
16    }
17}
18
19#[cfg(test)]
20mod tests {
21    use super::*;
22
23    #[test]
24    fn fasta_is_subtype_of_file() {
25        assert!(is_subtype_of("fasta", "file"));
26    }
27
28    #[test]
29    fn blast_tab_is_subtype_of_tabular_and_file() {
30        assert!(is_subtype_of("blast_tab", "tabular"));
31        assert!(is_subtype_of("blast_tab", "file"));
32    }
33
34    #[test]
35    fn file_is_not_subtype_of_fasta() {
36        assert!(!is_subtype_of("file", "fasta"));
37    }
38
39    #[test]
40    fn identity() {
41        assert!(is_subtype_of("fasta", "fasta"));
42    }
43
44    #[test]
45    fn unknown_type_is_not_subtype() {
46        assert!(!is_subtype_of("unknown_type", "file"));
47    }
48}