#[macro_export]
macro_rules! akp {
($($arg:tt)*) => {
println!($($arg)*);
};
}
#[macro_export]
macro_rules! input_prompt {
($prompt:expr) => {{
use std::io::{stdin, stdout, Write};
print!("{}", $prompt);
let _ = stdout().flush();
let mut input = String::new();
stdin().read_line(&mut input).expect("Failed to read input");
input.trim().to_string() }};
}
#[macro_export]
macro_rules! remove_folder {
($path:expr) => {
use std::fs;
if let Err(e) = fs::remove_dir($path) {
eprintln!("Error removing: {}", e);
} else {
println!("Done...!");
}
};
}
#[macro_export]
macro_rules! remove_file {
($path:expr) => {
if let Err(e) = fs::remove_file($path) {
eprintln!("Error removing: {}", e);
} else {
println!("Done...!");
}
};
}
#[macro_export]
macro_rules! remove_all_folders {
($path:expr) => {
if let Err(e) = fs::remove_dir_all($path) {
eprintln!("Error removing: {}", e);
} else {
println!("Done...!");
}
};
}
#[macro_export]
macro_rules! use_loop {
($should_execute:expr, $start_number:expr, $end_number:expr, $theVar:ident, $the_method:expr) => {
for $theVar in $start_number..$end_number {
if $should_execute {
$the_method;
}
}
};
}
#[macro_export]
macro_rules! if_cond {
($var:expr, $condition:expr, $first_method:expr, $last_method:expr) => {
if $condition {
$first_method
} else {
$last_method
}
};
($name:expr, $cond:expr, $action:expr) => {{
if $cond {
$action;
}
}};
($name:expr, $($cond:expr => $action:expr),*) => {{
$(if $cond { $action; })*
}};
($name:expr, $($cond:expr => $action:expr),* $(,)?) => {{
$(if $cond { $action; })*
}};
}
#[macro_export]
macro_rules! terminal {
($lang:expr, $command:expr) => {{
use std::fs::File;
use std::io::Write;
use std::process::Command;
let output = Command::new($lang)
.arg("-c")
.arg($command)
.output()
.expect("failed to execute process");
let output_str = String::from_utf8_lossy(&output.stdout);
let command_output = output_str.trim().to_string();
command_output }};
}
#[macro_export]
macro_rules! this_OS {
() => {
std::env::consts::OS
};
}
#[macro_export]
macro_rules! use_rand {
($var:ident, {$($method:tt)*}) => {
use rand::prelude::*;
let mut rng = rand::thread_rng();
let $var: i32 = rng.gen();
$($method)*
};
($var:ident, $kind:ty, {$($method:tt)*}) => {
use rand::prelude::*;
let mut rng = rand::thread_rng();
let $var: $kind = rng.gen();
$($method)*
};
}
#[macro_export]
macro_rules! use_fetch {
($api:expr, $method:ident) => {{
extern crate reqwest;
use std::collections::HashMap;
let api = $api.to_string();
let method = stringify!($method).to_string();
let response = reqwest::Client::new()
.request(reqwest::Method::from_bytes(method.as_bytes())?, &api)
.send()
.await?;
let resp_json = response
.json::<Vec<HashMap<String, serde_json::Value>>>()
.await?;
for item in resp_json {
println!("{:#?}", item);
}
Ok(()) as Result<(), Box<dyn std::error::Error>>
}};
}
#[macro_export]
macro_rules! use_zip {
($zip_file_location:expr, $extract_file_location:expr) => {{
use ak_macros::use_zip;
use std::fs::File;
use std::io::Cursor;
use std::io::{self, Read};
use std::path::PathBuf;
use zip_extract::ZipExtractError;
let input_value = $zip_file_location;
let target_path = $extract_file_location;
let file = std::fs::File::open(input_value.trim())?;
let mut bytes = Vec::new();
file.take(1024 * 1024 * 100) .read_to_end(&mut bytes)?;
let target = std::path::PathBuf::from(target_path.trim());
let cursor = std::io::Cursor::new(bytes);
match zip_extract::extract(cursor, &target, false) {
Ok(_) => {
println!("Extraction successful!");
Ok(())
}
Err(err) => match err {
zip_extract::ZipExtractError::Io(io_err) => Err(io_err),
other_err => Err(std::io::Error::new(
std::io::ErrorKind::Other,
format!("Zip extraction error: {:?}", other_err),
)),
},
}
}};
}
#[macro_export]
macro_rules! this_month {
() => {{
use std::time::{SystemTime, UNIX_EPOCH};
let now = SystemTime::now();
let since_epoch = now.duration_since(UNIX_EPOCH).expect("Time went backwards");
let seconds_since_epoch = since_epoch.as_secs();
let remaining_seconds = seconds_since_epoch % (365 * 24 * 60 * 60);
let month = remaining_seconds / (30 * 24 * 60 * 60) + 1;
month
}};
}
#[macro_export]
macro_rules! this_year {
() => {{
use std::time::{SystemTime, UNIX_EPOCH};
let now = SystemTime::now();
let since_epoch = now.duration_since(UNIX_EPOCH).expect("Time went backwards");
let seconds_since_epoch = since_epoch.as_secs();
let _ = seconds_since_epoch % (365 * 24 * 60 * 60);
let year = 1970 + seconds_since_epoch / (365 * 24 * 60 * 60);
year
}};
}
#[macro_export]
macro_rules! open_Web {
($website_url:expr) => {{
let url = $website_url;
let result = if cfg!(target_os = "windows") {
std::process::Command::new("cmd")
.arg("/C")
.arg("start")
.arg(url)
.spawn()
} else if cfg!(target_os = "macos") {
std::process::Command::new("open").arg(url).spawn()
} else {
std::process::Command::new("xdg-open").arg(url).spawn()
};
match result {
Ok(_) => println!("Successfully opened website."),
Err(e) => eprintln!("Failed to open website: {}", e),
}
}};
}
#[macro_export]
macro_rules! use_upper_case {
($input:ident) => {
$input = $input.to_uppercase();
};
}
#[macro_export]
macro_rules! use_lower_case {
($input:ident) => {
$input = $input.to_lowercase();
};
}
#[macro_export]
macro_rules! use_createFile {
($file_name:expr, $file_path:expr,$input_text:expr) => {
use std::fs::File;
use std::io::prelude::*;
use std::path::{Path, PathBuf};
let file_path = PathBuf::from($file_path).join($file_name); let mut file = File::create(&file_path)?; file.write_all($input_text.as_bytes())?; };
}
#[macro_export]
macro_rules! set_String {
($the_text:expr) => {
String::from($the_text)
};
}
#[macro_export]
macro_rules! Positive_number {
($num:expr) => {{
let num = $num;
if num < 0 {
panic!("Number must be positive!");
}
num as usize
}};
}
#[macro_export]
macro_rules! Negative_number {
($num:expr) => {{
let num = $num;
if num > 0 {
panic!("Number must be Negative!");
}
num as isize
}};
}
#[macro_export]
macro_rules! Ram_size {
() => {{
let mut ram_gb = 0.0;
if let Ok(meminfo) = std::fs::read_to_string("/proc/meminfo") {
for line in meminfo.lines() {
if line.starts_with("MemTotal:") {
let ram_kb: u64 = line.split_whitespace().nth(1).unwrap().parse().unwrap();
ram_gb = ram_kb as f64 / 1024.0 / 1024.0;
break;
}
}
} else {
eprintln!("Failed to read /proc/meminfo");
}
ram_gb
}};
}
#[macro_export]
macro_rules! Get_CPU {
() => {{
let mut cpu_name = String::new();
if let Ok(cpuinfo) = std::fs::read_to_string("/proc/cpuinfo") {
for line in cpuinfo.lines() {
if line.starts_with("model name") {
let parts: Vec<&str> = line.split(":").collect();
if parts.len() >= 2 {
cpu_name = parts[1].trim().to_string();
break; }
}
}
} else {
println!("Failed to read CPU info.");
}
cpu_name
}};
}
#[macro_export]
macro_rules! use_loopAt {
($arr:expr, $var:ident, $method:tt) => {
for $var in $arr {
$method
}
};
($arr:expr, $method:tt) => {
for ii in $arr {
$method
}
};
}
#[macro_export]
macro_rules! use_time {
($expr:expr) => {
use std::thread;
use std::time::Duration;
thread::spawn(|| {
thread::sleep(Duration::from_secs($expr));
})
.join()
.unwrap();
};
}