From 8d3ec24c11654d668ef1e1638a7770ec8beadfb7 Mon Sep 17 00:00:00 2001 From: mo8it Date: Mon, 25 Mar 2024 22:41:14 +0100 Subject: [PATCH] Optimize the serialized data types --- src/project.rs | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/src/project.rs b/src/project.rs index 347ca46..54cffe1 100644 --- a/src/project.rs +++ b/src/project.rs @@ -1,4 +1,4 @@ -use anyhow::{bail, Context, Result}; +use anyhow::{Context, Result}; use serde::Serialize; use std::env; use std::error::Error; @@ -11,24 +11,25 @@ use crate::exercise::Exercise; /// and functions to build the data required to create the file #[derive(Serialize)] pub struct RustAnalyzerProject { - sysroot_src: String, + sysroot_src: PathBuf, crates: Vec, } #[derive(Serialize)] -pub struct Crate { - root_module: String, - edition: String, - deps: Vec, - cfg: Vec, +struct Crate { + root_module: PathBuf, + edition: &'static str, + // Not used, but required in the JSON file. + deps: Vec<()>, + cfg: [&'static str; 1], } impl RustAnalyzerProject { pub fn build() -> Result { // check if RUST_SRC_PATH is set - if let Ok(sysroot_src) = env::var("RUST_SRC_PATH") { + if let Some(path) = env::var_os("RUST_SRC_PATH") { return Ok(Self { - sysroot_src, + sysroot_src: PathBuf::from(path), crates: Vec::new(), }); } @@ -49,9 +50,6 @@ impl RustAnalyzerProject { let mut sysroot_src = PathBuf::with_capacity(256); sysroot_src.extend([toolchain, "lib", "rustlib", "src", "rust", "library"]); - let Ok(sysroot_src) = sysroot_src.into_os_string().into_string() else { - bail!("The sysroot path is invalid UTF8"); - }; Ok(Self { sysroot_src, @@ -77,11 +75,11 @@ impl RustAnalyzerProject { self.crates = exercises .into_iter() .map(|exercise| Crate { - root_module: exercise.path.display().to_string(), - edition: "2021".to_string(), + root_module: exercise.path, + edition: "2021", deps: Vec::new(), // This allows rust_analyzer to work inside #[test] blocks - cfg: vec!["test".to_string()], + cfg: ["test"], }) .collect(); Ok(())