Skip to content

Commit

Permalink
Remove intermedia Language enum (#107)
Browse files Browse the repository at this point in the history
* Remove intermedia Language enum

* fix

* Simplify
  • Loading branch information
j178 authored Nov 23, 2024
1 parent 3113e84 commit 686b69f
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 118 deletions.
11 changes: 5 additions & 6 deletions src/hook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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!(
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 0 additions & 5 deletions src/languages/docker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -162,10 +161,6 @@ impl Docker {
}

impl LanguageImpl for Docker {
fn name(&self) -> Language {
Language::Docker
}

fn default_version(&self) -> &str {
DEFAULT_VERSION
}
Expand Down
5 changes: 0 additions & 5 deletions src/languages/fail.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
use std::{collections::HashMap, sync::Arc};

use crate::config;
use crate::hook::Hook;
use crate::languages::{LanguageImpl, DEFAULT_VERSION};

#[derive(Debug, Copy, Clone)]
pub struct Fail;

impl LanguageImpl for Fail {
fn name(&self) -> config::Language {
config::Language::Fail
}

fn default_version(&self) -> &str {
DEFAULT_VERSION
}
Expand Down
124 changes: 37 additions & 87 deletions src/languages/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<()>;
Expand All @@ -29,103 +33,48 @@ trait LanguageImpl {
) -> Result<(i32, Vec<u8>)>;
}

#[derive(Debug, Copy, Clone)]
pub enum Language {
Python(python::Python),
Node(node::Node),
System(system::System),
Fail(fail::Fail),
Docker(docker::Docker),
}

impl From<config::Language> 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!(),
}
}

Expand All @@ -136,11 +85,12 @@ impl Language {
env_vars: Arc<HashMap<&'static str, String>>,
) -> Result<(i32, Vec<u8>)> {
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!(),
}
}
}
5 changes: 0 additions & 5 deletions src/languages/node.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
use std::collections::HashMap;
use std::sync::Arc;

use crate::config;
use crate::hook::Hook;
use crate::languages::{LanguageImpl, DEFAULT_VERSION};

#[derive(Debug, Copy, Clone)]
pub struct Node;

impl LanguageImpl for Node {
fn name(&self) -> config::Language {
config::Language::Node
}

fn default_version(&self) -> &str {
DEFAULT_VERSION
}
Expand Down
5 changes: 0 additions & 5 deletions src/languages/python/impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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"
Expand Down
5 changes: 0 additions & 5 deletions src/languages/system.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
}
Expand Down

0 comments on commit 686b69f

Please sign in to comment.