11use anyhow:: { Context , Result } ;
22use serde:: Serialize ;
33use std:: env;
4- use std:: error:: Error ;
54use std:: path:: PathBuf ;
65use std:: process:: { Command , Stdio } ;
76
@@ -10,7 +9,7 @@ use crate::exercise::Exercise;
109/// Contains the structure of resulting rust-project.json file
1110/// and functions to build the data required to create the file
1211#[ derive( Serialize ) ]
13- pub struct RustAnalyzerProject {
12+ struct RustAnalyzerProject {
1413 sysroot_src : PathBuf ,
1514 crates : Vec < Crate > ,
1615}
@@ -25,12 +24,22 @@ struct Crate {
2524}
2625
2726impl 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+
3039 if let Some ( path) = env:: var_os ( "RUST_SRC_PATH" ) {
3140 return Ok ( Self {
3241 sysroot_src : PathBuf :: from ( path) ,
33- crates : Vec :: new ( ) ,
42+ crates,
3443 } ) ;
3544 }
3645
@@ -53,35 +62,21 @@ impl RustAnalyzerProject {
5362
5463 Ok ( Self {
5564 sysroot_src,
56- crates : Vec :: new ( ) ,
65+ crates,
5766 } )
5867 }
68+ }
5969
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) ?;
7073
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 ( ( ) )
8782}
0 commit comments