Skip to main content

fluent4rs/ast/
entry.rs

1#[cfg(feature = "serde")]
2use serde::{Deserialize, Serialize};
3
4use super::{CommentLine, Message, Term};
5#[cfg(feature = "walker")]
6use crate::walker::{Visitor, Walkable, Walker};
7
8/// [Entry](crate::ast::Entry) ::= ([Message](crate::ast::Message) line_end)
9///  | ([Term](crate::ast::Term) line_end)
10///  | [CommentLine](crate::ast::CommentLine)
11///
12/// Entries are the main building blocks of Fluent.
13///
14/// They define translations and contextual and semantic information about the
15/// translations. During the AST construction, adjacent comment lines of the same
16/// comment type (defined by the number of #) are joined together. Single-# comments
17/// directly preceding [Message](crate::ast::Message)s and [Term](crate::ast::Term)s
18/// are attached to the [Message](crate::ast::Message) or [Term](crate::ast::Term)
19/// and are not standalone Entries.
20#[derive(Clone, Debug, PartialEq)]
21#[cfg_attr(feature = "hash", derive(Eq, PartialOrd, Ord, Hash))]
22#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
23pub enum Entry {
24    #[doc(hidden)]
25    Message(Message),
26
27    #[doc(hidden)]
28    Term(Term),
29
30    #[doc(hidden)]
31    CommentLine(CommentLine),
32}
33
34#[cfg(feature = "walker")]
35impl Walkable for Entry {
36    fn walk(&self, visitor: &mut dyn Visitor) {
37        visitor.visit_entry(self);
38        match self {
39            Self::Message(message) => Walker::walk(message, visitor),
40            Self::Term(term) => Walker::walk(term, visitor),
41            Self::CommentLine(comment) => Walker::walk(comment, visitor),
42        }
43    }
44}
45
46impl std::fmt::Display for Entry {
47    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
48        let value = match self {
49            Self::Message(message) => message.to_string(),
50            Self::Term(term) => term.to_string(),
51            Self::CommentLine(line) => line.to_string(),
52        };
53        write!(f, "{value}")
54    }
55}