spo_rhai/ast/
ident.rs

1//! Module defining script identifiers.
2
3use crate::{ImmutableString, Position};
4#[cfg(feature = "no_std")]
5use std::prelude::v1::*;
6use std::{borrow::Borrow, fmt, hash::Hash};
7
8/// _(internals)_ An identifier containing a name and a [position][Position].
9/// Exported under the `internals` feature only.
10#[derive(Clone, Eq, PartialEq, Hash)]
11pub struct Ident {
12    /// Identifier name.
13    pub name: ImmutableString,
14    /// Position.
15    pub pos: Position,
16}
17
18impl fmt::Debug for Ident {
19    #[cold]
20    #[inline(never)]
21    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
22        write!(f, "{:?}", self.name)?;
23        if !self.pos.is_none() {
24            write!(f, " @ {:?}", self.pos)?;
25        }
26        Ok(())
27    }
28}
29
30impl Borrow<str> for Ident {
31    #[inline(always)]
32    #[must_use]
33    fn borrow(&self) -> &str {
34        self.name.as_ref()
35    }
36}
37
38impl AsRef<str> for Ident {
39    #[inline(always)]
40    #[must_use]
41    fn as_ref(&self) -> &str {
42        self.name.as_ref()
43    }
44}
45
46impl Ident {
47    /// Get the name of the identifier as a string slice.
48    #[inline(always)]
49    #[must_use]
50    pub fn as_str(&self) -> &str {
51        &self.name
52    }
53    /// Is the identifier empty?
54    #[inline(always)]
55    #[must_use]
56    pub fn is_empty(&self) -> bool {
57        self.name.is_empty()
58    }
59}