Add macros crate for utility macros

This commit is contained in:
s1idewhist1e 2023-05-13 16:56:38 -07:00
parent c34aab6ac8
commit 4fb4597e7b
No known key found for this signature in database
GPG key ID: 75E83831162775BB
6 changed files with 77 additions and 1 deletions

View file

@ -15,6 +15,7 @@ dependencies = [
"config",
"lazy_static",
"logging",
"macros",
"queue",
"server",
]
@ -63,6 +64,13 @@ dependencies = [
"config",
]
[[package]]
name = "macros"
version = "0.1.0"
dependencies = [
"lazy_static",
]
[[package]]
name = "once_cell"
version = "1.17.1"

View file

@ -21,6 +21,7 @@ server = { path = "crates/server" }
logging = { path = "crates/logging" }
queue = { path = "crates/queue" }
config = { path = "crates/config" }
macros = { path = "crates/macros" }
lazy_static = "1.4.0"
[dev-dependencies]

View file

@ -0,0 +1,9 @@
[package]
name = "macros"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
lazy_static = "1.4.0"

View file

@ -0,0 +1,54 @@
#![macro_use]
use std::env;
use lazy_static::lazy_static;
#[derive(PartialEq)]
pub enum EnvType {
Release,
Debug,
Test,
}
lazy_static! {
pub static ref NODE_ENV: EnvType = init_env_type();
}
#[macro_export]
macro_rules! node_env {
() => {
*macros::environment::NODE_ENV
};
}
#[macro_export]
macro_rules! is_debug {
() => {
macros::node_env!() == macros::environment::EnvType::Debug
};
}
#[macro_export]
macro_rules! is_release {
() => {
macros::node_env!() == macros::environment::EnvType::Release
};
}
#[macro_export]
macro_rules! is_test {
() => {
macros::node_env!() == macros::environment::EnvType::Test
};
}
fn init_env_type() -> EnvType {
use EnvType::*;
match env::var("NODE_ENV") {
Ok(s) if s == *"production" => Release,
Ok(s) if s == *"development" => Debug,
Ok(s) if s == *"test" => Test,
_ => Debug,
}
}

View file

@ -0,0 +1,3 @@
pub mod environment;
//pub use environment::*;

View file

@ -3,7 +3,8 @@ use std::{
path::{Path, PathBuf},
};
extern crate config;
#[macro_use]
extern crate macros;
fn main() -> Result<(), Box<dyn error::Error>> {
env::set_var(