diff --git a/src/bundler.rs b/src/bundler.rs index 9fd9a30..23c0e65 100644 --- a/src/bundler.rs +++ b/src/bundler.rs @@ -1,9 +1,9 @@ use crate::command_executor::CommandExecutor; -use std::path::Path; +use std::path::PathBuf; /// A simple wrapper around the `bundle` command. pub struct Bundler { - pub working_dir: String, + pub working_dir: PathBuf, command_executor: Box, } @@ -13,7 +13,7 @@ impl Bundler { /// # Arguments /// * `working_dir` - The working directory where `bundle` commands should be executed. /// * `command_executor` - An executor for `bundle` commands. - pub fn new(working_dir: String, command_executor: Box) -> Self { + pub fn new(working_dir: PathBuf, command_executor: Box) -> Self { Bundler { working_dir, command_executor, @@ -43,7 +43,7 @@ impl Bundler { args: &[&str], envs: &[(&str, &str)], ) -> Result { - let bundle_gemfile_path = Path::new(&self.working_dir).join("Gemfile"); + let bundle_gemfile_path = self.working_dir.join("Gemfile"); let bundle_gemfile = bundle_gemfile_path .to_str() .ok_or_else(|| "Invalid path to Gemfile".to_string())?; diff --git a/src/gemset.rs b/src/gemset.rs index 0af8b35..8c96d73 100644 --- a/src/gemset.rs +++ b/src/gemset.rs @@ -1,14 +1,15 @@ use crate::command_executor::CommandExecutor; use regex::Regex; +use std::path::PathBuf; /// A simple wrapper around the `gem` command. pub struct Gemset { - pub gem_home: String, + pub gem_home: PathBuf, command_executor: Box, } impl Gemset { - pub fn new(gem_home: String, command_executor: Box) -> Self { + pub fn new(gem_home: PathBuf, command_executor: Box) -> Self { Self { gem_home, command_executor, @@ -17,9 +18,7 @@ impl Gemset { /// Returns the full path to a gem binary executable. pub fn gem_bin_path(&self, bin_name: &str) -> Result { - let path = std::path::Path::new(&self.gem_home) - .join("bin") - .join(bin_name); + let path = self.gem_home.join("bin").join(bin_name); path.to_str() .map(ToString::to_string) @@ -29,7 +28,7 @@ impl Gemset { pub fn gem_path_env(&self) -> Vec<(String, String)> { vec![( "GEM_PATH".to_string(), - format!("{}:$GEM_PATH", self.gem_home), + format!("{}:$GEM_PATH", self.gem_home.display()), )] } @@ -97,7 +96,11 @@ impl Gemset { .chain(std::iter::once("--norc")) .chain(args.iter().copied()) .collect(); - let command_envs = &[("GEM_HOME", self.gem_home.as_str())]; + let gem_home_str = self + .gem_home + .to_str() + .ok_or("Failed to convert gem_home path to string")?; + let command_envs = &[("GEM_HOME", gem_home_str)]; self.command_executor .execute("gem", &full_args, command_envs) @@ -200,13 +203,13 @@ mod tests { const TEST_GEM_HOME: &str = "/test/gem_home"; fn create_gemset(mock_executor: MockGemCommandExecutor) -> Gemset { - Gemset::new(TEST_GEM_HOME.to_string(), Box::new(mock_executor)) + Gemset::new(TEST_GEM_HOME.into(), Box::new(mock_executor)) } #[test] fn test_gem_bin_path() { let gemset = Gemset::new( - TEST_GEM_HOME.to_string(), + TEST_GEM_HOME.into(), Box::new(MockGemCommandExecutor::new()), ); let path = gemset.gem_bin_path("ruby-lsp").unwrap(); @@ -216,7 +219,7 @@ mod tests { #[test] fn test_gem_path_env() { let gemset = Gemset::new( - TEST_GEM_HOME.to_string(), + TEST_GEM_HOME.into(), Box::new(MockGemCommandExecutor::new()), ); let env = gemset.gem_path_env(); diff --git a/src/language_servers/herb.rs b/src/language_servers/herb.rs index 4f7e6ad..c89e64b 100644 --- a/src/language_servers/herb.rs +++ b/src/language_servers/herb.rs @@ -41,7 +41,7 @@ impl Herb { .unwrap() .join(&server_path) .to_string_lossy() - .to_string(), + .into_owned(), "--stdio".to_string(), ], env: Default::default(), diff --git a/src/language_servers/language_server.rs b/src/language_servers/language_server.rs index e39bed6..3564fe9 100644 --- a/src/language_servers/language_server.rs +++ b/src/language_servers/language_server.rs @@ -2,6 +2,7 @@ use std::collections::HashMap; use crate::{bundler::Bundler, command_executor::RealCommandExecutor, gemset::Gemset}; +use std::path::PathBuf; use zed_extension_api::{self as zed}; #[derive(Clone, Debug)] @@ -164,7 +165,10 @@ pub trait LanguageServer { return self.try_find_on_path_or_extension_gemset(language_server_id, worktree); } - let bundler = Bundler::new(worktree.root_path(), Box::new(RealCommandExecutor)); + let bundler = Bundler::new( + PathBuf::from(worktree.root_path()), + Box::new(RealCommandExecutor), + ); let shell_env = worktree.shell_env(); let env_vars: Vec<(&str, &str)> = shell_env .iter() @@ -218,7 +222,7 @@ pub trait LanguageServer { .to_string_lossy() .to_string(); - let gemset = Gemset::new(gem_home.clone(), Box::new(RealCommandExecutor)); + let gemset = Gemset::new(PathBuf::from(&gem_home), Box::new(RealCommandExecutor)); zed::set_language_server_installation_status( language_server_id, diff --git a/src/ruby.rs b/src/ruby.rs index 8453664..fbb43e3 100644 --- a/src/ruby.rs +++ b/src/ruby.rs @@ -3,7 +3,7 @@ mod command_executor; mod gemset; mod language_servers; -use std::{collections::HashMap, path::Path}; +use std::{collections::HashMap, path::PathBuf}; use bundler::Bundler; use command_executor::RealCommandExecutor; @@ -133,14 +133,17 @@ impl zed::Extension for RubyExtension { .map(|(key, value)| (key.as_str(), value.as_str())) .collect(); - let mut rdbg_path = Path::new(&adapter_name) + let mut rdbg_path = PathBuf::from(&adapter_name) .join("rdbg") .to_string_lossy() .into_owned(); let mut use_bundler = false; if worktree.which(&rdbg_path).is_none() { - let bundler = Bundler::new(worktree.root_path(), Box::new(RealCommandExecutor)); + let bundler = Bundler::new( + PathBuf::from(worktree.root_path()), + Box::new(RealCommandExecutor), + ); match bundler.installed_gem_version("debug", &env_vars) { Ok(_version) => { rdbg_path = worktree @@ -150,10 +153,8 @@ impl zed::Extension for RubyExtension { } Err(_e) => { let gem_home = std::env::current_dir() - .map_err(|e| format!("Failed to get extension directory: {e}"))? - .to_string_lossy() - .to_string(); - let gemset = Gemset::new(gem_home.clone(), Box::new(RealCommandExecutor)); + .map_err(|e| format!("Failed to get extension directory: {e}"))?; + let gemset = Gemset::new(gem_home, Box::new(RealCommandExecutor)); match gemset.install_gem("debug") { Ok(_) => rdbg_path = gemset.gem_bin_path("rdbg")?,