Skip to main content

mit_commit_message_lints/mit/cmd/
get_config_rotation.rs

1use miette::Result;
2
3use crate::{external::Vcs, mit::lib::rotation_option::RotationOption};
4
5/// Get the rotation configuration setting
6///
7/// Returns `None` when rotation is not configured (rotation is off).
8/// Returns `Some(RotationOption::RoundRobin)` when round-robin rotation
9/// is enabled.
10///
11/// # Errors
12///
13/// Returns an error if reading the git config fails, or if the stored
14/// value cannot be parsed as a valid rotation option.
15pub fn get_config_rotation(store: &dyn Vcs) -> Result<Option<RotationOption>> {
16    match store.get_str(super::CONFIG_KEY_ROTATION)?.map(String::from) {
17        Some(s) => Ok(Some(s.parse()?)),
18        None => Ok(None),
19    }
20}
21
22#[cfg(test)]
23mod tests {
24    use std::collections::BTreeMap;
25
26    use crate::external::InMemory;
27    use crate::mit::lib::rotation_option::RotationOption;
28
29    #[test]
30    fn get_config_rotation_returns_none_when_not_set() {
31        let mut buffer = BTreeMap::new();
32        let vcs_config = InMemory::new(&mut buffer);
33
34        let result = crate::mit::cmd::get_config_rotation::get_config_rotation(&vcs_config);
35
36        assert_eq!(
37            result.unwrap(),
38            None,
39            "Expected no rotation config when the key is not set"
40        );
41    }
42
43    #[test]
44    fn get_config_rotation_returns_round_robin_when_set() {
45        let mut buffer = BTreeMap::new();
46        buffer.insert("mit.author.rotate".into(), "round-robin".into());
47        let vcs_config = InMemory::new(&mut buffer);
48
49        let result = crate::mit::cmd::get_config_rotation::get_config_rotation(&vcs_config);
50
51        assert_eq!(
52            result.unwrap(),
53            Some(RotationOption::RoundRobin),
54            "Expected round-robin rotation config when set to 'round-robin'"
55        );
56    }
57
58    #[test]
59    fn get_config_rotation_returns_error_for_invalid_value() {
60        let mut buffer = BTreeMap::new();
61        buffer.insert("mit.author.rotate".into(), "nonsense".into());
62        let vcs_config = InMemory::new(&mut buffer);
63
64        let result = crate::mit::cmd::get_config_rotation::get_config_rotation(&vcs_config);
65
66        assert!(
67            result.is_err(),
68            "Expected an error when rotation config is set to an invalid value"
69        );
70    }
71}