Skip to main content

zlink_core/idl/
comment.rs

1//! Comment definitions for Varlink interfaces.
2
3use core::fmt;
4
5/// A comment in a Varlink interface.
6#[derive(Debug, Clone, PartialEq, Eq)]
7pub struct Comment<'a> {
8    /// The comment text content (without the leading #).
9    content: &'a str,
10}
11
12impl<'a> Comment<'a> {
13    /// Creates a new comment with the given text content (without # prefix).
14    pub const fn new(content: &'a str) -> Self {
15        Self { content }
16    }
17
18    /// Returns the comment text content.
19    pub fn content(&self) -> &'a str {
20        self.content
21    }
22
23    /// Returns the comment text content.
24    pub fn text(&self) -> &'a str {
25        self.content
26    }
27}
28
29impl<'a> fmt::Display for Comment<'a> {
30    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31        write!(f, "# {}", self.content)
32    }
33}
34
35#[cfg(test)]
36mod tests {
37    use super::*;
38
39    #[test]
40    fn comment_creation() {
41        let comment = Comment::new("This is a comment");
42        assert_eq!(comment.content(), "This is a comment");
43        assert_eq!(comment.text(), "This is a comment");
44    }
45
46    #[test]
47    fn comment_without_hash() {
48        let comment = Comment::new("This is not a proper comment");
49        assert_eq!(comment.content(), "This is not a proper comment");
50        assert_eq!(comment.text(), "This is not a proper comment");
51    }
52
53    #[test]
54    fn comment_display() {
55        let comment = Comment::new("A enum field allowing to gracefully get metadata");
56        use core::fmt::Write;
57        let mut buf = String::new();
58        write!(buf, "{}", comment).unwrap();
59        assert_eq!(
60            buf.as_str(),
61            "# A enum field allowing to gracefully get metadata"
62        );
63    }
64}