|
| 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 | +} |
0 commit comments