1
//! Miscellaneous utility functions/macros/types.
2

            
3
use serde::{Deserialize, Serialize};
4

            
5
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
6
#[serde(into = "Vec<T>")]
7
#[serde(try_from = "Vec<T>")]
8
/// A list that contains at least one item.
9
///
10
/// We mainly use this for ensuring the user configures at least one item during config
11
/// deserialization, but may be useful for enforcing at least one item in the type system.
12
pub(crate) struct NonEmptyList<T: Clone>(T, Vec<T>);
13

            
14
impl<T: Clone> NonEmptyList<T> {
15
    /// Get the items in the list.
16
    ///
17
    /// Is guaranteed to have at least one item.
18
    pub(crate) fn iter(&self) -> impl Iterator<Item = &T> {
19
        Some(&self.0).into_iter().chain(&self.1)
20
    }
21
}
22

            
23
impl<T: Clone> From<NonEmptyList<T>> for Vec<T> {
24
2
    fn from(from: NonEmptyList<T>) -> Vec<T> {
25
2
        Some(from.0).into_iter().chain(from.1).collect()
26
2
    }
27
}
28

            
29
impl<T: Clone> TryFrom<Vec<T>> for NonEmptyList<T> {
30
    type Error = EmptyListError;
31

            
32
4
    fn try_from(mut from: Vec<T>) -> Result<Self, Self::Error> {
33
4
        if from.is_empty() {
34
2
            return Err(EmptyListError);
35
2
        }
36
2
        Ok(Self(from.remove(0), from))
37
4
    }
38
}
39

            
40
#[derive(Debug, thiserror::Error)]
41
/// An error indicating that the list was empty, so cannot be converted to a [`NonEmptyList`].
42
#[error("The list is empty")]
43
pub(crate) struct EmptyListError;
44

            
45
#[cfg(test)]
46
mod test {
47
    #![allow(clippy::unwrap_used)]
48

            
49
    use super::*;
50

            
51
    #[test]
52
    fn non_empty_list() {
53
        let v = vec![1, 2, 3];
54
        let l: NonEmptyList<_> = v.clone().try_into().unwrap();
55
        assert_eq!(v, Vec::from(l));
56

            
57
        assert!(NonEmptyList::<u32>::try_from(Vec::new()).is_err());
58
    }
59
}