1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
#[cfg(feature = "walker")]
use crate::walker::{Visitor, Walkable};
/// [CommentLine](crate::ast::CommentLine) ::= ("###" | "##" | "#") ("\u0020" comment_char*)? line_end
///
/// Adjacent comment lines of the same comment type are joined together during
/// the AST construction.
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "hash", derive(Eq, PartialOrd, Ord, Hash))]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct CommentLine {
lead: String,
comment: Option<String>,
}
impl CommentLine {
/// Constructs a new `CommentLine` representing a comment line in a Fluent Translation List (FTL) file.
///
/// # Arguments
/// * `lead` - The leading part of the line, including the `#` symbols and any immediate following whitespace.
/// * `comment` - The optional comment text after the initial whitespace following the `#` symbols.
/// If `None`, the line consists only of the leading hashes (an "empty" comment).
pub fn new(lead: String, comment: Option<String>) -> Self {
Self { lead, comment }
}
}
#[cfg(feature = "walker")]
impl Walkable for CommentLine {
fn walk(&self, visitor: &mut dyn Visitor) {
visitor.visit_comment_line(self);
}
}
impl std::fmt::Display for CommentLine {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let comment = self.comment.as_ref().map_or("".into(), |c| format!(" {c}"));
writeln!(f, "{}{}", self.lead, comment)
}
}