Serialize Python requests for tools as canonicalized strings (#14109)

When working on support for reading global Python pins in tool
operations, I noticed that we weren't using the canonicalized Python
request in receipts — we were using the raw string provided by the user.
Since we'll need to compare these values, we should be using the
canonicalized string.

The `Tool` and `ToolReceipt` types have been updated to hold a
`PythonRequest` instead of a `String`, and `Serialize` was implemented
for `PythonRequest` so canonicalization can happen at the edge instead
of being the caller's responsibility.
This commit is contained in:
Zanie Blue
2025-06-17 17:45:11 -05:00
committed by GitHub
parent 6c096246d8
commit 47c522f9be
5 changed files with 46 additions and 13 deletions

View File

@@ -67,6 +67,26 @@ pub enum PythonRequest {
Key(PythonDownloadRequest),
}
impl<'a> serde::Deserialize<'a> for PythonRequest {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'a>,
{
let s = String::deserialize(deserializer)?;
Ok(PythonRequest::parse(&s))
}
}
impl serde::Serialize for PythonRequest {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let s = self.to_canonical_string();
serializer.serialize_str(&s)
}
}
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, serde::Deserialize)]
#[serde(deny_unknown_fields, rename_all = "kebab-case")]
#[cfg_attr(feature = "clap", derive(clap::ValueEnum))]