mit_commit_message_lints/relates/cmd/
get_relate_to_configuration.rs1use std::{
2 convert::TryInto,
3 time::{Duration, SystemTime, UNIX_EPOCH},
4};
5
6use miette::{IntoDiagnostic, Result, WrapErr};
7
8use crate::{external::Vcs, relates::RelateTo};
9
10const CONFIG_KEY_EXPIRES: &str = "mit.relate.expires";
11
12pub fn get_relate_to_configuration(config: &dyn Vcs) -> Result<Option<RelateTo<'_>>> {
19 let config_value = config.get_i64(CONFIG_KEY_EXPIRES)?;
20
21 match config_value {
22 Some(config_value) => {
23 let now = now()?;
24
25 if now < Duration::from_secs(config_value.try_into().into_diagnostic()?) {
26 let relate_to_config = get_vcs_relate_to(config)?.map(RelateTo::from);
27
28 Ok(relate_to_config)
29 } else {
30 Ok(None)
31 }
32 }
33 None => Ok(None),
34 }
35}
36
37fn now() -> Result<Duration> {
38 SystemTime::now()
39 .duration_since(UNIX_EPOCH)
40 .into_diagnostic()
41}
42
43#[allow(clippy::maybe_infinite_iter)]
44fn get_vcs_relate_to(config: &dyn Vcs) -> Result<Option<&str>> {
45 config
46 .get_str("mit.relate.to")
47 .wrap_err("failed to read relate-to issue")
48}