Skip to content

Commit

Permalink
feat: S! & C! macros
Browse files Browse the repository at this point in the history
  • Loading branch information
mrjackwills committed Oct 19, 2024
1 parent 8c333d2 commit 5959492
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 78 deletions.
57 changes: 28 additions & 29 deletions src/app_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{collections::HashMap, env, fmt, time::SystemTime};
use time::UtcOffset;
use time_tz::{timezones, Offset, TimeZone};

use crate::app_error::AppError;
use crate::{app_error::AppError, S};

type EnvHashMap = HashMap<String, String>;

Expand All @@ -15,7 +15,7 @@ impl EnvTimeZone {
if timezones::get_by_name(&x).is_some() {
Self(x)
} else {
Self("Etc/UTC".into())
Self(S!("Etc/UTC"))
}
}

Expand Down Expand Up @@ -165,12 +165,14 @@ impl AppEnv {
#[cfg(test)]
#[expect(clippy::unwrap_used)]
mod tests {
use crate::S;

use super::*;

#[test]
fn env_missing_env() {
let mut map = HashMap::new();
map.insert("not_fish".to_owned(), "not_fish".to_owned());
map.insert(S!("not_fish"), S!("not_fish"));
// ACTION
let result = AppEnv::parse_string("fish", &map);

Expand All @@ -183,7 +185,7 @@ mod tests {
fn env_parse_string_valid() {
// FIXTURES
let mut map = HashMap::new();
map.insert("LOCATION_SQLITE".to_owned(), "/alarms.db".to_owned());
map.insert(S!("LOCATION_SQLITE"), S!("/alarms.db"));

// ACTION
let result = AppEnv::parse_string("LOCATION_SQLITE", &map).unwrap();
Expand All @@ -196,9 +198,9 @@ mod tests {
fn env_parse_boolean_ok() {
// FIXTURES
let mut map = HashMap::new();
map.insert("valid_true".to_owned(), "true".to_owned());
map.insert("valid_false".to_owned(), "false".to_owned());
map.insert("invalid_but_false".to_owned(), "as".to_owned());
map.insert(S!("valid_true"), S!("true"));
map.insert(S!("valid_false"), S!("false"));
map.insert(S!("invalid_but_false"), S!("as"));

// ACTION
let result01 = AppEnv::parse_boolean("valid_true", &map);
Expand All @@ -217,7 +219,7 @@ mod tests {
fn env_parse_rotation_ok() {
// FIXTURES
let mut map = HashMap::new();
map.insert("ROTATION".to_owned(), "90".to_owned());
map.insert(S!("ROTATION"), S!("90"));

// ACTION
let result = AppEnv::parse_rotation(&map);
Expand All @@ -227,7 +229,7 @@ mod tests {

// FIXTURES
let mut map = HashMap::new();
map.insert("ROTATION".to_owned(), "180".to_owned());
map.insert(S!("ROTATION"), S!("180"));

// ACTION
let result = AppEnv::parse_rotation(&map);
Expand All @@ -237,7 +239,7 @@ mod tests {

// FIXTURES
let mut map = HashMap::new();
map.insert("ROTATION".to_owned(), "270".to_owned());
map.insert(S!("ROTATION"), S!("270"));

// ACTION
let result = AppEnv::parse_rotation(&map);
Expand All @@ -247,7 +249,7 @@ mod tests {

// FIXTURES
let mut map = HashMap::new();
map.insert("ROTATION".to_owned(), "0".to_owned());
map.insert(S!("ROTATION"), S!("0"));

// ACTION
let result = AppEnv::parse_rotation(&map);
Expand All @@ -257,7 +259,7 @@ mod tests {

// FIXTURES
let mut map = HashMap::new();
map.insert("ROTATION".to_owned(), "181".to_owned());
map.insert(S!("ROTATION"), S!("181"));

// ACTION
let result = AppEnv::parse_rotation(&map);
Expand All @@ -266,7 +268,7 @@ mod tests {
assert_eq!(result, Rotation::Zero);

let mut map = HashMap::new();
map.insert("ROTATION".to_owned(), String::new());
map.insert(S!("ROTATION"), S!());

// ACTION
let result = AppEnv::parse_rotation(&map);
Expand All @@ -288,7 +290,7 @@ mod tests {
fn env_parse_timezone_ok() {
// FIXTURES
let mut map = HashMap::new();
map.insert("TIMEZONE".to_owned(), "America/New_York".to_owned());
map.insert(S!("TIMEZONE"), S!("America/New_York"));

// ACTION
let result = AppEnv::parse_timezone(&map);
Expand All @@ -297,7 +299,7 @@ mod tests {
assert_eq!(result.0, "America/New_York");

let mut map = HashMap::new();
map.insert("TIMEZONE".to_owned(), "Europe/Berlin".to_owned());
map.insert(S!("TIMEZONE"), S!("Europe/Berlin"));

// ACTION
let result = AppEnv::parse_timezone(&map);
Expand All @@ -319,7 +321,7 @@ mod tests {
fn env_parse_timezone_err() {
// FIXTURES
let mut map = HashMap::new();
map.insert("TIMEZONE".to_owned(), "america/New_York".to_owned());
map.insert(S!("TIMEZONE"), S!("america/New_York"));

// ACTION
let result = AppEnv::parse_timezone(&map);
Expand All @@ -338,7 +340,7 @@ mod tests {
#[test]
fn env_parse_log_valid() {
// FIXTURES
let map = HashMap::from([("RANDOM_STRING".to_owned(), "123".to_owned())]);
let map = HashMap::from([(S!("RANDOM_STRING"), S!("123"))]);

// ACTION
let result = AppEnv::parse_log(&map);
Expand All @@ -347,7 +349,7 @@ mod tests {
assert_eq!(result, tracing::Level::INFO);

// FIXTURES
let map = HashMap::from([("LOG_DEBUG".to_owned(), "false".to_owned())]);
let map = HashMap::from([(S!("LOG_DEBUG"), S!("false"))]);

// ACTION
let result = AppEnv::parse_log(&map);
Expand All @@ -356,7 +358,7 @@ mod tests {
assert_eq!(result, tracing::Level::INFO);

// FIXTURES
let map = HashMap::from([("LOG_TRACE".to_owned(), "false".to_owned())]);
let map = HashMap::from([(S!("LOG_TRACE"), S!("false"))]);

// ACTION
let result = AppEnv::parse_log(&map);
Expand All @@ -366,8 +368,8 @@ mod tests {

// FIXTURES
let map = HashMap::from([
("LOG_DEBUG".to_owned(), "false".to_owned()),
("LOG_TRACE".to_owned(), "false".to_owned()),
(S!("LOG_DEBUG"), S!("false")),
(S!("LOG_TRACE"), S!("false")),
]);

// ACTION
Expand All @@ -378,8 +380,8 @@ mod tests {

// FIXTURES
let map = HashMap::from([
("LOG_DEBUG".to_owned(), "true".to_owned()),
("LOG_TRACE".to_owned(), "false".to_owned()),
(S!("LOG_DEBUG"), S!("true")),
(S!("LOG_TRACE"), S!("false")),
]);

// ACTION
Expand All @@ -389,10 +391,7 @@ mod tests {
assert_eq!(result, tracing::Level::DEBUG);

// FIXTURES
let map = HashMap::from([
("LOG_DEBUG".to_owned(), "true".to_owned()),
("LOG_TRACE".to_owned(), "true".to_owned()),
]);
let map = HashMap::from([(S!("LOG_DEBUG"), S!("true")), (S!("LOG_TRACE"), S!("true"))]);

// ACTION
let result = AppEnv::parse_log(&map);
Expand All @@ -402,8 +401,8 @@ mod tests {

// FIXTURES
let map = HashMap::from([
("LOG_DEBUG".to_owned(), "false".to_owned()),
("LOG_TRACE".to_owned(), "true".to_owned()),
(S!("LOG_DEBUG"), S!("false")),
(S!("LOG_TRACE"), S!("true")),
]);

// ACTION
Expand Down
9 changes: 6 additions & 3 deletions src/camera.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::app_env::{AppEnv, EnvTimeZone};
use crate::{
app_env::{AppEnv, EnvTimeZone},
C,
};
use image::imageops::FilterType;
use std::{
io::Cursor,
Expand Down Expand Up @@ -66,8 +69,8 @@ impl Camera {
image_timestamp: SystemTime::now(),
file_size: FileSize::default(),
rotation: app_envs.rotation.to_string(),
timezone: app_envs.timezone.clone(),
location_images: app_envs.location_images.clone(),
timezone: C!(app_envs.timezone),
location_images: C!(app_envs.location_images),
};
let photo_buffer = camera.photograph().await;
camera.convert_to_webp(&photo_buffer).await;
Expand Down
19 changes: 19 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,25 @@ use ws::open_connection;

const LOGS_NAME: &str = "leafcast.log";

/// Simple macro to create a new String, or convert from a &str to a String - basically just gets rid of String::from() / .to_owned() etc
#[macro_export]
macro_rules! S {
() => {
String::new()
};
($s:expr) => {
String::from($s)
};
}

/// Simple macro to call `.clone()` on whatever is passed in
#[macro_export]
macro_rules! C {
($i:expr) => {
$i.clone()
};
}

fn setup_tracing(app_env: &AppEnv) -> Result<(), AppError> {
let logfile = tracing_appender::rolling::never(&app_env.location_log, LOGS_NAME);

Expand Down
26 changes: 13 additions & 13 deletions src/sysinfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::time::Instant;
use serde::{Deserialize, Serialize};
use tokio::fs::read_to_string;

use crate::{app_env::AppEnv, app_error::AppError};
use crate::{app_env::AppEnv, app_error::AppError, C, S};

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct SysInfo {
Expand Down Expand Up @@ -38,10 +38,10 @@ impl SysInfo {
}

async fn get_ip(app_envs: &AppEnv) -> String {
let na = String::from("N/A");
let na = S!("N/A");
let ip = read_to_string(&app_envs.location_ip_address)
.await
.unwrap_or_else(|_| na.clone());
.unwrap_or_else(|_| C!(na));
let output = if ip.len() > 1 {
ip.trim().to_owned()
} else {
Expand Down Expand Up @@ -85,26 +85,26 @@ mod tests {
use super::*;

fn gen_app_env(location_ip_address: String) -> AppEnv {
let na = String::from("na");
let na = S!("na");
AppEnv {
location_images: String::from("photos"),
location_images: S!("photos"),
location_ip_address,
location_log: na.clone(),
location_log: C!(na),
log_level: tracing::Level::INFO,
rotation: Rotation::Zero,
start_time: SystemTime::now(),
timezone: EnvTimeZone::new("America/New_York"),
ws_address: na.clone(),
ws_apikey: na.clone(),
ws_password: na.clone(),
ws_address: C!(na),
ws_apikey: C!(na),
ws_password: C!(na),
ws_token_address: na,
}
}

#[tokio::test]
async fn sysinfo_getuptime_ok() {
// FIXTURES
gen_app_env(String::from("ip.addr"));
gen_app_env(S!("ip.addr"));

// ACTIONS
let result = SysInfo::get_uptime().await;
Expand All @@ -117,7 +117,7 @@ mod tests {
#[tokio::test]
async fn sysinfo_get_ip_na() {
// FIXTURES
let app_envs = gen_app_env(String::from("na"));
let app_envs = gen_app_env(S!("na"));

// ACTIONS
let result = SysInfo::get_ip(&app_envs).await;
Expand All @@ -129,7 +129,7 @@ mod tests {
#[tokio::test]
async fn sysinfo_get_ip_ok() {
// FIXTURES
let app_envs = gen_app_env(String::from("ip.addr"));
let app_envs = gen_app_env(S!("ip.addr"));
// ACTIONS
let result = SysInfo::get_ip(&app_envs).await;

Expand All @@ -140,7 +140,7 @@ mod tests {
#[tokio::test]
async fn sysinfo_get_sysinfo_ok() {
// FIXTURES
let app_envs = gen_app_env(String::from("ip.addr"));
let app_envs = gen_app_env(S!("ip.addr"));
tokio::time::sleep(std::time::Duration::from_secs(1)).await;

let now = Instant::now();
Expand Down
Loading

0 comments on commit 5959492

Please sign in to comment.