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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use super::{AttributeAccessor, Identifier};
#[cfg(feature = "walker")]
use crate::walker::{Visitor, Walkable, Walker};
/// [MessageReference](crate::ast::MessageReference) ::= [Identifier](crate::ast::Identifier) [AttributeAccessor](crate::ast::AttributeAccessor)?
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "hash", derive(Eq, PartialOrd, Ord, Hash))]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct MessageReference {
identifier: Identifier,
attribute_accessor: Option<AttributeAccessor>,
}
impl MessageReference {
/// Constructs a new `MessageReference` representing a reference to a Fluent message or term.
/// This allows referencing either the primary value of a message/term (when `attribute_accessor` is `None`)
/// or one of its attributes (e.g., `my-message.title`).
///
/// Message references are commonly used in placeables like `{ $var }` or directly as `{ msg-ref }`.
///
/// # Arguments
/// * `identifier` - The identifier of the referenced message or term.
/// * `attribute_accessor` - Optional attribute access. If `Some`, refers to a specific attribute
/// of the message/term; if `None`, refers to its primary value.
pub fn new(identifier: Identifier, attribute_accessor: Option<AttributeAccessor>) -> Self {
Self {
identifier,
attribute_accessor,
}
}
/// Returns the message identifier.
///
/// Note: a [MessageReference](crate::ast::MessageReference) and [TermReference](crate::ast::TermReference) [Identifier](crate::ast::Identifier)
/// may be the same, e,g, `product = ...` versus `-product = ...`.
pub fn identifier(&self) -> &Identifier {
&self.identifier
}
/// Returns the message identifier _name_.
///
/// Note: Differentiates the [MessageReference](crate::ast::MessageReference) and [TermReference](crate::ast::TermReference)
/// [Identifier](crate::ast::Identifier) name by using the '-' prefix for the [TermReference](crate::ast::TermReference).
pub fn identifier_name(&self) -> String {
self.identifier.to_string()
}
/// Returns an optional reference to the attribute accessor.
pub fn attribute_accessor(&self) -> Option<&AttributeAccessor> {
self.attribute_accessor.as_ref()
}
}
#[cfg(feature = "walker")]
impl Walkable for MessageReference {
fn walk(&self, visitor: &mut dyn Visitor) {
visitor.visit_message_reference(self);
Walker::walk(&self.identifier, visitor);
self.attribute_accessor
.iter()
.for_each(|aa| Walker::walk(aa, visitor));
}
}
impl std::fmt::Display for MessageReference {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}{}",
self.identifier,
self.attribute_accessor
.as_ref()
.map_or("".to_string(), |aa| aa.to_string())
)
}
}