Skip to content

Commit f1ef4e4

Browse files
committed
Parse and validate variant metadata
1 parent 298b85d commit f1ef4e4

8 files changed

Lines changed: 351 additions & 5 deletions

File tree

‎crates/uv-client/src/lib.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,5 @@ mod remote_metadata;
3131
mod retry;
3232
mod rkyvutil;
3333
mod tls;
34+
#[cfg(test)]
35+
mod variants_json_tests;
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
use insta::{assert_json_snapshot, assert_snapshot};
2+
use serde_json::json;
3+
4+
use uv_distribution_types::VariantsJsonContent;
5+
6+
const VARIANT_SCHEMA: &str = "https://variants-schema.wheelnext.dev/peps/825/v0.1.1.json";
7+
8+
#[test]
9+
fn parse_variants_json() -> Result<(), Box<dyn std::error::Error>> {
10+
let metadata = json!({
11+
"$schema": VARIANT_SCHEMA,
12+
"default-priorities": {"namespace": ["nvidia", "x86_64"]},
13+
"variants": {
14+
"gpu": {"nvidia": {"cuda_version_lower_bound": ["13.0", "12.8"]}, "unused": {}},
15+
"cpu": {"x86_64": {"level": ["v3"]}},
16+
"null": {"unused": {}}
17+
}
18+
});
19+
let parsed: VariantsJsonContent = serde_json::from_value(metadata)?;
20+
21+
assert_json_snapshot!(parsed, @r#"
22+
{
23+
"$schema": "https://variants-schema.wheelnext.dev/peps/825/v0.1.1.json",
24+
"default-priorities": {
25+
"namespace": [
26+
"nvidia",
27+
"x86_64"
28+
]
29+
},
30+
"variants": {
31+
"cpu": {
32+
"x86_64": {
33+
"level": [
34+
"v3"
35+
]
36+
}
37+
},
38+
"gpu": {
39+
"nvidia": {
40+
"cuda_version_lower_bound": [
41+
"12.8",
42+
"13.0"
43+
]
44+
}
45+
},
46+
"null": {}
47+
}
48+
}
49+
"#);
50+
Ok(())
51+
}
52+
53+
#[test]
54+
fn reject_invalid_variants_json() {
55+
let metadata = json!({
56+
"$schema": VARIANT_SCHEMA,
57+
"default-priorities": {"namespace": ["cpu"]},
58+
"variants": {"fast": {"cpu": {"level": ["v2", "v3"]}}, "null": {}}
59+
});
60+
let mut invalid = metadata.clone();
61+
invalid["$schema"] = json!("https://variants-schema.wheelnext.dev/peps/825/v0.1.0.json");
62+
let error = serde_json::from_value::<VariantsJsonContent>(invalid)
63+
.expect_err("metadata should be rejected");
64+
assert_snapshot!(error, @"Unsupported variant metadata schema: https://variants-schema.wheelnext.dev/peps/825/v0.1.0.json");
65+
66+
let mut invalid = metadata.clone();
67+
invalid["default-priorities"]["namespace"] = json!([]);
68+
let error = serde_json::from_value::<VariantsJsonContent>(invalid)
69+
.expect_err("metadata should be rejected");
70+
assert_snapshot!(error, @"Variant namespace priorities must be non-empty and unique");
71+
72+
let mut invalid = metadata.clone();
73+
invalid["default-priorities"]["namespace"] = json!(["cpu", "cpu"]);
74+
let error = serde_json::from_value::<VariantsJsonContent>(invalid)
75+
.expect_err("metadata should be rejected");
76+
assert_snapshot!(error, @"Variant namespace priorities must be non-empty and unique");
77+
78+
let mut invalid = metadata.clone();
79+
invalid["default-priorities"]["namespace"] = json!(["CPU"]);
80+
let error = serde_json::from_value::<VariantsJsonContent>(invalid)
81+
.expect_err("metadata should be rejected");
82+
assert_snapshot!(error, @"Invalid variant namespace: `CPU`");
83+
84+
let mut invalid = metadata.clone();
85+
invalid["default-priorities"]["namespace"] = json!(["gpu"]);
86+
let error = serde_json::from_value::<VariantsJsonContent>(invalid)
87+
.expect_err("metadata should be rejected");
88+
assert_snapshot!(error, @"Variant namespace `cpu` is missing from default-priorities.namespace");
89+
90+
let mut invalid = metadata.clone();
91+
invalid["variants"] = json!({"Bad": {}});
92+
let error = serde_json::from_value::<VariantsJsonContent>(invalid)
93+
.expect_err("metadata should be rejected");
94+
assert_snapshot!(error, @"Variant label must contain only lowercase ASCII letters, digits, underscores, and periods, not `B`");
95+
96+
let mut invalid = metadata.clone();
97+
invalid["variants"]["fast"] = json!({"CPU": {"level": ["v3"]}});
98+
let error = serde_json::from_value::<VariantsJsonContent>(invalid)
99+
.expect_err("metadata should be rejected");
100+
assert_snapshot!(error, @"Invalid variant namespace: `CPU`");
101+
102+
let mut invalid = metadata.clone();
103+
invalid["variants"]["fast"]["cpu"] = json!({"bad.feature": ["v3"]});
104+
let error = serde_json::from_value::<VariantsJsonContent>(invalid)
105+
.expect_err("metadata should be rejected");
106+
assert_snapshot!(error, @"Invalid variant feature: `bad.feature`");
107+
108+
let mut invalid = metadata.clone();
109+
invalid["variants"]["fast"]["cpu"]["level"] = json!(["v 3"]);
110+
let error = serde_json::from_value::<VariantsJsonContent>(invalid)
111+
.expect_err("metadata should be rejected");
112+
assert_snapshot!(error, @"Invalid variant value: `v 3`");
113+
114+
let mut invalid = metadata.clone();
115+
invalid["variants"]["fast"]["cpu"]["level"] = json!([]);
116+
let error = serde_json::from_value::<VariantsJsonContent>(invalid)
117+
.expect_err("metadata should be rejected");
118+
assert_snapshot!(error, @"Variant feature `cpu :: level` must have at least one value");
119+
120+
let mut invalid = metadata.clone();
121+
invalid["variants"]["fast"]["cpu"]["level"] = json!(["v3", "v2", "v3"]);
122+
let error = serde_json::from_value::<VariantsJsonContent>(invalid)
123+
.expect_err("metadata should be rejected");
124+
assert_snapshot!(error, @"Variant feature `cpu :: level` must not contain duplicate values");
125+
126+
let mut invalid = metadata.clone();
127+
invalid["variants"]["null"] = json!({"cpu": {"level": ["v3"]}});
128+
let error = serde_json::from_value::<VariantsJsonContent>(invalid)
129+
.expect_err("metadata should be rejected");
130+
assert_snapshot!(error, @"The null variant must not have properties");
131+
132+
let mut invalid = metadata;
133+
invalid["default-priorities"]["unknown"] = json!([]);
134+
let error = serde_json::from_value::<VariantsJsonContent>(invalid)
135+
.expect_err("metadata should be rejected");
136+
assert_snapshot!(error, @"unknown field `unknown`, expected `namespace`");
137+
}

‎crates/uv-distribution-filename/src/variant_label.rs‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use std::fmt::{Display, Formatter};
22
use std::str::FromStr;
33

4+
use serde::{Deserialize, Deserializer};
5+
46
use uv_small_str::SmallString;
57

68
/// An invalid wheel variant label.
@@ -23,13 +25,21 @@ pub enum InvalidVariantLabel {
2325
Hash,
2426
Ord,
2527
PartialOrd,
28+
serde::Serialize,
2629
rkyv::Archive,
2730
rkyv::Deserialize,
2831
rkyv::Serialize,
2932
)]
3033
#[rkyv(derive(Debug))]
34+
#[serde(transparent)]
3135
pub struct VariantLabel(SmallString);
3236

