1
1
use anyhow:: { Context , Result } ;
2
2
use serde:: Serialize ;
3
3
use std:: env;
4
- use std:: error:: Error ;
5
4
use std:: path:: PathBuf ;
6
5
use std:: process:: { Command , Stdio } ;
7
6
@@ -10,7 +9,7 @@ use crate::exercise::Exercise;
10
9
/// Contains the structure of resulting rust-project.json file
11
10
/// and functions to build the data required to create the file
12
11
#[ derive( Serialize ) ]
13
- pub struct RustAnalyzerProject {
12
+ struct RustAnalyzerProject {
14
13
sysroot_src : PathBuf ,
15
14
crates : Vec < Crate > ,
16
15
}
@@ -25,12 +24,22 @@ struct Crate {
25
24
}
26
25
27
26
impl RustAnalyzerProject {
28
- pub fn build ( ) -> Result < Self > {
29
- // check if RUST_SRC_PATH is set
27
+ fn build ( exercises : Vec < Exercise > ) -> Result < Self > {
28
+ let crates = exercises
29
+ . into_iter ( )
30
+ . map ( |exercise| Crate {
31
+ root_module : exercise. path ,
32
+ edition : "2021" ,
33
+ deps : Vec :: new ( ) ,
34
+ // This allows rust_analyzer to work inside #[test] blocks
35
+ cfg : [ "test" ] ,
36
+ } )
37
+ . collect ( ) ;
38
+
30
39
if let Some ( path) = env:: var_os ( "RUST_SRC_PATH" ) {
31
40
return Ok ( Self {
32
41
sysroot_src : PathBuf :: from ( path) ,
33
- crates : Vec :: new ( ) ,
42
+ crates,
34
43
} ) ;
35
44
}
36
45
@@ -53,35 +62,21 @@ impl RustAnalyzerProject {
53
62
54
63
Ok ( Self {
55
64
sysroot_src,
56
- crates : Vec :: new ( ) ,
65
+ crates,
57
66
} )
58
67
}
68
+ }
59
69
60
- /// Write rust-project.json to disk
61
- pub fn write_to_disk ( & self ) -> Result < ( ) , std:: io:: Error > {
62
- // Using the capacity 2^14 = 16384 since the file length in bytes is higher than 2^13.
63
- // The final length is not known exactly because it depends on the user's sysroot path,
64
- // the current number of exercises etc.
65
- let mut buf = Vec :: with_capacity ( 16384 ) ;
66
- serde_json:: to_writer ( & mut buf, & self ) . expect ( "Failed to serialize to JSON" ) ;
67
- std:: fs:: write ( "rust-project.json" , buf) ?;
68
- Ok ( ( ) )
69
- }
70
+ /// Write `rust-project.json` to disk.
71
+ pub fn write_project_json ( exercises : Vec < Exercise > ) -> Result < ( ) > {
72
+ let content = RustAnalyzerProject :: build ( exercises) ?;
70
73
71
- /// Parse the exercises folder for .rs files, any matches will create
72
- /// a new `crate` in rust-project.json which allows rust-analyzer to
73
- /// treat it like a normal binary
74
- pub fn exercises_to_json ( & mut self , exercises : Vec < Exercise > ) -> Result < ( ) , Box < dyn Error > > {
75
- self . crates = exercises
76
- . into_iter ( )
77
- . map ( |exercise| Crate {
78
- root_module : exercise. path ,
79
- edition : "2021" ,
80
- deps : Vec :: new ( ) ,
81
- // This allows rust_analyzer to work inside #[test] blocks
82
- cfg : [ "test" ] ,
83
- } )
84
- . collect ( ) ;
85
- Ok ( ( ) )
86
- }
74
+ // Using the capacity 2^14 since the file length in bytes is higher than 2^13.
75
+ // The final length is not known exactly because it depends on the user's sysroot path,
76
+ // the current number of exercises etc.
77
+ let mut buf = Vec :: with_capacity ( 1 << 14 ) ;
78
+ serde_json:: to_writer ( & mut buf, & content) ?;
79
+ std:: fs:: write ( "rust-project.json" , buf) ?;
80
+
81
+ Ok ( ( ) )
87
82
}
0 commit comments