rune_core/item/
component_ref.rs

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
use core::fmt;

use serde::{Deserialize, Serialize};

#[cfg(feature = "alloc")]
use crate::alloc;
#[cfg(feature = "alloc")]
use crate::item::Component;

/// A reference to a component of an item.
///
/// All indexes refer to sibling indexes. So two sibling id components could
/// have the indexes `1` and `2` respectively.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub enum ComponentRef<'a> {
    /// A crate string component.
    Crate(&'a str),
    /// A regular string component.
    Str(&'a str),
    /// A nested anonymous part with an identifier.
    Id(usize),
}

impl<'a> ComponentRef<'a> {
    /// Get the component as a `&str` reference.
    pub fn as_str(&self) -> Option<&'a str> {
        match self {
            ComponentRef::Str(string) => Some(string),
            _ => None,
        }
    }

    /// Get the identifier of the component if it is an identifier component.
    pub fn id(self) -> Option<usize> {
        match self {
            Self::Id(n) => Some(n),
            _ => None,
        }
    }

    /// Coerce this [ComponentRef] into an owned [Component].
    #[cfg(feature = "alloc")]
    pub fn to_owned(&self) -> alloc::Result<Component> {
        Ok(match *self {
            ComponentRef::Crate(s) => Component::Crate(s.try_into()?),
            ComponentRef::Str(s) => Component::Str(s.try_into()?),
            ComponentRef::Id(id) => Component::Id(id),
        })
    }
}

impl fmt::Display for ComponentRef<'_> {
    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Crate(s) => write!(fmt, "::{}", s),
            Self::Str(s) => write!(fmt, "{}", s),
            Self::Id(n) => write!(fmt, "${}", n),
        }
    }
}