camel_case_token_with_comment/
camel_case_token_with_comment.rs

1// ---------------- [ File: camel-case-token-with-comment/src/camel_case_token_with_comment.rs ]
2crate::ix!();
3
4pub type TokenQuad = [CamelCaseTokenWithComment; 4];
5
6#[derive(Builder,Getters,Setters,Serialize,Deserialize,Hash,Debug,Clone,PartialEq,Eq)]
7#[getset(get = "pub")]
8#[builder(setter(into, strip_option))]
9pub struct CamelCaseTokenWithComment {
10    data:    String,
11    comment: Option<String>,
12}
13
14impl Named for CamelCaseTokenWithComment {
15
16    fn name(&self) -> Cow<'_,str> {
17        Cow::Borrowed(&self.data)
18    }
19}
20
21impl Display for CamelCaseTokenWithComment {
22    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
23        match &self.comment {
24            Some(comment) => write!(f, "{} -- {}", self.data, comment),
25            None => write!(f, "{}", self.data),
26        }
27    }
28}
29
30impl Into<String> for CamelCaseTokenWithComment {
31
32    fn into(self) -> String {
33        match self.comment {
34            Some(ref comment) => format!("{} -- {}", self.data, comment),
35            None              => format!("{}", self.data),
36        }
37    }
38}
39
40impl std::str::FromStr for CamelCaseTokenWithComment {
41    type Err = TokenParseError;
42
43    fn from_str(s: &str) -> Result<Self, Self::Err> {
44        let line = s.trim();
45        if line.is_empty() {
46            return Err(TokenParseError::InvalidTokenLine(line.to_string()));
47        }
48
49        let parts: Vec<&str> = line.splitn(2, "--").collect();
50        let data = parts[0].trim();
51        if data.is_empty() {
52            return Err(TokenParseError::InvalidTokenLine(line.to_string()));
53        }
54
55        let comment = parts.get(1).map(|&s| s.trim().to_string()).filter(|s| !s.is_empty());
56
57        Ok(CamelCaseTokenWithComment {
58            data: data.to_string(),
59            comment,
60        })
61    }
62}
63
64#[cfg(test)]
65mod test_camel_case_token_with_comment {
66    use super::*;
67
68    #[traced_test]
69    fn test_from_str_valid() {
70        info!("Testing CamelCaseTokenWithComment::from_str with valid input");
71        let input = "TokenData -- Some comment";
72        let token = CamelCaseTokenWithComment::from_str(input).unwrap();
73        pretty_assert_eq!(token.data(), "TokenData");
74        pretty_assert_eq!(*token.comment(), Some("Some comment".to_string()));
75    }
76
77    #[traced_test]
78    fn test_from_str_no_comment() {
79        info!("Testing CamelCaseTokenWithComment::from_str with no comment");
80        let input = "TokenData";
81        let token = CamelCaseTokenWithComment::from_str(input).unwrap();
82        pretty_assert_eq!(token.data(), "TokenData");
83        pretty_assert_eq!(*token.comment(), None);
84    }
85
86    #[traced_test]
87    fn test_from_str_empty_data() {
88        info!("Testing CamelCaseTokenWithComment::from_str with empty data");
89        let input = " -- CommentOnly";
90        let err = CamelCaseTokenWithComment::from_str(input).unwrap_err();
91        assert!(matches!(err, TokenParseError::InvalidTokenLine(_)));
92    }
93
94    #[traced_test]
95    fn test_from_str_empty_input() {
96        info!("Testing CamelCaseTokenWithComment::from_str with empty input");
97        let input = "";
98        let err = CamelCaseTokenWithComment::from_str(input).unwrap_err();
99        assert!(matches!(err, TokenParseError::InvalidTokenLine(_)));
100    }
101
102    #[traced_test]
103    fn test_into_string_with_comment() {
104        info!("Testing Into<String> for CamelCaseTokenWithComment with comment");
105        let token = CamelCaseTokenWithComment::from_str("Data -- Comment").unwrap();
106        let as_string: String = token.into();
107        pretty_assert_eq!(as_string, "Data -- Comment");
108    }
109
110    #[traced_test]
111    fn test_into_string_without_comment() {
112        info!("Testing Into<String> for CamelCaseTokenWithComment without comment");
113        let token = CamelCaseTokenWithComment::from_str("Data").unwrap();
114        let as_string: String = token.into();
115        pretty_assert_eq!(as_string, "Data");
116    }
117
118    #[traced_test]
119    fn test_display_with_comment() {
120        info!("Testing Display for CamelCaseTokenWithComment with comment");
121        let token = CamelCaseTokenWithComment::from_str("Data -- Comment").unwrap();
122        pretty_assert_eq!(format!("{}", token), "Data -- Comment");
123    }
124
125    #[traced_test]
126    fn test_display_without_comment() {
127        info!("Testing Display for CamelCaseTokenWithComment without comment");
128        let token = CamelCaseTokenWithComment::from_str("Data").unwrap();
129        pretty_assert_eq!(format!("{}", token), "Data");
130    }
131
132    #[traced_test]
133    fn test_name_method() {
134        info!("Testing name() from Named trait on CamelCaseTokenWithComment");
135        let token = CamelCaseTokenWithComment::from_str("Data -- Comment").unwrap();
136        pretty_assert_eq!(token.name(), "Data");
137    }
138
139    #[traced_test]
140    fn test_token_quad_usage() {
141        info!("Testing TokenQuad type alias (array of 4 CamelCaseTokenWithComment)");
142        let t1 = CamelCaseTokenWithComment::from_str("Data1 -- Comment1").unwrap();
143        let t2 = CamelCaseTokenWithComment::from_str("Data2 -- Comment2").unwrap();
144        let t3 = CamelCaseTokenWithComment::from_str("Data3").unwrap();
145        let t4 = CamelCaseTokenWithComment::from_str("Data4 -- 4").unwrap();
146
147        let quad: TokenQuad = [t1, t2, t3, t4];
148        pretty_assert_eq!(quad.len(), 4);
149        pretty_assert_eq!(quad[0].data(), "Data1");
150        pretty_assert_eq!(quad[1].data(), "Data2");
151        pretty_assert_eq!(quad[2].data(), "Data3");
152        pretty_assert_eq!(quad[3].data(), "Data4");
153    }
154}