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
/// Result of looking up a MIME type.
#[derive(Debug, Clone)]
pub struct Answer<'a> {
    types: Vec<&'a str>,
    ambiguous: bool,
}

impl Answer<'static> {
    pub(crate) fn unknown() -> Answer<'static> {
        Self::new(vec![], false)
    }
}

impl<'a> Answer<'a> {
    pub(crate) fn new(types: Vec<&'a str>, ambiguous: bool) -> Answer<'a> {
        Answer { types, ambiguous }
    }

    pub(crate) fn definite(name: &'a str) -> Answer<'a> {
        Answer {
            types: vec![name],
            ambiguous: false,
        }
    }

    /// Query whether this answer is definite (resolved to a single, known type).
    pub fn is_definite(&self) -> bool {
        self.types.len() >= 1 && !self.ambiguous
    }

    /// Query whether this answer is unknown (no resulting types).
    pub fn is_unknown(&self) -> bool {
        self.types.len() == 0
    }

    /// Query whether this answer is ambiguous (multiple matching types).
    pub fn is_ambiguous(&self) -> bool {
        self.ambiguous
    }

    /// Get the best type, if known.  Returns [None] when no type is found or the type is ambiguous.
    pub fn best(&self) -> Option<&'a str> {
        if self.ambiguous || self.types.is_empty() {
            None
        } else {
            Some(self.types[0])
        }
    }

    /// Get all matching types.
    pub fn all_types(&self) -> &'_ [&'a str] {
        &self.types
    }
}