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::{CallArguments, Identifier};
#[cfg(feature = "walker")]
use crate::walker::{Visitor, Walkable, Walker};
/// [FunctionReference](crate::ast::FunctionReference) ::= [Identifier](crate::ast::Identifier) [CallArguments](crate::ast::CallArguments)
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "hash", derive(Eq, PartialOrd, Ord, Hash))]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct FunctionReference {
identifier: Identifier,
call_arguments: CallArguments,
}
impl FunctionReference {
/// Constructs a new `FunctionReference` representing a built-in or custom function call in Fluent.
/// Functions are used for advanced formatting, such as `NUMBER(value, style: "percent")`
/// or `DATETIME(date, month: "long")`.
///
/// # Arguments
/// * `identifier` - The name of the function (e.g., `NUMBER`, `PLATFORM`, or a custom function).
/// * `call_arguments` - The arguments passed to the function, which may include positional
/// and/or named arguments.
pub fn new(identifier: Identifier, call_arguments: CallArguments) -> Self {
Self {
identifier,
call_arguments,
}
}
/// Returns a reference to the function identifier.
pub fn identifier(&self) -> &Identifier {
&self.identifier
}
/// Returns the string name of the function identifier.
///
/// This is equivalent to `self.identifier.to_string()` and is useful when serializing
/// or displaying the function call.
pub fn identifier_name(&self) -> String {
self.identifier.to_string()
}
/// Returns a reference to the call arguments of this function reference.
pub fn call_arguments(&self) -> &CallArguments {
&self.call_arguments
}
}
#[cfg(feature = "walker")]
impl Walkable for FunctionReference {
fn walk(&self, visitor: &mut dyn Visitor) {
visitor.visit_function_reference(self);
Walker::walk(&self.identifier, visitor);
Walker::walk(&self.call_arguments, visitor);
}
}
impl std::fmt::Display for FunctionReference {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}{}", self.identifier, self.call_arguments)
}
}