use std::borrow::Borrow;
use bevy::asset::AssetPath;
pub trait MapKey {
fn from_asset_path(path: &AssetPath) -> Self;
}
macro_rules! impl_map_key_extras {
($Key:ty) => {
impl AsRef<str> for $Key {
#[inline]
fn as_ref(&self) -> &str {
&self.0
}
}
impl Borrow<str> for $Key {
#[inline]
fn borrow(&self) -> &str {
&self.0
}
}
impl From<$Key> for Box<str> {
#[inline]
fn from(key: $Key) -> Self {
key.0
}
}
impl From<$Key> for String {
#[inline]
fn from(key: $Key) -> Self {
key.0.into()
}
}
};
}
#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct AssetFileName(Box<str>);
impl_map_key_extras!(AssetFileName);
impl MapKey for AssetFileName {
#[inline]
fn from_asset_path(path: &AssetPath) -> Self {
Self(
path.path()
.file_name()
.unwrap()
.to_str()
.expect("Path should be valid UTF-8")
.into(),
)
}
}
#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct AssetFileStem(Box<str>);
impl_map_key_extras!(AssetFileStem);
impl MapKey for AssetFileStem {
#[inline]
fn from_asset_path(path: &AssetPath) -> Self {
Self(
path.path()
.file_stem()
.unwrap()
.to_str()
.expect("Path should be valid UTF-8")
.into(),
)
}
}
#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct AssetLabel(Box<str>);
impl_map_key_extras!(AssetLabel);
impl MapKey for AssetLabel {
#[inline]
fn from_asset_path(path: &AssetPath) -> Self {
Self(path.label().expect("Asset does not have a label").into())
}
}
impl MapKey for String {
#[inline]
fn from_asset_path(path: &AssetPath) -> Self {
path_slash::PathExt::to_slash(path.path())
.expect("Path should be valid UTF-8")
.into()
}
}
impl MapKey for Box<str> {
#[inline]
fn from_asset_path(path: &AssetPath) -> Self {
path_slash::PathExt::to_slash(path.path())
.expect("Path should be valid UTF-8")
.into()
}
}