//! Provides a way to specify assets either by handle or by path. use bevy_asset::{Asset, Handle}; /// Enum that represents a reference to an asset as either a [`Handle`] or a [`String`] path. /// /// This is useful for when you want to specify an asset, but don't always have convenient /// access to an asset server reference. #[derive(Clone, Debug)] pub enum HandleOrPath { /// Specify the asset reference as a handle. Handle(Handle), /// Specify the asset reference as a [`String`]. Path(String), } impl Default for HandleOrPath { fn default() -> Self { Self::Path("".to_string()) } } // Necessary because we don't want to require T: PartialEq impl PartialEq for HandleOrPath { fn eq(&self, other: &Self) -> bool { match (self, other) { (HandleOrPath::Handle(h1), HandleOrPath::Handle(h2)) => h1 == h2, (HandleOrPath::Path(p1), HandleOrPath::Path(p2)) => p1 == p2, _ => false, } } } impl From> for HandleOrPath { fn from(h: Handle) -> Self { HandleOrPath::Handle(h) } } impl From<&str> for HandleOrPath { fn from(p: &str) -> Self { HandleOrPath::Path(p.to_string()) } } impl From for HandleOrPath { fn from(p: String) -> Self { HandleOrPath::Path(p.clone()) } } impl From<&String> for HandleOrPath { fn from(p: &String) -> Self { HandleOrPath::Path(p.to_string()) } } impl From<&HandleOrPath> for HandleOrPath { fn from(p: &HandleOrPath) -> Self { p.to_owned() } }