file_uti/
lib.rs

1use std::process::Command;
2use tokio::process::Command as AsyncCommand;
3
4pub fn file_uti_sync(path: String) -> String {
5    let output = Command::new("mdls")
6        .arg("-raw")
7        .arg("-name")
8        .arg("kMDItemContentType")
9        .arg(path)
10        .output()
11        .expect("failed to execute process");
12    let mut result = String::new();
13    if output.status.success() {
14        result = String::from_utf8(output.stdout).unwrap();
15    } else {
16        let stderr = String::from_utf8(output.stderr).unwrap();
17        panic!("Command failed with error: {}", stderr);
18    }
19    result
20}
21
22pub async fn file_uti(path: String) -> String {
23    let output = AsyncCommand::new("mdls")
24        .arg("-raw")
25        .arg("-name")
26        .arg("kMDItemContentType")
27        .arg(path)
28        .output()
29        .await
30        .expect("Failed to execute command");
31    let mut result = String::new();
32    if output.status.success() {
33        result = String::from_utf8(output.stdout).unwrap();
34    } else {
35        // 处理错误情况
36        let stderr = String::from_utf8_lossy(&output.stderr);
37        panic!("Command failed with error: {}", stderr);
38    }
39    result
40}
41
42#[cfg(test)]
43mod tests {
44    use super::*;
45
46    #[test]
47    fn it_works_sync() {
48        let result = file_uti_sync(file!().to_string());
49        assert_eq!(result, "dyn.ah62d4rv4ge81e62");
50    }
51    #[tokio::test]
52    async fn it_works() {
53        let result = file_uti(file!().to_string()).await;
54        assert_eq!(result, "dyn.ah62d4rv4ge81e62");
55    }
56}