mecomp_core/
lib.rs

1#![deny(clippy::missing_inline_in_public_items)]
2
3use errors::DirectoryError;
4
5#[cfg(feature = "audio")]
6pub mod audio;
7pub mod config;
8pub mod errors;
9pub mod logger;
10#[cfg(feature = "rpc")]
11pub mod rpc;
12pub mod state;
13#[cfg(any(test, feature = "test_utils"))]
14pub mod test_utils;
15#[cfg(feature = "rpc")]
16pub mod udp;
17
18#[cfg(test)]
19extern crate rstest_reuse;
20
21/// This macro returns the name of the enclosing function.
22/// As the internal implementation is based on the [`std::any::type_name`], this macro derives
23/// all the limitations of this function.
24///
25/// ## Examples
26///
27/// ```rust
28/// mod bar {
29///     pub fn sample_function() {
30///         use mecomp_core::function_name;
31///         assert!(function_name!().ends_with("bar::sample_function"));
32///     }
33/// }
34///
35/// bar::sample_function();
36/// ```
37///
38/// [`std::any::type_name`]: https://doc.rust-lang.org/std/any/fn.type_name.html
39///
40/// # Note
41///
42/// This macro is copied from the `stdext` crate. <https://github.com/popzxc/stdext-rs>
43#[macro_export]
44macro_rules! function_name {
45    () => {{
46        // Okay, this is ugly, I get it. However, this is the best we can get on a stable rust.
47        const fn f() {}
48        fn type_name_of<T>(_: T) -> &'static str {
49            std::any::type_name::<T>()
50        }
51        let name = type_name_of(f);
52        // `3` is the length of the `::f`.
53        &name[..name.len() - 3]
54    }};
55}
56
57#[must_use]
58#[inline]
59pub fn format_duration(duration: &std::time::Duration) -> String {
60    let total_seconds = duration.as_secs();
61    let hours = total_seconds / 3600;
62    let minutes = (total_seconds % 3600) / 60;
63    let seconds = duration.as_secs_f32() % 60.;
64
65    format!("{hours:02}:{minutes:02}:{seconds:05.2}")
66}
67
68/// Get the data directory for the application.
69///
70/// Follows the XDG Base Directory Specification for linux, and the equivalents on other platforms.
71/// See the [`directories`](https://docs.rs/directories/latest/directories/) crate for more information.
72///
73/// # Errors
74///
75/// This function will return an error if the data directory could not be found.
76#[inline]
77pub fn get_data_dir() -> Result<std::path::PathBuf, DirectoryError> {
78    let directory = if let Ok(s) = std::env::var("MECOMP_DATA") {
79        std::path::PathBuf::from(s)
80    } else if let Some(proj_dirs) = directories::ProjectDirs::from("", "", "mecomp") {
81        proj_dirs.data_local_dir().to_path_buf()
82    } else {
83        return Err(DirectoryError::Data);
84    };
85    Ok(directory)
86}
87
88/// Get the config directory for the application.
89///
90/// Follows the XDG Base Directory Specification for linux, and the equivalents on other platforms.
91/// See the [`directories`](https://docs.rs/directories/latest/directories/) crate for more information.
92///
93/// # Errors
94///
95/// This function will return an error if the config directory could not be found.
96#[inline]
97pub fn get_config_dir() -> Result<std::path::PathBuf, DirectoryError> {
98    let directory = if let Ok(s) = std::env::var("MECOMP_CONFIG") {
99        std::path::PathBuf::from(s)
100    } else if let Some(proj_dirs) = directories::ProjectDirs::from("", "", "mecomp") {
101        proj_dirs.config_local_dir().to_path_buf()
102    } else {
103        return Err(DirectoryError::Config);
104    };
105    Ok(directory)
106}
107
108/// Check if a server is already running on localhost on the given port.
109/// If a server is already running, return true, otherwise return false.
110#[must_use]
111#[inline]
112pub fn is_server_running(port: u16) -> bool {
113    std::net::TcpStream::connect(format!("localhost:{port}")).is_ok()
114}
115
116#[cfg(test)]
117mod test {
118    use super::format_duration;
119    use pretty_assertions::assert_eq;
120    use rstest::rstest;
121    use std::time::Duration;
122
123    #[rstest]
124    #[case::zero(Duration::from_secs(0), "00:00:00.00")]
125    #[case::sub_second(Duration::from_millis(100), "00:00:00.10")]
126    #[case::sub_second(Duration::from_millis(101), "00:00:00.10")]
127    #[case::one_second(Duration::from_secs(1), "00:00:01.00")]
128    #[case::one_minute(Duration::from_secs(60), "00:01:00.00")]
129    #[case::one_hour(Duration::from_secs(3600), "01:00:00.00")]
130    #[case::one_hour_one_minute_one_second(Duration::from_secs(3661), "01:01:01.00")]
131    #[case(Duration::from_secs(3600 + 120 + 1), "01:02:01.00")]
132    fn test_format_duration(#[case] duration: Duration, #[case] expected: &str) {
133        let actual = format_duration(&duration);
134        assert_eq!(actual, expected);
135    }
136
137    #[test]
138    fn test_function_name() {
139        fn test_function() {
140            let result = super::function_name!();
141            assert!(result.ends_with("test_function"));
142        }
143
144        test_function();
145    }
146
147    #[test]
148    fn test_get_data_dir() {
149        let data_dir = super::get_data_dir().unwrap();
150        assert_eq!(
151            data_dir
152                .components()
153                .last()
154                .unwrap()
155                .as_os_str()
156                .to_string_lossy(),
157            "mecomp"
158        );
159    }
160
161    #[test]
162    fn test_get_config_dir() {
163        let config_dir = super::get_config_dir().unwrap();
164        assert_eq!(
165            config_dir
166                .components()
167                .last()
168                .unwrap()
169                .as_os_str()
170                .to_string_lossy(),
171            "mecomp"
172        );
173    }
174}