shared_mime/
answer.rs

1/// Result of looking up a MIME type.
2#[derive(Debug, Clone)]
3pub struct Answer<'a> {
4    types: Vec<&'a str>,
5    ambiguous: bool,
6}
7
8impl Answer<'static> {
9    pub(crate) fn unknown() -> Answer<'static> {
10        Self::new(vec![], false)
11    }
12}
13
14impl<'a> Answer<'a> {
15    pub(crate) fn new(types: Vec<&'a str>, ambiguous: bool) -> Answer<'a> {
16        Answer { types, ambiguous }
17    }
18
19    pub(crate) fn definite(name: &'a str) -> Answer<'a> {
20        Answer {
21            types: vec![name],
22            ambiguous: false,
23        }
24    }
25
26    /// Query whether this answer is definite (resolved to a single, known type).
27    pub fn is_definite(&self) -> bool {
28        self.types.len() >= 1 && !self.ambiguous
29    }
30
31    /// Query whether this answer is unknown (no resulting types).
32    pub fn is_unknown(&self) -> bool {
33        self.types.len() == 0
34    }
35
36    /// Query whether this answer is ambiguous (multiple matching types).
37    pub fn is_ambiguous(&self) -> bool {
38        self.ambiguous
39    }
40
41    /// Get the best type, if known.  Returns [None] when no type is found or the type is ambiguous.
42    pub fn best(&self) -> Option<&'a str> {
43        if self.ambiguous || self.types.is_empty() {
44            None
45        } else {
46            Some(self.types[0])
47        }
48    }
49
50    /// Get all matching types.
51    pub fn all_types(&self) -> &'_ [&'a str] {
52        &self.types
53    }
54}