#[cfg(any(feature = "wgsl-in", feature = "wgsl-out"))]
use crate::common::wgsl::TypeContext;
use crate::proc::TypeResolution;
use crate::{Handle, Scalar, Type, TypeInner, UniqueArena};
use core::fmt;
pub struct DiagnosticDebug<T>(pub T);
impl fmt::Debug for DiagnosticDebug<(Handle<Type>, &UniqueArena<Type>)> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let (handle, ctx) = self.0;
#[cfg(any(feature = "wgsl-in", feature = "wgsl-out"))]
ctx.write_type(handle, f)?;
#[cfg(not(any(feature = "wgsl-in", feature = "wgsl-out")))]
{
let _ = ctx;
write!(f, "{handle:?}")?;
}
Ok(())
}
}
impl fmt::Debug for DiagnosticDebug<(&TypeInner, &UniqueArena<Type>)> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let (inner, ctx) = self.0;
#[cfg(any(feature = "wgsl-in", feature = "wgsl-out"))]
ctx.write_type_inner(inner, f)?;
#[cfg(not(any(feature = "wgsl-in", feature = "wgsl-out")))]
{
let _ = ctx;
write!(f, "{inner:?}")?;
}
Ok(())
}
}
impl fmt::Debug for DiagnosticDebug<(&TypeResolution, &UniqueArena<Type>)> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let (resolution, ctx) = self.0;
#[cfg(any(feature = "wgsl-in", feature = "wgsl-out"))]
ctx.write_type_resolution(resolution, f)?;
#[cfg(not(any(feature = "wgsl-in", feature = "wgsl-out")))]
{
let _ = ctx;
write!(f, "{resolution:?}")?;
}
Ok(())
}
}
impl fmt::Debug for DiagnosticDebug<Scalar> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let scalar = self.0;
#[cfg(any(feature = "wgsl-in", feature = "wgsl-out"))]
f.write_str(&crate::common::wgsl::TryToWgsl::to_wgsl_for_diagnostics(
scalar,
))?;
#[cfg(not(any(feature = "wgsl-in", feature = "wgsl-out")))]
write!(f, "{scalar:?}")?;
Ok(())
}
}
pub trait ForDebug: Sized {
fn for_debug(self) -> DiagnosticDebug<Self> {
DiagnosticDebug(self)
}
}
impl ForDebug for Scalar {}
pub trait ForDebugWithTypes: Sized {
fn for_debug(self, types: &UniqueArena<Type>) -> DiagnosticDebug<(Self, &UniqueArena<Type>)> {
DiagnosticDebug((self, types))
}
}
impl ForDebugWithTypes for Handle<Type> {}
impl ForDebugWithTypes for &TypeInner {}
impl ForDebugWithTypes for &TypeResolution {}