Avoid unwrap when serializing receipts (#5678)

This commit is contained in:
Charlie Marsh 2024-07-31 22:57:16 -04:00 committed by GitHub
parent df6106f5bf
commit b9866b3ee8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 22 additions and 23 deletions

View file

@ -52,6 +52,8 @@ pub enum Error {
EnvironmentRead(PathBuf, String),
#[error("Failed find tool package `{0}` at `{1}`")]
MissingToolPackage(PackageName, PathBuf),
#[error(transparent)]
Serialization(#[from] toml_edit::ser::Error),
}
/// A collection of uv-managed tools installed on the current system.
@ -159,7 +161,7 @@ impl InstalledTools {
path.user_display()
);
let doc = tool_receipt.to_toml();
let doc = tool_receipt.to_toml()?;
// Save the modified `uv-receipt.toml`.
fs_err::write(&path, doc)?;

View file

@ -32,13 +32,13 @@ impl ToolReceipt {
}
/// Returns the TOML representation of this receipt.
pub(crate) fn to_toml(&self) -> String {
pub(crate) fn to_toml(&self) -> Result<String, toml_edit::ser::Error> {
// We construct a TOML document manually instead of going through Serde to enable
// the use of inline tables.
let mut doc = toml_edit::DocumentMut::new();
doc.insert("tool", toml_edit::Item::Table(self.tool.to_toml()));
doc.insert("tool", toml_edit::Item::Table(self.tool.to_toml()?));
doc.to_string()
Ok(doc.to_string())
}
}

View file

@ -73,26 +73,25 @@ impl Tool {
}
/// Returns the TOML table for this tool.
pub(crate) fn to_toml(&self) -> Table {
pub(crate) fn to_toml(&self) -> Result<Table, toml_edit::ser::Error> {
let mut table = Table::new();
table.insert("requirements", {
let requirements = match self.requirements.as_slice() {
let requirements = self
.requirements
.iter()
.map(|requirement| {
serde::Serialize::serialize(
&requirement,
toml_edit::ser::ValueSerializer::new(),
)
})
.collect::<Result<Vec<_>, _>>()?;
let requirements = match requirements.as_slice() {
[] => Array::new(),
[requirement] => Array::from_iter([serde::Serialize::serialize(
&requirement,
toml_edit::ser::ValueSerializer::new(),
)
.unwrap()]),
requirements => {
each_element_on_its_line_array(requirements.iter().map(|requirement| {
serde::Serialize::serialize(
&requirement,
toml_edit::ser::ValueSerializer::new(),
)
.unwrap()
}))
}
[requirement] => Array::from_iter([requirement]),
requirements => each_element_on_its_line_array(requirements.iter()),
};
value(requirements)
});
@ -111,7 +110,7 @@ impl Tool {
value(entrypoints)
});
table
Ok(table)
}
pub fn entrypoints(&self) -> &[ToolEntrypoint] {

View file

@ -144,7 +144,6 @@ fn tool_install() {
sys.argv[0] = re.sub(r"(-script\.pyw|\.exe)?$", "", sys.argv[0])
sys.exit(main())
"###);
});
uv_snapshot!(context.filters(), Command::new("flask").arg("--version").env("PATH", bin_dir.as_os_str()), @r###"
@ -161,7 +160,6 @@ fn tool_install() {
insta::with_settings!({
filters => context.filters(),
}, {
// We should have a new tool receipt
assert_snapshot!(fs_err::read_to_string(tool_dir.join("flask").join("uv-receipt.toml")).unwrap(), @r###"
[tool]
requirements = [{ name = "flask" }]