rune_core/item/
component_ref.rsuse core::fmt;
use serde::{Deserialize, Serialize};
#[cfg(feature = "alloc")]
use crate::alloc;
#[cfg(feature = "alloc")]
use crate::item::Component;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub enum ComponentRef<'a> {
Crate(&'a str),
Str(&'a str),
Id(usize),
}
impl<'a> ComponentRef<'a> {
pub fn as_str(&self) -> Option<&'a str> {
match self {
ComponentRef::Str(string) => Some(string),
_ => None,
}
}
pub fn id(self) -> Option<usize> {
match self {
Self::Id(n) => Some(n),
_ => None,
}
}
#[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),
}
}
}