From 840f61fc2bf25814f98aadb218734cea4b90548f Mon Sep 17 00:00:00 2001 From: Andrew Gallant Date: Tue, 25 Jun 2024 12:41:40 -0400 Subject: [PATCH] uv-resolver: re-arrange some code Previously, we had Lock and LockWire impl blocks inter-mixed. This bugs me a bit, so I've just shuffled things around so that we have Lock, impl Lock, LockWire and then impl LockWire. No changes are otherwise made to the code here. --- crates/uv-resolver/src/lock.rs | 98 +++++++++++++++++----------------- 1 file changed, 48 insertions(+), 50 deletions(-) diff --git a/crates/uv-resolver/src/lock.rs b/crates/uv-resolver/src/lock.rs index 7ca42569f..c6a500d7d 100644 --- a/crates/uv-resolver/src/lock.rs +++ b/crates/uv-resolver/src/lock.rs @@ -392,56 +392,6 @@ impl Lock { Ok(Resolution::new(map, diagnostics)) } - /// Returns the distribution with the given name. If there are multiple - /// matching distributions, then an error is returned. If there are no - /// matching distributions, then `Ok(None)` is returned. - fn find_by_name(&self, name: &PackageName) -> Result, String> { - let mut found_dist = None; - for dist in &self.distributions { - if &dist.id.name == name { - if found_dist.is_some() { - return Err(format!("found multiple distributions matching `{name}`")); - } - found_dist = Some(dist); - } - } - Ok(found_dist) - } - - fn find_by_id(&self, id: &DistributionId) -> &Distribution { - let index = *self.by_id.get(id).expect("locked distribution for ID"); - let dist = self - .distributions - .get(index) - .expect("valid index for distribution"); - dist - } -} - -#[derive(Clone, Debug, serde::Deserialize)] -struct LockWire { - version: u32, - #[serde(rename = "distribution")] - distributions: Vec, - #[serde(rename = "requires-python")] - requires_python: Option, -} - -impl From for LockWire { - fn from(lock: Lock) -> LockWire { - LockWire { - version: lock.version, - distributions: lock - .distributions - .into_iter() - .map(DistributionWire::from) - .collect(), - requires_python: lock.requires_python, - } - } -} - -impl Lock { /// Returns the TOML representation of this lock file. pub fn to_toml(&self) -> anyhow::Result { // We construct a TOML document manually instead of going through Serde to enable @@ -535,6 +485,54 @@ impl Lock { doc.insert("distribution", Item::ArrayOfTables(distributions)); Ok(doc.to_string()) } + + /// Returns the distribution with the given name. If there are multiple + /// matching distributions, then an error is returned. If there are no + /// matching distributions, then `Ok(None)` is returned. + fn find_by_name(&self, name: &PackageName) -> Result, String> { + let mut found_dist = None; + for dist in &self.distributions { + if &dist.id.name == name { + if found_dist.is_some() { + return Err(format!("found multiple distributions matching `{name}`")); + } + found_dist = Some(dist); + } + } + Ok(found_dist) + } + + fn find_by_id(&self, id: &DistributionId) -> &Distribution { + let index = *self.by_id.get(id).expect("locked distribution for ID"); + let dist = self + .distributions + .get(index) + .expect("valid index for distribution"); + dist + } +} + +#[derive(Clone, Debug, serde::Deserialize)] +struct LockWire { + version: u32, + #[serde(rename = "distribution")] + distributions: Vec, + #[serde(rename = "requires-python")] + requires_python: Option, +} + +impl From for LockWire { + fn from(lock: Lock) -> LockWire { + LockWire { + version: lock.version, + distributions: lock + .distributions + .into_iter() + .map(DistributionWire::from) + .collect(), + requires_python: lock.requires_python, + } + } } impl TryFrom for Lock {