fluent4rs 2.3.1

Parser / codec for [Fluent FTL files](https://github.com/projectfluent/fluent/blob/master/spec/fluent.ebnf), written for [lingora](https://github.com/nigeleke/lingora) (a localization management program), and may be found to be useful outside of that context. It is not intended to replace any aspects of the [fluent-rs](https://github.com/projectfluent/fluent-rs) crate implemented by [Project Fluent](https://projectfluent.org/), and, for the majority of language translation needs, the reader is referred back to that crate.
Documentation
#[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())
        )
    }
}