use std::{
ops::{Deref, DerefMut},
path::{Path, PathBuf},
};
use crate::api::{ResourceKind, ResourceLocation};
pub struct ResourcePath(PathBuf);
impl ResourcePath {
pub fn for_resource(root: impl AsRef<Path>, resource: &ResourceLocation) -> Self {
let mut path = Self::for_kind(root, resource.namespace(), resource.kind());
path.push(resource.path());
Self(path.with_extension(resource.kind().extension()))
}
pub fn for_kind(root: impl AsRef<Path>, namespace: &str, kind: ResourceKind) -> Self {
let mut path = PathBuf::from(root.as_ref());
path.push(kind.category().directory());
path.push(namespace);
path.push(kind.directory());
Self(path)
}
pub fn into_inner(self) -> PathBuf {
self.0
}
}
impl AsRef<Path> for ResourcePath {
fn as_ref(&self) -> &Path {
self.0.as_ref()
}
}
impl Deref for ResourcePath {
type Target = PathBuf;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for ResourcePath {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}