open_with/
lib.rs

1use std::error::Error;
2use std::path::PathBuf;
3
4#[cfg(target_os = "linux")]
5mod lib_linux;
6#[cfg(target_os = "linux")]
7use lib_linux as backend;
8
9#[cfg(target_os = "macos")]
10mod lib_macos;
11#[cfg(target_os = "macos")]
12use lib_macos as backend;
13
14#[cfg(target_os = "windows")]
15mod lib_windows;
16#[cfg(target_os = "windows")]
17use lib_windows as backend;
18
19/// Opens the file at the given path using the default application.
20///
21/// # Arguments
22///
23/// * `file_path` - A `PathBuf` that holds the path to the file to be opened.
24///
25/// # Errors
26///
27/// Returns an error if the file cannot be opened.
28pub fn open(file_path: PathBuf) -> Result<(), Box<dyn Error>> {
29    backend::open(file_path)
30}
31
32/// Invokes the platform's "Open With" dialog for the file at the provided path.
33///
34/// # Arguments
35///
36/// * `file_path` - A `PathBuf` that holds the path to the file to be opened.
37///
38/// # Errors
39///
40/// Returns an error if the file cannot be opened.
41pub fn open_with(file_path: PathBuf) -> Result<(), Box<dyn Error>> {
42    backend::open_with(file_path)
43}
44
45/// Shows the folder containing the file at the given path. Where supported, the file is selected.
46///
47/// # Arguments
48///
49/// * `file_path` - A `PathBuf` that holds the path to the file.
50///
51/// # Errors
52///
53/// Returns an error if the folder cannot be shown.
54pub fn show_in_folder(file_path: PathBuf) -> Result<(), Box<dyn Error>> {
55    backend::show_in_folder(file_path)
56}
57
58/// Uses the default file manager to show properties of the file at the given path.
59///
60/// # Arguments
61///
62/// * `file_path` - A `PathBuf` that holds the path to the file.
63///
64/// # Errors
65///
66/// Returns an error if the properties cannot be shown.
67pub fn show_properties(file_path: PathBuf) -> Result<(), Box<dyn Error>> {
68    backend::show_properties(file_path)
69}
70
71#[cfg(test)]
72mod tests {
73    use super::*;
74    use std::path::PathBuf;
75
76    #[test]
77    fn test_open() {
78        let file_path = PathBuf::from("src/lib.rs");
79        let _result = open(file_path).unwrap();
80    }
81
82    #[test]
83    fn test_open_with() {
84        let file_path = PathBuf::from("src/lib.rs");
85        let _result = open_with(file_path).unwrap();
86    }
87
88    #[test]
89    fn test_open_folder() {
90        let file_path = PathBuf::from("src/lib.rs");
91        let _result = show_in_folder(file_path).unwrap();
92    }
93
94    #[test]
95    fn test_show_properties() {
96        let file_path = PathBuf::from("src/lib.rs");
97        let _result = show_properties(file_path).unwrap();
98        std::thread::sleep(std::time::Duration::from_millis(10000));
99    }
100}