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
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
use std::{
    collections::{btree_map::IntoIter, BTreeMap, HashSet},
    convert::TryFrom,
};

use crate::mit::lib::{
    author::Author,
    errors::{DeserializeAuthorsError, SerializeAuthorsError},
};

/// Collection of authors
#[derive(Debug, Eq, PartialEq, Clone, Default)]
pub struct Authors<'a> {
    /// A btree of the authors
    pub authors: BTreeMap<String, Author<'a>>,
}

impl<'a> Authors<'a> {
    /// From a list of initials get te ones that aren't in our config
    #[must_use]
    pub fn missing_initials(&'a self, authors_initials: Vec<&'a str>) -> Vec<&'a str> {
        let configured: HashSet<_> = self
            .authors
            .keys()
            .map(std::string::String::as_str)
            .collect();
        let from_cli: HashSet<_> = authors_initials.into_iter().collect();
        from_cli
            .difference(&configured)
            .into_iter()
            .copied()
            .collect()
    }

    /// Create a new author collection
    #[must_use]
    pub fn new(authors: BTreeMap<String, Author<'a>>) -> Self {
        Self { authors }
    }

    /// Get some authors by their initials
    #[must_use]
    pub fn get(&self, author_initials: &'a [&'a str]) -> Vec<&'a Author<'_>> {
        author_initials
            .iter()
            .filter_map(|initial| self.authors.get(*initial))
            .collect()
    }

    /// Merge two lists of authors
    ///
    /// This is used if the user has a author config file, and the authors are
    /// also saved in the vcs config
    #[must_use]
    pub fn merge(&self, authors: &Self) -> Self {
        Self {
            authors: authors
                .authors
                .iter()
                .fold(self.authors.clone(), |mut acc, (key, value)| {
                    acc.insert(key.clone(), value.clone());
                    acc
                }),
        }
    }

    /// Generate an example authors list
    ///
    /// Used to show the user what their config file might look like
    #[must_use]
    pub fn example() -> Self {
        let mut store = BTreeMap::new();
        store.insert(
            "ae".into(),
            Author::new("Anyone Else".into(), "anyone@example.com".into(), None),
        );
        store.insert(
            "se".into(),
            Author::new("Someone Else".into(), "someone@example.com".into(), None),
        );
        store.insert(
            "bt".into(),
            Author::new(
                "Billie Thompson".into(),
                "billie@example.com".into(),
                Some("0A46826A".into()),
            ),
        );
        Self::new(store)
    }
}

impl<'a> IntoIterator for Authors<'a> {
    type IntoIter = IntoIter<String, Author<'a>>;
    type Item = (String, Author<'a>);

    fn into_iter(self) -> Self::IntoIter {
        self.authors.into_iter()
    }
}

impl<'a> TryFrom<&'a str> for Authors<'a> {
    type Error = DeserializeAuthorsError;

    fn try_from(input: &str) -> std::result::Result<Self, Self::Error> {
        serde_yaml::from_str(input)
            .or_else(|yaml_error| {
                toml::from_str(input).map_err(|toml_error| {
                    DeserializeAuthorsError::new(input, &yaml_error, &toml_error)
                })
            })
            .map(Self::new)
    }
}

impl<'a> TryFrom<String> for Authors<'a> {
    type Error = DeserializeAuthorsError;

    fn try_from(input: String) -> std::result::Result<Self, Self::Error> {
        serde_yaml::from_str(&input)
            .or_else(|yaml_error| {
                toml::from_str(&input).map_err(|toml_error| {
                    DeserializeAuthorsError::new(&input, &yaml_error, &toml_error)
                })
            })
            .map(Authors::new)
    }
}

impl<'a> TryFrom<Authors<'a>> for String {
    type Error = SerializeAuthorsError;

    fn try_from(value: Authors<'a>) -> Result<Self, Self::Error> {
        toml::to_string(&value.authors).map_err(SerializeAuthorsError)
    }
}