use std::path::{Path, PathBuf};
pub fn get_library_names() -> Vec<&'static str> {
if cfg!(windows) {
vec!["MediaInfo.dll"]
} else if cfg!(target_os = "macos") {
vec!["libmediainfo.0.dylib", "libmediainfo.dylib"]
} else {
vec!["libmediainfo.so.0"]
}
}
#[allow(dead_code)]
pub fn is_windows() -> bool {
cfg!(windows)
}
pub fn get_library_paths(bundled_search_dir: Option<&Path>) -> Vec<PathBuf> {
let library_names = get_library_names();
let mut paths = Vec::new();
if let Some(dir) = bundled_search_dir {
for name in &library_names {
let bundled_path = dir.join(name);
if bundled_path.is_file() {
return vec![bundled_path];
}
}
}
for name in library_names {
paths.push(PathBuf::from(name));
}
paths
}
#[allow(dead_code)]
pub fn wchar_size() -> usize {
if cfg!(windows) {
2 } else {
4 }
}
#[allow(dead_code)]
pub fn uses_utf16() -> bool {
cfg!(windows)
}
#[allow(dead_code)]
pub fn platform_name() -> &'static str {
if cfg!(windows) {
"Windows"
} else if cfg!(target_os = "macos") {
"macOS"
} else if cfg!(target_os = "linux") {
"Linux"
} else if cfg!(unix) {
"Unix"
} else {
"Unknown"
}
}
#[cfg(test)]
mod tests {
use super::*;
use tempfile::tempdir;
#[test]
fn test_library_names_not_empty() {
let names = get_library_names();
assert!(!names.is_empty());
}
#[test]
fn test_library_names_have_correct_extension() {
let names = get_library_names();
for name in names {
if cfg!(windows) {
assert!(name.ends_with(".dll"));
} else if cfg!(target_os = "macos") {
assert!(name.ends_with(".dylib"));
} else {
assert!(name.contains(".so"));
}
}
}
#[test]
fn test_wchar_size() {
let size = wchar_size();
if cfg!(windows) {
assert_eq!(size, 2);
} else {
assert_eq!(size, 4);
}
}
#[test]
fn test_uses_utf16() {
let uses = uses_utf16();
if cfg!(windows) {
assert!(uses);
} else {
assert!(!uses);
}
}
#[test]
fn test_platform_name() {
let name = platform_name();
assert!(!name.is_empty());
assert!(["Windows", "macOS", "Linux", "Unix", "Unknown"].contains(&name));
}
#[test]
fn test_get_library_paths_without_bundled() {
let paths = get_library_paths(None);
assert!(!paths.is_empty());
}
#[test]
fn test_get_library_paths_prefers_bundled_dir() {
let dir = tempdir().expect("failed to create temp directory");
let name = get_library_names()[0];
let bundled_path = dir.path().join(name);
std::fs::write(&bundled_path, b"").expect("failed to create dummy library file");
let paths = get_library_paths(Some(dir.path()));
assert_eq!(paths, vec![bundled_path]);
}
#[test]
fn test_is_windows_consistent() {
assert_eq!(is_windows(), cfg!(windows));
}
}