use std::fmt::{Display, Formatter};
use crate::graph::resource::ResourceType;
#[derive(Debug, Default, Clone, Hash, Eq, PartialEq)]
pub struct VirtualResource {
pub(crate) name: String,
pub(crate) version: usize,
ty: ResourceType,
}
#[derive(Eq, PartialEq, Copy, Clone, Debug)]
pub struct HashedResource {
pub hash: u64,
}
impl Display for HashedResource {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.hash)
}
}
impl VirtualResource {
pub(crate) fn final_image(name: impl Into<String>) -> Self {
VirtualResource {
name: name.into(),
version: usize::MAX,
ty: ResourceType::Image,
}
}
pub fn image(name: impl Into<String>) -> Self {
VirtualResource {
name: name.into(),
version: 0,
ty: ResourceType::Image,
}
}
pub fn buffer(name: impl Into<String>) -> Self {
VirtualResource {
name: name.into(),
version: 0,
ty: ResourceType::Buffer,
}
}
pub fn upgrade(&self) -> Self {
VirtualResource {
name: self.name.clone(),
version: self.version + 1,
ty: self.ty,
}
}
pub fn name(&self) -> &str {
&self.name
}
pub fn version(&self) -> usize {
self.version
}
pub fn is_source(&self) -> bool {
self.version() == 0
}
pub fn is_associated_with(&self, rhs: &VirtualResource) -> bool {
self.name() == rhs.name()
}
pub fn is_older(lhs: &VirtualResource, rhs: &VirtualResource) -> bool {
if !lhs.is_associated_with(rhs) {
return false;
}
lhs.version() < rhs.version()
}
pub fn is_younger(lhs: &VirtualResource, rhs: &VirtualResource) -> bool {
if !lhs.is_associated_with(rhs) {
return false;
}
lhs.version() > rhs.version()
}
pub fn resource_type(&self) -> ResourceType {
self.ty
}
pub fn uid(&self) -> String {
format!("{}", self)
}
}
impl Display for VirtualResource {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
if self.version == usize::MAX {
write!(f, "{}_final", self.name())
} else {
write!(f, "{}{}", self.name(), String::from_utf8(vec![b'+'; self.version]).unwrap())
}
}
}
#[macro_export]
macro_rules! image {
($id:literal) => {
::phobos::prelude::VirtualResource::image($id)
};
}
#[macro_export]
macro_rules! buffer {
($id:literal) => {
::phobos::prelude::VirtualResource::buffer($id)
};
}