From 686b69f7a378c09a82f0ca0fe84d62df232adba0 Mon Sep 17 00:00:00 2001 From: Jo <10510431+j178@users.noreply.github.com> Date: Sat, 23 Nov 2024 23:27:27 +0800 Subject: [PATCH] Remove intermedia Language enum (#107) * Remove intermedia Language enum * fix * Simplify --- src/hook.rs | 11 ++-- src/languages/docker.rs | 5 -- src/languages/fail.rs | 5 -- src/languages/mod.rs | 124 +++++++++++------------------------ src/languages/node.rs | 5 -- src/languages/python/impl.rs | 5 -- src/languages/system.rs | 5 -- 7 files changed, 42 insertions(+), 118 deletions(-) diff --git a/src/hook.rs b/src/hook.rs index 246b89f..500b8c0 100644 --- a/src/hook.rs +++ b/src/hook.rs @@ -13,10 +13,10 @@ use url::Url; use crate::config::{ self, read_config, read_manifest, ConfigLocalHook, ConfigRemoteHook, ConfigRepo, ConfigWire, - ManifestHook, Stage, CONFIG_FILE, MANIFEST_FILE, + Language, ManifestHook, Stage, CONFIG_FILE, MANIFEST_FILE, }; use crate::fs::{Simplified, CWD}; -use crate::languages::{Language, DEFAULT_VERSION}; +use crate::languages::DEFAULT_VERSION; use crate::printer::Printer; use crate::store::Store; use crate::warn_user; @@ -340,8 +340,7 @@ impl HookBuilder { .and_then(|v| v.get(&language).cloned()); } if self.config.language_version.is_none() { - self.config.language_version = - Some(Language::from(language).default_version().to_string()); + self.config.language_version = Some(language.default_version().to_string()); } if self.config.stages.is_none() { @@ -374,7 +373,7 @@ impl HookBuilder { /// Check the hook configuration. fn check(&self) { - let language = Language::from(self.config.language); + let language = self.config.language; if language.environment_dir().is_none() { if self.config.language_version != Some(DEFAULT_VERSION.to_string()) { warn_user!( @@ -403,7 +402,7 @@ impl HookBuilder { id: self.config.id, name: self.config.name, entry: self.config.entry, - language: self.config.language.into(), + language: self.config.language, alias: self.config.alias.expect("alias not set"), files: self.config.files, exclude: self.config.exclude, diff --git a/src/languages/docker.rs b/src/languages/docker.rs index 7a61604..f713ea0 100644 --- a/src/languages/docker.rs +++ b/src/languages/docker.rs @@ -9,7 +9,6 @@ use anyhow::Result; use fancy_regex::Regex; use tracing::trace; -use crate::config::Language; use crate::fs::CWD; use crate::hook::Hook; use crate::languages::{LanguageImpl, DEFAULT_VERSION}; @@ -162,10 +161,6 @@ impl Docker { } impl LanguageImpl for Docker { - fn name(&self) -> Language { - Language::Docker - } - fn default_version(&self) -> &str { DEFAULT_VERSION } diff --git a/src/languages/fail.rs b/src/languages/fail.rs index 0d6489e..ba3741c 100644 --- a/src/languages/fail.rs +++ b/src/languages/fail.rs @@ -1,6 +1,5 @@ use std::{collections::HashMap, sync::Arc}; -use crate::config; use crate::hook::Hook; use crate::languages::{LanguageImpl, DEFAULT_VERSION}; @@ -8,10 +7,6 @@ use crate::languages::{LanguageImpl, DEFAULT_VERSION}; pub struct Fail; impl LanguageImpl for Fail { - fn name(&self) -> config::Language { - config::Language::Fail - } - fn default_version(&self) -> &str { DEFAULT_VERSION } diff --git a/src/languages/mod.rs b/src/languages/mod.rs index e9bd9be..66b7303 100644 --- a/src/languages/mod.rs +++ b/src/languages/mod.rs @@ -1,10 +1,9 @@ use std::collections::HashMap; -use std::fmt::{Debug, Display}; use std::sync::Arc; use anyhow::Result; -use crate::config; +use crate::config::Language; use crate::hook::Hook; mod docker; @@ -13,10 +12,15 @@ mod node; mod python; mod system; +static PYTHON: python::Python = python::Python; +static NODE: node::Node = node::Node; +static SYSTEM: system::System = system::System; +static FAIL: fail::Fail = fail::Fail; +static DOCKER: docker::Docker = docker::Docker; + pub const DEFAULT_VERSION: &str = "default"; trait LanguageImpl { - fn name(&self) -> config::Language; fn default_version(&self) -> &str; fn environment_dir(&self) -> Option<&str>; async fn install(&self, hook: &Hook) -> Result<()>; @@ -29,103 +33,48 @@ trait LanguageImpl { ) -> Result<(i32, Vec)>; } -#[derive(Debug, Copy, Clone)] -pub enum Language { - Python(python::Python), - Node(node::Node), - System(system::System), - Fail(fail::Fail), - Docker(docker::Docker), -} - -impl From for Language { - fn from(language: config::Language) -> Self { - match language { - // config::Language::Conda => Language::Conda, - // config::Language::Coursier => Language::Coursier, - // config::Language::Dart => Language::Dart, - config::Language::Docker => Language::Docker(docker::Docker), - // config::Language::DockerImage => Language::DockerImage, - // config::Language::Dotnet => Language::Dotnet, - config::Language::Fail => Language::Fail(fail::Fail), - // config::Language::Golang => Language::Golang, - // config::Language::Haskell => Language::Haskell, - // config::Language::Lua => Language::Lua, - config::Language::Node => Language::Node(node::Node), - // config::Language::Perl => Language::Perl, - config::Language::Python => Language::Python(python::Python), - // config::Language::R => Language::R, - // config::Language::Ruby => Language::Ruby, - // config::Language::Rust => Language::Rust, - // config::Language::Swift => Language::Swift, - // config::Language::Pygrep => Language::Pygrep, - // config::Language::Script => Language::Script, - config::Language::System => Language::System(system::System), - _ => todo!("Not implemented yet"), - } - } -} - -impl Display for Language { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - Self::Python(python) => python.fmt(f), - Self::Node(node) => node.fmt(f), - Self::System(system) => system.fmt(f), - Self::Fail(fail) => fail.fmt(f), - Self::Docker(docker) => docker.fmt(f), - } - } -} - impl Language { - pub fn name(self) -> config::Language { - match self { - Self::Python(python) => python.name(), - Self::Node(node) => node.name(), - Self::System(system) => system.name(), - Self::Fail(fail) => fail.name(), - Self::Docker(docker) => docker.name(), - } - } - pub fn default_version(&self) -> &str { match self { - Self::Python(python) => python.default_version(), - Self::Node(node) => node.default_version(), - Self::System(system) => system.default_version(), - Self::Fail(fail) => fail.default_version(), - Self::Docker(docker) => docker.default_version(), + Self::Python => PYTHON.default_version(), + Self::Node => NODE.default_version(), + Self::System => SYSTEM.default_version(), + Self::Fail => FAIL.default_version(), + Self::Docker => DOCKER.default_version(), + _ => todo!(), } } pub fn environment_dir(&self) -> Option<&str> { match self { - Self::Python(python) => python.environment_dir(), - Self::Node(node) => node.environment_dir(), - Self::System(system) => system.environment_dir(), - Self::Fail(fail) => fail.environment_dir(), - Self::Docker(docker) => docker.environment_dir(), + Self::Python => PYTHON.environment_dir(), + Self::Node => NODE.environment_dir(), + Self::System => SYSTEM.environment_dir(), + Self::Fail => FAIL.environment_dir(), + Self::Docker => DOCKER.environment_dir(), + _ => todo!(), } } pub async fn install(&self, hook: &Hook) -> Result<()> { match self { - Self::Python(python) => python.install(hook).await, - Self::Node(node) => node.install(hook).await, - Self::System(system) => system.install(hook).await, - Self::Fail(fail) => fail.install(hook).await, - Self::Docker(docker) => docker.install(hook).await, + Self::Python => PYTHON.install(hook).await, + Self::Node => NODE.install(hook).await, + Self::System => SYSTEM.install(hook).await, + Self::Fail => FAIL.install(hook).await, + Self::Docker => DOCKER.install(hook).await, + _ => todo!(), } } pub async fn check_health(&self) -> Result<()> { match self { - Self::Python(python) => python.check_health().await, - Self::Node(node) => node.check_health().await, - Self::System(system) => system.check_health().await, - Self::Fail(fail) => fail.check_health().await, - Self::Docker(docker) => docker.check_health().await, + Self::Python => PYTHON.check_health().await, + Self::Node => NODE.check_health().await, + Self::System => SYSTEM.check_health().await, + Self::Fail => FAIL.check_health().await, + Self::Docker => DOCKER.check_health().await, + _ => todo!(), } } @@ -136,11 +85,12 @@ impl Language { env_vars: Arc>, ) -> Result<(i32, Vec)> { match self { - Self::Python(python) => python.run(hook, filenames, env_vars).await, - Self::Node(node) => node.run(hook, filenames, env_vars).await, - Self::System(system) => system.run(hook, filenames, env_vars).await, - Self::Fail(fail) => fail.run(hook, filenames, env_vars).await, - Self::Docker(docker) => docker.run(hook, filenames, env_vars).await, + Self::Python => PYTHON.run(hook, filenames, env_vars).await, + Self::Node => NODE.run(hook, filenames, env_vars).await, + Self::System => SYSTEM.run(hook, filenames, env_vars).await, + Self::Fail => FAIL.run(hook, filenames, env_vars).await, + Self::Docker => DOCKER.run(hook, filenames, env_vars).await, + _ => todo!(), } } } diff --git a/src/languages/node.rs b/src/languages/node.rs index c4693e7..a40d7ae 100644 --- a/src/languages/node.rs +++ b/src/languages/node.rs @@ -1,7 +1,6 @@ use std::collections::HashMap; use std::sync::Arc; -use crate::config; use crate::hook::Hook; use crate::languages::{LanguageImpl, DEFAULT_VERSION}; @@ -9,10 +8,6 @@ use crate::languages::{LanguageImpl, DEFAULT_VERSION}; pub struct Node; impl LanguageImpl for Node { - fn name(&self) -> config::Language { - config::Language::Node - } - fn default_version(&self) -> &str { DEFAULT_VERSION } diff --git a/src/languages/python/impl.rs b/src/languages/python/impl.rs index b2d7005..759558e 100644 --- a/src/languages/python/impl.rs +++ b/src/languages/python/impl.rs @@ -2,7 +2,6 @@ use std::collections::HashMap; use std::path::{Path, PathBuf}; use std::sync::Arc; -use crate::config; use crate::hook::Hook; use crate::languages::python::uv::ensure_uv; use crate::languages::LanguageImpl; @@ -13,10 +12,6 @@ use crate::run::run_by_batch; pub struct Python; impl LanguageImpl for Python { - fn name(&self) -> config::Language { - config::Language::Python - } - fn default_version(&self) -> &str { // TODO find the version of python on the system "python3" diff --git a/src/languages/system.rs b/src/languages/system.rs index ea542e3..804dadb 100644 --- a/src/languages/system.rs +++ b/src/languages/system.rs @@ -1,7 +1,6 @@ use std::collections::HashMap; use std::sync::Arc; -use crate::config; use crate::hook::Hook; use crate::languages::{LanguageImpl, DEFAULT_VERSION}; use crate::process::Cmd; @@ -11,10 +10,6 @@ use crate::run::run_by_batch; pub struct System; impl LanguageImpl for System { - fn name(&self) -> config::Language { - config::Language::System - } - fn default_version(&self) -> &str { DEFAULT_VERSION }