|
1 | | -use alloc::{boxed::Box, string::String, vec::Vec}; |
| 1 | +use alloc::{boxed::Box, string::String}; |
2 | 2 | use core::{error::Error, fmt}; |
3 | 3 |
|
4 | 4 | #[derive(Clone, Debug)] |
@@ -43,14 +43,88 @@ impl fmt::Display for ShaderError<crate::WithSpan<crate::valid::ValidationError> |
43 | 43 | let label = self.label.as_deref().unwrap_or_default(); |
44 | 44 | let files = SimpleFile::new(label, &self.source); |
45 | 45 | let config = term::Config::default(); |
46 | | - let mut writer = term::termcolor::NoColor::new(Vec::new()); |
47 | | - term::emit(&mut writer, &config, &files, &self.inner.diagnostic()) |
| 46 | + |
| 47 | + let writer = { |
| 48 | + let mut writer = DiagnosticBuffer::new(); |
| 49 | + term::emit( |
| 50 | + writer.inner_mut(), |
| 51 | + &config, |
| 52 | + &files, |
| 53 | + &self.inner.diagnostic(), |
| 54 | + ) |
48 | 55 | .expect("cannot write error"); |
49 | | - write!( |
50 | | - f, |
51 | | - "\nShader validation {}", |
52 | | - String::from_utf8_lossy(&writer.into_inner()) |
| 56 | + writer.into_string() |
| 57 | + }; |
| 58 | + |
| 59 | + write!(f, "\nShader validation {}", writer) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +cfg_if::cfg_if! { |
| 64 | + if #[cfg(feature = "termcolor")] { |
| 65 | + type DiagnosticBufferInner = codespan_reporting::term::termcolor::NoColor<alloc::vec::Vec<u8>>; |
| 66 | + pub(crate) use codespan_reporting::term::termcolor::WriteColor as _ErrorWrite; |
| 67 | + } else if #[cfg(feature = "stderr")] { |
| 68 | + type DiagnosticBufferInner = alloc::vec::Vec<u8>; |
| 69 | + pub(crate) use std::io::Write as _ErrorWrite; |
| 70 | + } else { |
| 71 | + type DiagnosticBufferInner = String; |
| 72 | + pub(crate) use core::fmt::Write as _ErrorWrite; |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +// Using this indirect export to avoid duplicating the expect(...) for all three cases above. |
| 77 | +#[cfg_attr( |
| 78 | + not(any(feature = "spv-in", feature = "glsl-in")), |
| 79 | + expect( |
| 80 | + unused_imports, |
| 81 | + reason = "only need `ErrorWrite` with an appropriate front-end." |
| 82 | + ) |
| 83 | +)] |
| 84 | +pub(crate) use _ErrorWrite as ErrorWrite; |
| 85 | + |
| 86 | +pub(crate) struct DiagnosticBuffer { |
| 87 | + inner: DiagnosticBufferInner, |
| 88 | +} |
| 89 | + |
| 90 | +impl DiagnosticBuffer { |
| 91 | + #[cfg_attr( |
| 92 | + not(feature = "termcolor"), |
| 93 | + expect( |
| 94 | + clippy::missing_const_for_fn, |
| 95 | + reason = "`NoColor::new` isn't `const`, but other `inner`s are." |
53 | 96 | ) |
| 97 | + )] |
| 98 | + pub fn new() -> Self { |
| 99 | + cfg_if::cfg_if! { |
| 100 | + if #[cfg(feature = "termcolor")] { |
| 101 | + let inner = codespan_reporting::term::termcolor::NoColor::new(alloc::vec::Vec::new()); |
| 102 | + } else if #[cfg(feature = "stderr")] { |
| 103 | + let inner = alloc::vec::Vec::new(); |
| 104 | + } else { |
| 105 | + let inner = String::new(); |
| 106 | + } |
| 107 | + }; |
| 108 | + |
| 109 | + Self { inner } |
| 110 | + } |
| 111 | + |
| 112 | + pub fn inner_mut(&mut self) -> &mut DiagnosticBufferInner { |
| 113 | + &mut self.inner |
| 114 | + } |
| 115 | + |
| 116 | + pub fn into_string(self) -> String { |
| 117 | + let Self { inner } = self; |
| 118 | + |
| 119 | + cfg_if::cfg_if! { |
| 120 | + if #[cfg(feature = "termcolor")] { |
| 121 | + String::from_utf8(inner.into_inner()).unwrap() |
| 122 | + } else if #[cfg(feature = "stderr")] { |
| 123 | + String::from_utf8(inner).unwrap() |
| 124 | + } else { |
| 125 | + inner |
| 126 | + } |
| 127 | + } |
54 | 128 | } |
55 | 129 | } |
56 | 130 |
|
|
0 commit comments