@@ -9,6 +9,7 @@ use cargo_metadata::camino::{Utf8Path, Utf8PathBuf};
99use cargo_metadata:: semver:: Version ;
1010use cargo_metadata:: { Metadata , MetadataCommand , Package } ;
1111use std:: fs;
12+ use std:: ops:: Deref ;
1213use std:: path:: { Path , PathBuf } ;
1314
1415#[ expect(
@@ -75,7 +76,7 @@ impl SpirvSource {
7576 /// # Errors
7677 /// Crate may not depend on `spirv-std` or is otherwise malformed
7778 pub fn new (
78- shader_crate_path : & Path ,
79+ shader_crate : & CrateMetadata ,
7980 maybe_rust_gpu_source : Option < & str > ,
8081 maybe_rust_gpu_version : Option < & str > ,
8182 ) -> anyhow:: Result < Self > {
@@ -89,10 +90,10 @@ impl SpirvSource {
8990 Self :: CratesIO ( Version :: parse ( rust_gpu_version) ?)
9091 }
9192 } else {
92- Self :: get_rust_gpu_deps_from_shader ( shader_crate_path ) . with_context ( || {
93+ Self :: get_rust_gpu_deps_from_shader ( shader_crate ) . with_context ( || {
9394 format ! (
9495 "get spirv-std dependency from shader crate '{}'" ,
95- shader_crate_path . display( )
96+ shader_crate . path ( ) . display( )
9697 )
9798 } ) ?
9899 } ;
@@ -103,14 +104,13 @@ impl SpirvSource {
103104 ///
104105 /// # Errors
105106 /// Crate may not depend on `spirv-std` or is otherwise malformed
106- pub fn get_rust_gpu_deps_from_shader ( shader_crate_path : & Path ) -> anyhow:: Result < Self > {
107- let crate_metadata = query_metadata ( shader_crate_path) ?;
107+ pub fn get_rust_gpu_deps_from_shader ( crate_metadata : & CrateMetadata ) -> anyhow:: Result < Self > {
108108 let spirv_std_package = crate_metadata. find_package ( "spirv-std" ) ?;
109109 let spirv_source = Self :: parse_spirv_std_source_and_version ( spirv_std_package) ?;
110110 log:: debug!(
111111 "Parsed `SpirvSource` from crate `{}`: \
112112 {spirv_source:?}",
113- shader_crate_path . display( ) ,
113+ crate_metadata . path ( ) . display( ) ,
114114 ) ;
115115 Ok ( spirv_source)
116116 }
@@ -193,33 +193,39 @@ impl SpirvSource {
193193 }
194194}
195195
196- /// get the Package metadata from some crate
197- ///
198- /// # Errors
199- /// metadata query may fail
200- pub fn query_metadata ( crate_path : & Path ) -> anyhow:: Result < Metadata > {
201- log:: debug!( "Running `cargo metadata` on `{}`" , crate_path. display( ) ) ;
202- let metadata = MetadataCommand :: new ( )
203- . current_dir (
204- & crate_path
205- . canonicalize ( )
206- . context ( "could not get absolute path to shader crate" ) ?,
207- )
208- . exec ( ) ?;
209- Ok ( metadata)
196+ /// [`cargo_metadata::Metadata`] combined with the path to the crate being queried
197+ pub struct CrateMetadata {
198+ path : PathBuf ,
199+ metadata : Metadata ,
210200}
211201
212- /// implements [`Self::find_package`]
213- pub trait FindPackage {
202+ impl CrateMetadata {
203+ /// get the Package metadata from some crate
204+ ///
205+ /// # Errors
206+ /// metadata query may fail
207+ pub fn query ( path : PathBuf ) -> anyhow:: Result < Self > {
208+ log:: debug!( "Running `cargo metadata` on `{}`" , path. display( ) ) ;
209+ let metadata = MetadataCommand :: new ( )
210+ . current_dir (
211+ & path
212+ . canonicalize ( )
213+ . context ( "could not get absolute path to shader crate" ) ?,
214+ )
215+ . exec ( ) ?;
216+ Ok ( Self { path, metadata } )
217+ }
218+
219+ /// Path to the crate that was queried
220+ pub fn path ( & self ) -> & Path {
221+ self . path . as_path ( )
222+ }
223+
214224 /// Search for a package or return a nice error
215225 ///
216226 /// # Errors
217227 /// package may not be found or crate may be malformed
218- fn find_package ( & self , crate_name : & str ) -> anyhow:: Result < & Package > ;
219- }
220-
221- impl FindPackage for Metadata {
222- fn find_package ( & self , crate_name : & str ) -> anyhow:: Result < & Package > {
228+ pub fn find_package ( & self , crate_name : & str ) -> anyhow:: Result < & Package > {
223229 if let Some ( package) = self
224230 . packages
225231 . iter ( )
@@ -236,6 +242,14 @@ impl FindPackage for Metadata {
236242 }
237243}
238244
245+ impl Deref for CrateMetadata {
246+ type Target = Metadata ;
247+
248+ fn deref ( & self ) -> & Self :: Target {
249+ & self . metadata
250+ }
251+ }
252+
239253/// Parse the `rust-toolchain.toml` in the working tree of the checked-out version of the `rust-gpu` repo.
240254///
241255/// # Errors
@@ -273,7 +287,8 @@ mod test {
273287 #[ test_log:: test]
274288 fn parsing_spirv_std_dep_for_shader_template ( ) {
275289 let shader_template_path = crate :: test:: shader_crate_template_path ( ) ;
276- let source = SpirvSource :: get_rust_gpu_deps_from_shader ( & shader_template_path) . unwrap ( ) ;
290+ let metadata = CrateMetadata :: query ( shader_template_path) . unwrap ( ) ;
291+ let source = SpirvSource :: get_rust_gpu_deps_from_shader ( & metadata) . unwrap ( ) ;
277292 expect ! [ [ r#"
278293 Git {
279294 url: "https://github.com/Rust-GPU/rust-gpu",
@@ -286,7 +301,8 @@ mod test {
286301 fn cached_checkout_dir_sanity ( ) {
287302 let _env = TestEnv :: new ( ) ;
288303 let shader_template_path = crate :: test:: shader_crate_template_path ( ) ;
289- let source = SpirvSource :: get_rust_gpu_deps_from_shader ( & shader_template_path) . unwrap ( ) ;
304+ let metadata = CrateMetadata :: query ( shader_template_path) . unwrap ( ) ;
305+ let source = SpirvSource :: get_rust_gpu_deps_from_shader ( & metadata) . unwrap ( ) ;
290306 let dir = source. install_dir ( ) . unwrap ( ) ;
291307 let name = dir
292308 . file_name ( )
0 commit comments