use errors::DirectoryError;
#[cfg(feature = "audio")]
pub mod audio;
pub mod errors;
pub mod logger;
#[cfg(feature = "rpc")]
pub mod rpc;
pub mod state;
#[cfg(test)]
pub mod test_utils;
#[cfg(test)]
extern crate rstest_reuse;
#[macro_export]
macro_rules! function_name {
() => {{
fn f() {}
fn type_name_of<T>(_: T) -> &'static str {
std::any::type_name::<T>()
}
let name = type_name_of(f);
&name[..name.len() - 3]
}};
}
#[must_use]
pub fn format_duration(duration: &std::time::Duration) -> String {
let total_seconds = duration.as_secs();
let hours = total_seconds / 3600;
let minutes = (total_seconds % 3600) / 60;
let seconds = duration.as_secs_f32() % 60.;
format!("{hours:02}:{minutes:02}:{seconds:05.2}")
}
pub fn get_data_dir() -> Result<std::path::PathBuf, DirectoryError> {
let directory = if let Ok(s) = std::env::var("MECOMP_DATA") {
std::path::PathBuf::from(s)
} else if let Some(proj_dirs) = directories::ProjectDirs::from("", "", "mecomp") {
proj_dirs.data_local_dir().to_path_buf()
} else {
return Err(DirectoryError::Data);
};
Ok(directory)
}
pub fn get_config_dir() -> Result<std::path::PathBuf, DirectoryError> {
let directory = if let Ok(s) = std::env::var("MECOMP_CONFIG") {
std::path::PathBuf::from(s)
} else if let Some(proj_dirs) = directories::ProjectDirs::from("", "", "mecomp") {
proj_dirs.config_local_dir().to_path_buf()
} else {
return Err(DirectoryError::Config);
};
Ok(directory)
}
#[must_use]
pub fn is_server_running(port: u16) -> bool {
std::net::TcpStream::connect(format!("localhost:{port}")).is_ok()
}
#[cfg(test)]
mod test {
use super::format_duration;
use pretty_assertions::assert_eq;
use rstest::rstest;
use std::time::Duration;
#[rstest]
#[case::zero(Duration::from_secs(0), "00:00:00.00")]
#[case::sub_second(Duration::from_millis(100), "00:00:00.10")]
#[case::sub_second(Duration::from_millis(101), "00:00:00.10")]
#[case::one_second(Duration::from_secs(1), "00:00:01.00")]
#[case::one_minute(Duration::from_secs(60), "00:01:00.00")]
#[case::one_hour(Duration::from_secs(3600), "01:00:00.00")]
#[case::one_hour_one_minute_one_second(Duration::from_secs(3661), "01:01:01.00")]
#[case(Duration::from_secs(3600 + 120 + 1), "01:02:01.00")]
fn test_format_duration(#[case] duration: Duration, #[case] expected: &str) {
let actual = format_duration(&duration);
assert_eq!(actual, expected);
}
#[test]
fn test_function_name() {
fn test_function() {
let result = super::function_name!();
assert!(result.ends_with("test_function"));
}
test_function();
}
#[test]
fn test_get_data_dir() {
let data_dir = super::get_data_dir().unwrap();
assert_eq!(
data_dir
.components()
.last()
.unwrap()
.as_os_str()
.to_string_lossy(),
"mecomp"
);
}
#[test]
fn test_get_config_dir() {
let config_dir = super::get_config_dir().unwrap();
assert_eq!(
config_dir
.components()
.last()
.unwrap()
.as_os_str()
.to_string_lossy(),
"mecomp"
);
}
}