rune_core/item/
component.rsuse core::fmt;
use serde::{Deserialize, Serialize};
use crate::alloc;
use crate::alloc::prelude::*;
use crate::item::ComponentRef;
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[non_exhaustive]
#[serde(rename_all = "snake_case", tag = "type")]
pub enum Component {
Crate(Box<str>),
Str(Box<str>),
Id(usize),
}
impl Component {
pub fn id(&self) -> Option<usize> {
match self {
Self::Id(n) => Some(*n),
_ => None,
}
}
pub fn as_component_ref(&self) -> ComponentRef<'_> {
match self {
Self::Crate(s) => ComponentRef::Crate(s),
Self::Str(s) => ComponentRef::Str(s),
Self::Id(n) => ComponentRef::Id(*n),
}
}
}
impl TryClone for Component {
fn try_clone(&self) -> alloc::Result<Self> {
Ok(match self {
Component::Crate(string) => Component::Crate(string.try_clone()?),
Component::Str(string) => Component::Str(string.try_clone()?),
Component::Id(id) => Component::Id(*id),
})
}
}
impl fmt::Display for Component {
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),
}
}
}