From d24c9ccea5338b863de3a63e178b6e65d93ce2c5 Mon Sep 17 00:00:00 2001 From: Jeff Dickey <216188+jdx@users.noreply.github.com> Date: Tue, 28 May 2024 10:55:32 -0500 Subject: [PATCH] refactor: simplify ForgeArg building --- src/cli/args/forge_arg.rs | 20 ++++++++++++-------- src/plugins/core/bun.rs | 2 +- src/plugins/core/deno.rs | 2 +- src/plugins/core/erlang.rs | 2 +- src/plugins/core/go.rs | 2 +- src/plugins/core/java.rs | 2 +- src/plugins/core/mod.rs | 9 +++------ src/plugins/core/node.rs | 2 +- src/plugins/core/python.rs | 2 +- src/plugins/core/ruby.rs | 2 +- src/plugins/core/zig.rs | 2 +- 11 files changed, 24 insertions(+), 23 deletions(-) diff --git a/src/cli/args/forge_arg.rs b/src/cli/args/forge_arg.rs index 1a639caa5..835ab9a37 100644 --- a/src/cli/args/forge_arg.rs +++ b/src/cli/args/forge_arg.rs @@ -11,16 +11,25 @@ use crate::registry::REGISTRY; #[derive(Clone, PartialOrd, Ord)] pub struct ForgeArg { + /// user-specified identifier, "node", "npm:prettier", "cargo:eza", "vfox:version-fox/vfox-nodejs" + /// multiple ids may point to a single tool, e.g.: "node", "core:node" or "vfox:version-fox/vfox-nodejs" + /// and "vfox:https://github.com/version-fox/vfox-nodejs" pub id: String, + /// the name of the tool within the forge, e.g.: "node", "prettier", "eza", "vfox-nodejs" pub name: String, + /// type of forge, "asdf", "cargo", "core", "npm", "vfox" pub forge_type: ForgeType, + /// ~/.local/share/mise/cache/ pub cache_path: PathBuf, + /// ~/.local/share/mise/installs/ pub installs_path: PathBuf, + /// ~/.local/share/mise/downloads/ pub downloads_path: PathBuf, } -impl From<&str> for ForgeArg { - fn from(s: &str) -> Self { +impl> From for ForgeArg { + fn from(s: A) -> Self { + let s = s.as_ref(); if let Some(fa) = FORGE_MAP.get(s) { return fa.clone(); } @@ -32,18 +41,13 @@ impl From<&str> for ForgeArg { Self::new(ForgeType::Asdf, s) } } -impl From<&String> for ForgeArg { - fn from(s: &String) -> Self { - Self::from(s.as_str()) - } -} impl ForgeArg { pub fn new(forge_type: ForgeType, name: &str) -> Self { let name = unalias_forge(name).to_string(); let id = match forge_type { ForgeType::Asdf | ForgeType::Core => name.clone(), - forge_type => format!("{}:{}", forge_type.as_ref(), name), + forge_type => format!("{forge_type}:{name}"), }; let pathname = regex!(r#"[/:]"#).replace_all(&id, "-").to_string(); Self { diff --git a/src/plugins/core/bun.rs b/src/plugins/core/bun.rs index 76aa6306c..3a9f7f916 100644 --- a/src/plugins/core/bun.rs +++ b/src/plugins/core/bun.rs @@ -24,7 +24,7 @@ pub struct BunPlugin { impl BunPlugin { pub fn new() -> Self { - let core = CorePlugin::new("bun"); + let core = CorePlugin::new("bun".into()); Self { core } } diff --git a/src/plugins/core/deno.rs b/src/plugins/core/deno.rs index 4dc72810a..6ce704448 100644 --- a/src/plugins/core/deno.rs +++ b/src/plugins/core/deno.rs @@ -26,7 +26,7 @@ pub struct DenoPlugin { impl DenoPlugin { pub fn new() -> Self { - let core = CorePlugin::new("deno"); + let core = CorePlugin::new("deno".into()); Self { core } } diff --git a/src/plugins/core/erlang.rs b/src/plugins/core/erlang.rs index 9816ad387..08854256f 100644 --- a/src/plugins/core/erlang.rs +++ b/src/plugins/core/erlang.rs @@ -22,7 +22,7 @@ const KERL_VERSION: &str = "4.1.1"; impl ErlangPlugin { pub fn new() -> Self { Self { - core: CorePlugin::new("erlang"), + core: CorePlugin::new("erlang".into()), } } diff --git a/src/plugins/core/go.rs b/src/plugins/core/go.rs index 253eb39ac..341b9062c 100644 --- a/src/plugins/core/go.rs +++ b/src/plugins/core/go.rs @@ -26,7 +26,7 @@ pub struct GoPlugin { impl GoPlugin { pub fn new() -> Self { Self { - core: CorePlugin::new("go"), + core: CorePlugin::new("go".into()), } } diff --git a/src/plugins/core/java.rs b/src/plugins/core/java.rs index 5469b02a2..69a529ed1 100644 --- a/src/plugins/core/java.rs +++ b/src/plugins/core/java.rs @@ -35,7 +35,7 @@ pub struct JavaPlugin { impl JavaPlugin { pub fn new() -> Self { - let core = CorePlugin::new("java"); + let core = CorePlugin::new("java".into()); let java_metadata_ga_cache_filename = format!("java_metadata_ga_{}_{}-$KEY.msgpack.z", os(), arch()); let java_metadata_ea_cache_filename = diff --git a/src/plugins/core/mod.rs b/src/plugins/core/mod.rs index b2e9e9de1..e1e8a8162 100644 --- a/src/plugins/core/mod.rs +++ b/src/plugins/core/mod.rs @@ -11,7 +11,7 @@ use crate::cache::CacheManager; use crate::cli::args::ForgeArg; use crate::config::Settings; use crate::env; -use crate::forge::{Forge, ForgeList, ForgeType}; +use crate::forge::{Forge, ForgeList}; use crate::http::HTTP_FETCH; use crate::plugins::core::bun::BunPlugin; use crate::plugins::core::deno::DenoPlugin; @@ -55,15 +55,12 @@ pub static CORE_PLUGINS: Lazy = Lazy::new(|| { #[derive(Debug)] pub struct CorePlugin { pub fa: ForgeArg, - pub name: &'static str, pub remote_version_cache: CacheManager>, } impl CorePlugin { - pub fn new(name: &'static str) -> Self { - let fa = ForgeArg::new(ForgeType::Asdf, name); + pub fn new(fa: ForgeArg) -> Self { Self { - name, remote_version_cache: CacheManager::new( fa.cache_path.join("remote_versions-$KEY.msgpack.z"), ) @@ -90,7 +87,7 @@ impl CorePlugin { if !*env::MISE_USE_VERSIONS_HOST { return Ok(None); } - let raw = HTTP_FETCH.get_text(format!("http://mise-versions.jdx.dev/{}", &self.name))?; + let raw = HTTP_FETCH.get_text(format!("http://mise-versions.jdx.dev/{}", &self.fa.name))?; let versions = raw .lines() .map(|v| v.trim().to_string()) diff --git a/src/plugins/core/node.rs b/src/plugins/core/node.rs index 7a9f30b42..3b6d7689d 100644 --- a/src/plugins/core/node.rs +++ b/src/plugins/core/node.rs @@ -27,7 +27,7 @@ pub struct NodePlugin { impl NodePlugin { pub fn new() -> Self { Self { - core: CorePlugin::new("node"), + core: CorePlugin::new("node".into()), } } diff --git a/src/plugins/core/python.rs b/src/plugins/core/python.rs index 3ab8e818f..05124098a 100644 --- a/src/plugins/core/python.rs +++ b/src/plugins/core/python.rs @@ -27,7 +27,7 @@ pub struct PythonPlugin { impl PythonPlugin { pub fn new() -> Self { - let core = CorePlugin::new("python"); + let core = CorePlugin::new("python".into()); Self { precompiled_cache: CacheManager::new( core.fa.cache_path.join("precompiled-$KEY.msgpack.z"), diff --git a/src/plugins/core/ruby.rs b/src/plugins/core/ruby.rs index 83f6327f7..e636ba843 100644 --- a/src/plugins/core/ruby.rs +++ b/src/plugins/core/ruby.rs @@ -28,7 +28,7 @@ pub struct RubyPlugin { impl RubyPlugin { pub fn new() -> Self { Self { - core: CorePlugin::new("ruby"), + core: CorePlugin::new("ruby".into()), } } diff --git a/src/plugins/core/zig.rs b/src/plugins/core/zig.rs index a3bef56fc..b6c1bde9b 100644 --- a/src/plugins/core/zig.rs +++ b/src/plugins/core/zig.rs @@ -24,7 +24,7 @@ pub struct ZigPlugin { impl ZigPlugin { pub fn new() -> Self { - let core = CorePlugin::new("zig"); + let core = CorePlugin::new("zig".into()); Self { core } }