1- use anyhow:: { bail , Context , Result } ;
1+ use anyhow:: { Context , Result } ;
22use serde:: Serialize ;
33use std:: env;
44use std:: error:: Error ;
@@ -11,24 +11,25 @@ use crate::exercise::Exercise;
1111/// and functions to build the data required to create the file
1212#[ derive( Serialize ) ]
1313pub struct RustAnalyzerProject {
14- sysroot_src : String ,
14+ sysroot_src : PathBuf ,
1515 crates : Vec < Crate > ,
1616}
1717
1818#[ derive( Serialize ) ]
19- pub struct Crate {
20- root_module : String ,
21- edition : String ,
22- deps : Vec < String > ,
23- cfg : Vec < String > ,
19+ struct Crate {
20+ root_module : PathBuf ,
21+ edition : & ' static str ,
22+ // Not used, but required in the JSON file.
23+ deps : Vec < ( ) > ,
24+ cfg : [ & ' static str ; 1 ] ,
2425}
2526
2627impl RustAnalyzerProject {
2728 pub fn build ( ) -> Result < Self > {
2829 // check if RUST_SRC_PATH is set
29- if let Ok ( sysroot_src ) = env:: var ( "RUST_SRC_PATH" ) {
30+ if let Some ( path ) = env:: var_os ( "RUST_SRC_PATH" ) {
3031 return Ok ( Self {
31- sysroot_src,
32+ sysroot_src : PathBuf :: from ( path ) ,
3233 crates : Vec :: new ( ) ,
3334 } ) ;
3435 }
@@ -49,9 +50,6 @@ impl RustAnalyzerProject {
4950
5051 let mut sysroot_src = PathBuf :: with_capacity ( 256 ) ;
5152 sysroot_src. extend ( [ toolchain, "lib" , "rustlib" , "src" , "rust" , "library" ] ) ;
52- let Ok ( sysroot_src) = sysroot_src. into_os_string ( ) . into_string ( ) else {
53- bail ! ( "The sysroot path is invalid UTF8" ) ;
54- } ;
5553
5654 Ok ( Self {
5755 sysroot_src,
@@ -77,11 +75,11 @@ impl RustAnalyzerProject {
7775 self . crates = exercises
7876 . into_iter ( )
7977 . map ( |exercise| Crate {
80- root_module : exercise. path . display ( ) . to_string ( ) ,
81- edition : "2021" . to_string ( ) ,
78+ root_module : exercise. path ,
79+ edition : "2021" ,
8280 deps : Vec :: new ( ) ,
8381 // This allows rust_analyzer to work inside #[test] blocks
84- cfg : vec ! [ "test" . to_string ( ) ] ,
82+ cfg : [ "test" ] ,
8583 } )
8684 . collect ( ) ;
8785 Ok ( ( ) )
0 commit comments