Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve sources deserialization errors #8308

Merged
merged 1 commit into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 6 additions & 8 deletions crates/uv-distribution/src/metadata/requires_dist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,8 @@ mod test {
|
8 | tqdm = true
| ^^^^
invalid type: boolean `true`, expected an array or map
invalid type: boolean `true`, expected a single source (as a map) or list of sources

"###);
}

Expand All @@ -301,7 +302,6 @@ mod test {
8 | tqdm = { git = "https://github.com/tqdm/tqdm", rev = "baaaaaab", tag = "v1.0.0" }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
expected at most one of `rev`, `tag`, or `branch`

"###);
}

Expand All @@ -319,10 +319,10 @@ mod test {
"#};

assert_snapshot!(format_err(input).await, @r###"
error: TOML parse error at line 8, column 8
error: TOML parse error at line 8, column 48
|
8 | tqdm = { git = "https://github.com/tqdm/tqdm", ref = "baaaaaab" }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^
unknown field `ref`, expected one of `git`, `subdirectory`, `rev`, `tag`, `branch`, `url`, `path`, `editable`, `index`, `workspace`, `marker`
"###);
}
Expand Down Expand Up @@ -399,14 +399,12 @@ mod test {
"#};

assert_snapshot!(format_err(input).await, @r###"
error: TOML parse error at line 8, column 8
error: TOML parse error at line 8, column 16
|
8 | tqdm = { url = "§invalid#+#*Ä" }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^
invalid value: string "§invalid#+#*Ä", expected relative URL without a base


in `url`
"###);
}

Expand Down
1 change: 0 additions & 1 deletion crates/uv-workspace/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ rustc-hash = { workspace = true }
same-file = { workspace = true }
schemars = { workspace = true, optional = true }
serde = { workspace = true, features = ["derive"] }
serde-untagged = { workspace = true }
thiserror = { workspace = true }
tokio = { workspace = true }
toml = { workspace = true }
Expand Down
36 changes: 32 additions & 4 deletions crates/uv-workspace/src/pyproject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

use glob::Pattern;
use owo_colors::OwoColorize;
use serde::de::SeqAccess;
use serde::{de::IntoDeserializer, Deserialize, Deserializer, Serialize};
use std::ops::Deref;
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -502,10 +503,37 @@ impl<'de> serde::de::Deserialize<'de> for SourcesWire {
where
D: Deserializer<'de>,
{
serde_untagged::UntaggedEnumVisitor::new()
.map(|map| map.deserialize().map(SourcesWire::One))
.seq(|seq| seq.deserialize().map(SourcesWire::Many))
.deserialize(deserializer)
struct Visitor;

impl<'de> serde::de::Visitor<'de> for Visitor {
type Value = SourcesWire;

fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("a single source (as a map) or list of sources")
}

fn visit_seq<A>(self, seq: A) -> Result<Self::Value, A::Error>
where
A: SeqAccess<'de>,
{
let sources = serde::de::Deserialize::deserialize(
serde::de::value::SeqAccessDeserializer::new(seq),
)?;
Ok(SourcesWire::Many(sources))
}

fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error>
where
M: serde::de::MapAccess<'de>,
{
let source = serde::de::Deserialize::deserialize(
serde::de::value::MapAccessDeserializer::new(&mut map),
)?;
Ok(SourcesWire::One(source))
}
}

deserializer.deserialize_any(Visitor)
}
}

Expand Down
Loading