mit_commit_message_lints/mit/cmd/
get_commit_coauthor_configuration.rs

1use std::borrow::Cow;
2
3use miette::{IntoDiagnostic, Result};
4use time::OffsetDateTime;
5
6use crate::{
7    external::Vcs,
8    mit::{cmd::CONFIG_KEY_EXPIRES, Author, AuthorState},
9};
10
11/// Get the co-authors that are currently defined for this vcs config source
12///
13/// # Errors
14///
15/// Will fail if reading or writing from the VCS config fails, or it contains
16/// data in an incorrect format
17pub fn get_commit_coauthor_configuration(config: &dyn Vcs) -> Result<AuthorState<Vec<Author<'_>>>> {
18    let config_value = config.get_i64(CONFIG_KEY_EXPIRES)?;
19
20    match config_value {
21        Some(config_value) => {
22            let config_time =
23                OffsetDateTime::from_unix_timestamp(config_value).into_diagnostic()?;
24            if OffsetDateTime::now_utc() < config_time {
25                let author_config = get_vcs_authors(config)?;
26
27                Ok(AuthorState::Some(author_config))
28            } else {
29                Ok(AuthorState::Timeout(config_time))
30            }
31        }
32        None => Ok(AuthorState::None),
33    }
34}
35
36fn get_vcs_authors(config: &'_ dyn Vcs) -> Result<Vec<Author<'_>>> {
37    let co_author_names = get_vcs_coauthor_names(config)?;
38    let co_author_emails = get_vcs_coauthor_emails(config)?;
39
40    Ok(co_author_names
41        .into_iter()
42        .zip(co_author_emails)
43        .filter_map(new_author)
44        .collect())
45}
46
47fn new_author<'a>(parameters: (Option<Cow<'a, str>>, Option<Cow<'a, str>>)) -> Option<Author<'a>> {
48    match parameters {
49        (Some(name), Some(email)) => Some(Author::new(name, email, None)),
50        _ => None,
51    }
52}
53
54fn get_vcs_coauthor_names(config: &'_ dyn Vcs) -> Result<Vec<Option<Cow<'_, str>>>> {
55    super::vcs::get_vcs_coauthors_config(config, "name")
56}
57
58fn get_vcs_coauthor_emails(config: &'_ dyn Vcs) -> Result<Vec<Option<Cow<'_, str>>>> {
59    super::vcs::get_vcs_coauthors_config(config, "email")
60}