1
- use anyhow:: { bail , Context , Result } ;
1
+ use anyhow:: { Context , Result } ;
2
2
use serde:: Serialize ;
3
3
use std:: env;
4
4
use std:: error:: Error ;
@@ -11,24 +11,25 @@ use crate::exercise::Exercise;
11
11
/// and functions to build the data required to create the file
12
12
#[ derive( Serialize ) ]
13
13
pub struct RustAnalyzerProject {
14
- sysroot_src : String ,
14
+ sysroot_src : PathBuf ,
15
15
crates : Vec < Crate > ,
16
16
}
17
17
18
18
#[ 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 ] ,
24
25
}
25
26
26
27
impl RustAnalyzerProject {
27
28
pub fn build ( ) -> Result < Self > {
28
29
// 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" ) {
30
31
return Ok ( Self {
31
- sysroot_src,
32
+ sysroot_src : PathBuf :: from ( path ) ,
32
33
crates : Vec :: new ( ) ,
33
34
} ) ;
34
35
}
@@ -49,9 +50,6 @@ impl RustAnalyzerProject {
49
50
50
51
let mut sysroot_src = PathBuf :: with_capacity ( 256 ) ;
51
52
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
- } ;
55
53
56
54
Ok ( Self {
57
55
sysroot_src,
@@ -77,11 +75,11 @@ impl RustAnalyzerProject {
77
75
self . crates = exercises
78
76
. into_iter ( )
79
77
. map ( |exercise| Crate {
80
- root_module : exercise. path . display ( ) . to_string ( ) ,
81
- edition : "2021" . to_string ( ) ,
78
+ root_module : exercise. path ,
79
+ edition : "2021" ,
82
80
deps : Vec :: new ( ) ,
83
81
// This allows rust_analyzer to work inside #[test] blocks
84
- cfg : vec ! [ "test" . to_string ( ) ] ,
82
+ cfg : [ "test" ] ,
85
83
} )
86
84
. collect ( ) ;
87
85
Ok ( ( ) )
0 commit comments