mit_commit_message_lints/mit/lib/
authors.rs1use std::{
2 collections::{btree_map::IntoIter, BTreeMap, HashSet},
3 convert::TryFrom,
4};
5
6use crate::mit::lib::{
7 author::Author,
8 errors::{DeserializeAuthorsError, SerializeAuthorsError},
9};
10
11#[derive(Debug, Eq, PartialEq, Clone, Default)]
13pub struct Authors<'a> {
14 pub authors: BTreeMap<String, Author<'a>>,
16}
17
18impl<'a> Authors<'a> {
19 #[must_use]
21 pub fn missing_initials(&'a self, authors_initials: Vec<&'a str>) -> Vec<&'a str> {
22 let configured: HashSet<_> = self.authors.keys().map(String::as_str).collect();
23 let from_cli: HashSet<_> = authors_initials.into_iter().collect();
24 from_cli.difference(&configured).copied().collect()
25 }
26
27 #[must_use]
29 pub const fn new(authors: BTreeMap<String, Author<'a>>) -> Self {
30 Self { authors }
31 }
32
33 #[must_use]
35 pub fn get(&self, author_initials: &'a [&'a str]) -> Vec<&'a Author<'_>> {
36 author_initials
37 .iter()
38 .filter_map(|initial| self.authors.get(*initial))
39 .collect()
40 }
41
42 #[must_use]
47 pub fn merge(&self, authors: &Self) -> Self {
48 let mut merged = self.authors.clone();
49 merged.extend(authors.authors.clone());
50 Self { authors: merged }
51 }
52
53 #[must_use]
57 pub fn example() -> Self {
58 let mut store = BTreeMap::new();
59 store.insert(
60 "ae".into(),
61 Author::new("Anyone Else".into(), "anyone@example.com".into(), None),
62 );
63 store.insert(
64 "se".into(),
65 Author::new("Someone Else".into(), "someone@example.com".into(), None),
66 );
67 store.insert(
68 "bt".into(),
69 Author::new(
70 "Billie Thompson".into(),
71 "billie@example.com".into(),
72 Some("0A46826A".into()),
73 ),
74 );
75 Self::new(store)
76 }
77}
78
79impl<'a> IntoIterator for Authors<'a> {
80 type Item = (String, Author<'a>);
81 type IntoIter = IntoIter<String, Author<'a>>;
82
83 fn into_iter(self) -> Self::IntoIter {
84 self.authors.into_iter()
85 }
86}
87
88impl<'a> TryFrom<&'a str> for Authors<'a> {
89 type Error = DeserializeAuthorsError;
90
91 fn try_from(input: &str) -> Result<Self, Self::Error> {
92 serde_yaml::from_str(input)
93 .or_else(|yaml_error| {
94 toml::from_str(input).map_err(|toml_error| {
95 DeserializeAuthorsError::new(input, &yaml_error, &toml_error)
96 })
97 })
98 .map(Self::new)
99 }
100}
101
102impl TryFrom<String> for Authors<'_> {
103 type Error = DeserializeAuthorsError;
104
105 fn try_from(input: String) -> Result<Self, Self::Error> {
106 serde_yaml::from_str(&input)
107 .or_else(|yaml_error| {
108 toml::from_str(&input).map_err(|toml_error| {
109 DeserializeAuthorsError::new(&input, &yaml_error, &toml_error)
110 })
111 })
112 .map(Authors::new)
113 }
114}
115
116impl<'a> TryFrom<Authors<'a>> for String {
117 type Error = SerializeAuthorsError;
118
119 fn try_from(value: Authors<'a>) -> Result<Self, Self::Error> {
120 toml::to_string(&value.authors).map_err(SerializeAuthorsError)
121 }
122}