Add macros crate for utility macros
This commit is contained in:
parent
c34aab6ac8
commit
4fb4597e7b
6 changed files with 77 additions and 1 deletions
8
packages/backend/Cargo.lock
generated
8
packages/backend/Cargo.lock
generated
|
@ -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"
|
||||
|
|
|
@ -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]
|
||||
|
|
9
packages/backend/crates/macros/Cargo.toml
Normal file
9
packages/backend/crates/macros/Cargo.toml
Normal 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"
|
54
packages/backend/crates/macros/src/environment.rs
Normal file
54
packages/backend/crates/macros/src/environment.rs
Normal 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,
|
||||
}
|
||||
}
|
3
packages/backend/crates/macros/src/lib.rs
Normal file
3
packages/backend/crates/macros/src/lib.rs
Normal file
|
@ -0,0 +1,3 @@
|
|||
pub mod environment;
|
||||
|
||||
//pub use environment::*;
|
|
@ -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(
|
||||
|
|
Loading…
Reference in a new issue