Skip to main content

mit_commit_message_lints/relates/lib/
relates_to.rs

1use std::borrow::Cow;
2
3use serde::{Deserialize, Serialize};
4
5/// User input data for the relates to trailer
6#[derive(Debug, Eq, PartialEq, Serialize, Deserialize, Clone)]
7pub struct RelateTo<'a> {
8    relates: Cow<'a, str>,
9}
10
11impl<'a> RelateTo<'a> {
12    /// Create a new relates to
13    #[must_use]
14    pub const fn new(relates: Cow<'a, str>) -> Self {
15        Self { relates }
16    }
17
18    /// What this relates to
19    #[must_use]
20    pub fn to(&self) -> &str {
21        &self.relates
22    }
23}
24
25impl<'a> From<&'a str> for RelateTo<'a> {
26    fn from(input: &'a str) -> Self {
27        RelateTo {
28            relates: Cow::Borrowed(input),
29        }
30    }
31}
32impl From<String> for RelateTo<'_> {
33    fn from(input: String) -> Self {
34        RelateTo {
35            relates: Cow::Owned(input),
36        }
37    }
38}
39
40#[cfg(test)]
41mod tests {
42    #![allow(clippy::wildcard_imports)]
43
44    use crate::relates::RelateTo;
45
46    #[test]
47    fn test_convert_string_to_relate_to() {
48        let relate = RelateTo::from("[#12343567]");
49
50        assert_eq!(
51            relate.to(),
52            "[#12343567]",
53            "Expected the relates-to value to match the string used to construct it"
54        );
55    }
56}