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
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use super::{Identifier, Pattern};
#[cfg(feature = "walker")]
use crate::walker::{Visitor, Walkable, Walker};
/// [Attribute](crate::ast::Attribute) ::= line_end blank? "." [Identifier](crate::ast::Identifier) blank_inline? "=" blank_inline? [Pattern](crate::ast::Pattern)
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "hash", derive(Eq, PartialOrd, Ord, Hash))]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct Attribute {
identifier: Identifier,
pattern: Pattern,
}
impl Attribute {
/// Constructs a new `Attribute` with the given identifier and pattern.
///
/// # Arguments
/// * `identifier` - The name of the attribute (e.g., `title`, `description`). Must be a valid Fluent identifier.
/// * `pattern` - The value pattern of the attribute, which can include text, placeables, selectors, etc.
pub fn new(identifier: Identifier, pattern: Pattern) -> Self {
Self {
identifier,
pattern,
}
}
/// Returns the attribute identifier.
///
/// Note: a [Message](crate::ast::Message) and [Term](crate::ast::Term) [Identifier](crate::ast::Identifier)
/// may also be the same, e,g, `product = ...` versus `-product = ...`.
pub fn identifier(&self) -> &Identifier {
&self.identifier
}
/// Returns the attribute identifier _name_.
///
/// Note: Differentiates the [Message](crate::ast::Message) and [Term](crate::ast::Term)
/// [Identifier](crate::ast::Identifier) name by using the '.' prefix for the [Term](crate::ast::Term).
pub fn identifier_name(&self) -> String {
format!(".{}", self.identifier)
}
/// Returns the atrribute pattern.
pub fn pattern(&self) -> &Pattern {
&self.pattern
}
}
#[cfg(feature = "walker")]
impl Walkable for Attribute {
fn walk(&self, visitor: &mut dyn Visitor) {
visitor.visit_attribute(self);
Walker::walk(&self.identifier, visitor);
Walker::walk(&self.pattern, visitor);
}
}
impl std::fmt::Display for Attribute {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "\n .{} = {}", self.identifier, self.pattern)
}
}