37+
impl VariantLabel {
38+
pub fn as_str(&self) -> &str {
39+
&self.0
40+
}
41+
}
42+
3343
impl FromStr for VariantLabel {
3444
type Err = InvalidVariantLabel;
3545

@@ -47,6 +57,16 @@ impl FromStr for VariantLabel {
4757
}
4858
}
4959

60+
impl<'de> Deserialize<'de> for VariantLabel {
61+
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
62+
where
63+
D: Deserializer<'de>,
64+
{
65+
let label = String::deserialize(deserializer)?;
66+
Self::from_str(&label).map_err(serde::de::Error::custom)
67+
}
68+
}
69+
5070
impl Display for VariantLabel {
5171
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
5272
Display::fmt(&self.0, f)

‎crates/uv-distribution-types/src/lib.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ pub use crate::specified_requirement::*;
9696
pub use crate::status_code_strategy::*;
9797
pub use crate::traits::*;
9898
pub use crate::variant_json::*;
99+
pub use crate::variants_json::*;
99100

100101
mod annotation;
101102
mod any;
@@ -131,6 +132,7 @@ mod specified_requirement;
131132
mod status_code_strategy;
132133
mod traits;
133134
mod variant_json;
135+
mod variants_json;
134136

135137
#[derive(Debug, Clone)]
136138
pub enum VersionOrUrlRef<'a, T: Pep508Url = VerbatimUrl> {
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
use std::collections::{BTreeMap, HashSet};
2+
3+
use serde::{Deserialize, Serialize};
4+
5+
use uv_distribution_filename::VariantLabel;
6+
7+
/// The schema for the PEP 825 draft. Compatibility between draft versions is not guaranteed.
8+
const VARIANT_SCHEMA: &str = "https://variants-schema.wheelnext.dev/peps/825/v0.1.1.json";
9+
10+
/// An error parsing a variant metadata file.
11+
pub type VariantsJsonParseError = serde_json::Error;
12+
13+
/// Variant properties, grouped by namespace and feature.
14+
type VariantProperties = BTreeMap<String, BTreeMap<String, Vec<String>>>;
15+
16+
/// Index-level metadata for wheel variants, as defined by PEP 825.
17+
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
18+
#[serde(rename_all = "kebab-case", try_from = "VariantsJsonWire")]
19+
pub struct VariantsJsonContent {
20+
#[serde(rename = "$schema")]
21+
schema: String,
22+
default_priorities: DefaultPriorities,
23+
variants: BTreeMap<VariantLabel, VariantProperties>,
24+
}
25+
26+
impl VariantsJsonContent {
27+
/// Parse and validate the contents of a variant metadata file.
28+
pub fn from_slice(contents: &[u8]) -> Result<Self, VariantsJsonParseError> {
29+
serde_json::from_slice(contents)
30+
}
31+
}
32+
33+
/// Default priorities for the namespaces used by a release's variants.
34+
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
35+
#[serde(deny_unknown_fields)]
36+
struct DefaultPriorities {
37+
namespace: Vec<String>,
38+
}
39+
40+
#[derive(Deserialize)]
41+
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
42+
struct VariantsJsonWire {
43+
#[serde(rename = "$schema")]
44+
schema: String,
45+
default_priorities: DefaultPriorities,
46+
variants: BTreeMap<VariantLabel, VariantProperties>,
47+
}
48+
49+
#[derive(Debug, thiserror::Error)]
50+
enum VariantMetadataError {
51+
#[error("Unsupported variant metadata schema: {0}")]
52+
UnsupportedSchema(String),
53+
#[error("Invalid variant namespace: `{0}`")]
54+
InvalidNamespace(String),
55+
#[error("Invalid variant feature: `{0}`")]
56+
InvalidFeature(String),
57+
#[error("Invalid variant value: `{0}`")]
58+
InvalidValue(String),
59+
#[error("Variant namespace priorities must be non-empty and unique")]
60+
NamespacePriorities,
61+
#[error("Variant namespace `{0}` is missing from default-priorities.namespace")]
62+
MissingNamespace(String),
63+
#[error("Variant feature `{0} :: {1}` must have at least one value")]
64+
EmptyValues(String, String),
65+
#[error("Variant feature `{0} :: {1}` must not contain duplicate values")]
66+
DuplicateValues(String, String),
67+
#[error("The null variant must not have properties")]
68+
NullProperties,
69+
}
70+
71+
impl TryFrom<VariantsJsonWire> for VariantsJsonContent {
72+
type Error = VariantMetadataError;
73+
74+
fn try_from(mut wire: VariantsJsonWire) -> Result<Self, Self::Error> {
75+
if wire.schema != VARIANT_SCHEMA {
76+
return Err(VariantMetadataError::UnsupportedSchema(wire.schema));
77+
}
78+
79+
let namespaces = &wire.default_priorities.namespace;
80+
if namespaces.is_empty()
81+
|| namespaces.iter().collect::<HashSet<_>>().len() != namespaces.len()
82+
{
83+
return Err(VariantMetadataError::NamespacePriorities);
84+
}
85+
for namespace in namespaces {
86+
if !valid_component(namespace, false) {
87+
return Err(VariantMetadataError::InvalidNamespace(namespace.clone()));
88+
}
89+
}
90+
91+
for (label, properties) in &mut wire.variants {
92+
for (namespace, features) in properties.iter_mut() {
93+
if !valid_component(namespace, false) {
94+
return Err(VariantMetadataError::InvalidNamespace(namespace.clone()));
95+
}
96+
if !features.is_empty() && !namespaces.contains(namespace) {
97+
return Err(VariantMetadataError::MissingNamespace(namespace.clone()));
98+
}
99+
for (feature, values) in features {
100+
if !valid_component(feature, false) {
101+
return Err(VariantMetadataError::InvalidFeature(feature.clone()));
102+
}
103+
if values.is_empty() {
104+
return Err(VariantMetadataError::EmptyValues(
105+
namespace.clone(),
106+
feature.clone(),
107+
));
108+
}
109+
for value in values.iter() {
110+
if !valid_component(value, true) {
111+
return Err(VariantMetadataError::InvalidValue(value.clone()));
112+
}
113+
}
114+
values.sort_unstable();
115+
if values.windows(2).any(|pair| pair[0] == pair[1]) {
116+
return Err(VariantMetadataError::DuplicateValues(
117+
namespace.clone(),
118+
feature.clone(),
119+
));
120+
}
121+
if label.as_str() == "null" {
122+
return Err(VariantMetadataError::NullProperties);
123+
}
124+
}
125+
}
126+
properties.retain(|_, features| !features.is_empty());
127+
}
128+
129+
Ok(Self {
130+
schema: wire.schema,
131+
default_priorities: wire.default_priorities,
132+
variants: wire.variants,
133+
})
134+
}
135+
}
136+
137+
fn valid_component(value: &str, allow_period: bool) -> bool {
138+
!value.is_empty()
139+
&& value.bytes().all(|character| {
140+
character.is_ascii_lowercase()
141+
|| character.is_ascii_digit()
142+
|| character == b'_'
143+
|| (allow_period && character == b'.')
144+
})
145+
}

‎crates/uv-resolver/src/resolver/index.rs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::hash::BuildHasherDefault;
22
use std::sync::Arc;
33

44
use rustc_hash::FxHasher;
5-
use uv_distribution_types::IndexUrl;
5+
use uv_distribution_types::{IndexUrl, VariantsJsonContent, VariantsJsonParseError};
66
use uv_normalize::PackageName;
77
use uv_once_map::{RegisteredEntry, RegisteredOnceMap};
88
use uv_resolver_types::DistributionMetadataIndex;
@@ -25,11 +25,11 @@ struct SharedInMemoryIndex {
2525
/// A map from a concrete distribution to its metadata.
2626
distributions: DistributionMetadataIndex,
2727

28-
/// A map from an index variant metadata file to its contents.
28+
/// A map from an index variant metadata file to its parsed contents.
2929
variants_json: FxOnceMap<RegistryVariantsJson, Arc<VariantsJsonResponse>>,
3030
}
3131

32-
pub(crate) type VariantsJsonResponse = Option<Vec<u8>>;
32+
pub(crate) type VariantsJsonResponse = Option<Result<VariantsJsonContent, VariantsJsonParseError>>;
3333
pub(crate) type FxOnceMap<K, V> = RegisteredOnceMap<K, V, BuildHasherDefault<FxHasher>>;
3434
pub(crate) type FxRegisteredEntry<'a, K, V> =
3535
RegisteredEntry<'a, K, V, BuildHasherDefault<FxHasher>>;

0 commit comments

Comments
 (0)
⌂ 换网址