Skip to content
This repository was archived by the owner on Apr 6, 2026. It is now read-only.

Commit 5e48ab9

Browse files
authored
Provide better errors when deserializing build.toml (#145)
Before this change when there was an error in `build.toml`, one would get a generic error: ``` Error: Cannot parse TOML in /Users/daniel/git/relu-metal/build.toml Caused by: data did not match any variant of untagged enum BuildCompat ``` However, what we want is to give the error when trying to parse as the V2 format, since the V1 format is only there for backwards compat/updates. With this change the error of parsing as V2 is bubbled up. For instance: ``` Error: Cannot parse TOML in /Users/daniel/git/relu-metal/build.toml Caused by: Unknown variant metal. Expected one of cuda, rocm ```
1 parent bb1edfe commit 5e48ab9

3 files changed

Lines changed: 56 additions & 2 deletions

File tree

build2cmake/Cargo.lock

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

build2cmake/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ minijinja-embed = "2.5"
1919
rand = "0.8"
2020
serde = { version = "1", features = ["derive"] }
2121
serde_json = "1"
22+
serde-value = "0.7"
2223
toml = "0.8"
2324

2425
[build-dependencies]

build2cmake/src/config/mod.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,33 @@ use serde::Deserialize;
44
pub mod v1;
55

66
mod v2;
7+
use serde_value::Value;
78
pub use v2::{Backend, Build, Dependencies, Kernel, Torch};
89

9-
#[derive(Debug, Deserialize)]
10-
#[serde(untagged)]
10+
#[derive(Debug)]
1111
pub enum BuildCompat {
1212
V1(v1::Build),
1313
V2(Build),
1414
}
1515

16+
impl<'de> Deserialize<'de> for BuildCompat {
17+
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
18+
where
19+
D: serde::Deserializer<'de>,
20+
{
21+
let value = Value::deserialize(deserializer)?;
22+
23+
match v1::Build::deserialize(value.clone()) {
24+
Ok(v1_build) => Ok(BuildCompat::V1(v1_build)),
25+
Err(_) => {
26+
let v2_build: Build =
27+
Build::deserialize(value).map_err(serde::de::Error::custom)?;
28+
Ok(BuildCompat::V2(v2_build))
29+
}
30+
}
31+
}
32+
}
33+
1634
impl TryFrom<BuildCompat> for Build {
1735
type Error = eyre::Error;
1836

0 commit comments

Comments
 (0)