mirror of
				https://github.com/astral-sh/uv.git
				synced 2025-10-31 20:09:09 +00:00 
			
		
		
		
	Avoid unwrap when serializing receipts (#5678)
This commit is contained in:
		
							parent
							
								
									df6106f5bf
								
							
						
					
					
						commit
						b9866b3ee8
					
				
					 4 changed files with 22 additions and 23 deletions
				
			
		|  | @ -52,6 +52,8 @@ pub enum Error { | ||||||
|     EnvironmentRead(PathBuf, String), |     EnvironmentRead(PathBuf, String), | ||||||
|     #[error("Failed find tool package `{0}` at `{1}`")] |     #[error("Failed find tool package `{0}` at `{1}`")] | ||||||
|     MissingToolPackage(PackageName, PathBuf), |     MissingToolPackage(PackageName, PathBuf), | ||||||
|  |     #[error(transparent)] | ||||||
|  |     Serialization(#[from] toml_edit::ser::Error), | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| /// A collection of uv-managed tools installed on the current system.
 | /// A collection of uv-managed tools installed on the current system.
 | ||||||
|  | @ -159,7 +161,7 @@ impl InstalledTools { | ||||||
|             path.user_display() |             path.user_display() | ||||||
|         ); |         ); | ||||||
| 
 | 
 | ||||||
|         let doc = tool_receipt.to_toml(); |         let doc = tool_receipt.to_toml()?; | ||||||
| 
 | 
 | ||||||
|         // Save the modified `uv-receipt.toml`.
 |         // Save the modified `uv-receipt.toml`.
 | ||||||
|         fs_err::write(&path, doc)?; |         fs_err::write(&path, doc)?; | ||||||
|  |  | ||||||
|  | @ -32,13 +32,13 @@ impl ToolReceipt { | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     /// Returns the TOML representation of this receipt.
 |     /// 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
 |         // We construct a TOML document manually instead of going through Serde to enable
 | ||||||
|         // the use of inline tables.
 |         // the use of inline tables.
 | ||||||
|         let mut doc = toml_edit::DocumentMut::new(); |         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()) | ||||||
|     } |     } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -73,26 +73,25 @@ impl Tool { | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     /// Returns the TOML table for this 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(); |         let mut table = Table::new(); | ||||||
| 
 | 
 | ||||||
|         table.insert("requirements", { |         table.insert("requirements", { | ||||||
|             let requirements = match self.requirements.as_slice() { |             let requirements = self | ||||||
|                 [] => Array::new(), |                 .requirements | ||||||
|                 [requirement] => Array::from_iter([serde::Serialize::serialize( |                 .iter() | ||||||
|                     &requirement, |                 .map(|requirement| { | ||||||
|                     toml_edit::ser::ValueSerializer::new(), |  | ||||||
|                 ) |  | ||||||
|                 .unwrap()]), |  | ||||||
|                 requirements => { |  | ||||||
|                     each_element_on_its_line_array(requirements.iter().map(|requirement| { |  | ||||||
|                     serde::Serialize::serialize( |                     serde::Serialize::serialize( | ||||||
|                         &requirement, |                         &requirement, | ||||||
|                         toml_edit::ser::ValueSerializer::new(), |                         toml_edit::ser::ValueSerializer::new(), | ||||||
|                     ) |                     ) | ||||||
|                         .unwrap() |                 }) | ||||||
|                     })) |                 .collect::<Result<Vec<_>, _>>()?; | ||||||
|                 } | 
 | ||||||
|  |             let requirements = match requirements.as_slice() { | ||||||
|  |                 [] => Array::new(), | ||||||
|  |                 [requirement] => Array::from_iter([requirement]), | ||||||
|  |                 requirements => each_element_on_its_line_array(requirements.iter()), | ||||||
|             }; |             }; | ||||||
|             value(requirements) |             value(requirements) | ||||||
|         }); |         }); | ||||||
|  | @ -111,7 +110,7 @@ impl Tool { | ||||||
|             value(entrypoints) |             value(entrypoints) | ||||||
|         }); |         }); | ||||||
| 
 | 
 | ||||||
|         table |         Ok(table) | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     pub fn entrypoints(&self) -> &[ToolEntrypoint] { |     pub fn entrypoints(&self) -> &[ToolEntrypoint] { | ||||||
|  |  | ||||||
|  | @ -144,7 +144,6 @@ fn tool_install() { | ||||||
|             sys.argv[0] = re.sub(r"(-script\.pyw|\.exe)?$", "", sys.argv[0]) |             sys.argv[0] = re.sub(r"(-script\.pyw|\.exe)?$", "", sys.argv[0]) | ||||||
|             sys.exit(main()) |             sys.exit(main()) | ||||||
|         "###);
 |         "###);
 | ||||||
| 
 |  | ||||||
|     }); |     }); | ||||||
| 
 | 
 | ||||||
|     uv_snapshot!(context.filters(), Command::new("flask").arg("--version").env("PATH", bin_dir.as_os_str()), @r###" |     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!({ |     insta::with_settings!({ | ||||||
|         filters => context.filters(), |         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###" |         assert_snapshot!(fs_err::read_to_string(tool_dir.join("flask").join("uv-receipt.toml")).unwrap(), @r###" | ||||||
|         [tool] |         [tool] | ||||||
|         requirements = [{ name = "flask" }] |         requirements = [{ name = "flask" }] | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Charlie Marsh
						Charlie Marsh