oxiui_render_wgpu/
error.rs1use oxiui_core::UiError;
8
9#[derive(Clone, Debug, PartialEq)]
13pub enum GpuErrorKind {
14 DeviceLost,
16 OutOfMemory,
18 ShaderCompile,
20 SurfaceLost,
22}
23
24pub fn map_gpu_error(kind: GpuErrorKind, detail: String) -> UiError {
33 match kind {
34 GpuErrorKind::DeviceLost => {
35 UiError::Render(format!("GPU {:?}: {detail}", GpuErrorKind::DeviceLost))
36 }
37 GpuErrorKind::OutOfMemory => {
38 UiError::Render(format!("GPU {:?}: {detail}", GpuErrorKind::OutOfMemory))
39 }
40 GpuErrorKind::SurfaceLost => {
41 UiError::Render(format!("GPU {:?}: {detail}", GpuErrorKind::SurfaceLost))
42 }
43 GpuErrorKind::ShaderCompile => UiError::Unsupported(format!("shader compile: {detail}")),
44 }
45}
46
47#[cfg(test)]
50mod tests {
51 use super::*;
52
53 #[test]
54 fn map_gpu_error_all_kinds() {
55 let device_lost = map_gpu_error(GpuErrorKind::DeviceLost, "reset".to_string());
56 assert!(
57 matches!(device_lost, UiError::Render(ref s) if s.contains("DeviceLost")),
58 "expected Render(DeviceLost …), got {device_lost:?}"
59 );
60
61 let oom = map_gpu_error(GpuErrorKind::OutOfMemory, "vram".to_string());
62 assert!(
63 matches!(oom, UiError::Render(ref s) if s.contains("OutOfMemory")),
64 "expected Render(OutOfMemory …), got {oom:?}"
65 );
66
67 let surface = map_gpu_error(GpuErrorKind::SurfaceLost, "resize".to_string());
68 assert!(
69 matches!(surface, UiError::Render(ref s) if s.contains("SurfaceLost")),
70 "expected Render(SurfaceLost …), got {surface:?}"
71 );
72
73 let shader = map_gpu_error(GpuErrorKind::ShaderCompile, "syntax error".to_string());
74 assert!(
75 matches!(shader, UiError::Unsupported(ref s) if s.contains("shader compile")),
76 "expected Unsupported(shader compile …), got {shader:?}"
77 );
78 }
79}