Skip to content

Commit 7e3a7b5

Browse files
committed
compile_codegen: make SpirvBuilderError use thiserror
1 parent dd056a3 commit 7e3a7b5

File tree

3 files changed

+49
-64
lines changed

3 files changed

+49
-64
lines changed

Cargo.lock

Lines changed: 32 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/spirv-builder/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,6 @@ memchr = "2.4"
4141
raw-string = "0.3.5"
4242
serde = { version = "1.0", features = ["derive"] }
4343
serde_json = "1.0"
44+
thiserror = "2.0.12"
4445

4546
notify = { version = "7.0", optional = true }

crates/spirv-builder/src/lib.rs

Lines changed: 16 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -81,79 +81,42 @@ use serde::Deserialize;
8181
use std::borrow::Borrow;
8282
use std::collections::HashMap;
8383
use std::env;
84-
use std::error::Error;
85-
use std::fmt;
8684
use std::fs::File;
8785
use std::io::BufReader;
8886
use std::path::{Path, PathBuf};
8987
use std::process::{Command, Stdio};
88+
use thiserror::Error;
9089

9190
pub use rustc_codegen_spirv_types::Capability;
9291
pub use rustc_codegen_spirv_types::{CompileResult, ModuleResult};
9392

94-
#[derive(Debug)]
93+
#[derive(Debug, Error)]
9594
#[non_exhaustive]
9695
pub enum SpirvBuilderError {
96+
#[error("`target` must be set, for example `spirv-unknown-vulkan1.2`")]
97+
NoTargetSet,
98+
#[error("expected `{SPIRV_TARGET_PREFIX}...` target, found `{target}`")]
9799
NonSpirvTarget { target: String },
100+
#[error("SPIR-V target `{SPIRV_TARGET_PREFIX}-{target_env}` is not supported")]
98101
UnsupportedSpirvTargetEnv { target_env: String },
102+
#[error("`path_to_crate` must be set")]
103+
NoCratePathSet,
104+
#[error("crate path {0} does not exist")]
99105
CratePathDoesntExist(PathBuf),
106+
#[error("build failed")]
100107
BuildFailed,
108+
#[error("multi-module build cannot be used with print_metadata = MetadataPrintout::Full")]
101109
MultiModuleWithPrintMetadata,
110+
#[error("watching within build scripts will prevent build completion")]
102111
WatchWithPrintMetadata,
103-
MetadataFileMissing(std::io::Error),
104-
MetadataFileMalformed(serde_json::Error),
112+
#[error("multi-module metadata file missing")]
113+
MetadataFileMissing(#[from] std::io::Error),
114+
#[error("unable to parse multi-module metadata file")]
115+
MetadataFileMalformed(#[from] serde_json::Error),
105116
}
106117

107118
const SPIRV_TARGET_PREFIX: &str = "spirv-unknown-";
108119

109-
impl fmt::Display for SpirvBuilderError {
110-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
111-
match self {
112-
Self::NonSpirvTarget { target } => {
113-
write!(
114-
f,
115-
"expected `{SPIRV_TARGET_PREFIX}...` target, found `{target}`"
116-
)
117-
}
118-
Self::UnsupportedSpirvTargetEnv { target_env } if target_env.starts_with("opencl") => {
119-
write!(
120-
f,
121-
"OpenCL targets like `{SPIRV_TARGET_PREFIX}-{target_env}` are not supported"
122-
)
123-
}
124-
Self::UnsupportedSpirvTargetEnv { target_env } if target_env.starts_with("webgpu") => {
125-
write!(
126-
f,
127-
"WebGPU targets like `{SPIRV_TARGET_PREFIX}-{target_env}` are not supported, \
128-
consider using `{SPIRV_TARGET_PREFIX}-vulkan1.0` instead"
129-
)
130-
}
131-
Self::UnsupportedSpirvTargetEnv { target_env } => {
132-
write!(
133-
f,
134-
"SPIR-V target `{SPIRV_TARGET_PREFIX}-{target_env}` is not supported"
135-
)
136-
}
137-
Self::CratePathDoesntExist(path) => {
138-
write!(f, "crate path {} does not exist", path.display())
139-
}
140-
Self::BuildFailed => f.write_str("build failed"),
141-
Self::MultiModuleWithPrintMetadata => f.write_str(
142-
"multi-module build cannot be used with print_metadata = MetadataPrintout::Full",
143-
),
144-
Self::WatchWithPrintMetadata => {
145-
f.write_str("watching within build scripts will prevent build completion")
146-
}
147-
Self::MetadataFileMissing(_) => f.write_str("multi-module metadata file missing"),
148-
Self::MetadataFileMalformed(_) => {
149-
f.write_str("unable to parse multi-module metadata file")
150-
}
151-
}
152-
}
153-
}
154-
155-
impl Error for SpirvBuilderError {}
156-
157120
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
158121
pub enum MetadataPrintout {
159122
/// Print no cargo metadata.

0 commit comments

Comments
 (0)