Debian Codegraph @

src/imp/mod.rs

raw symbols SCIP
1 use crate::common::{AppDataType, AppDirsError, AppInfo};
2 use crate::utils;
3 use std::fs;
4 use std::path::PathBuf;
5
6 #[cfg(any(target_os = "macos", target_os = "ios"))]
7 mod platform {
8 mod macos;
9 pub use self::macos::*;
10 }
11 #[cfg(all(unix, not(target_os = "macos"), not(target_os = "ios"), not(target_os = "android")))]
12 mod platform {
13 mod unix;
14 pub use self::unix::*;
15 }
16 #[cfg(windows)]
17 mod platform {
18 mod windows;
19 pub use self::windows::*;
20 }
21 #[cfg(not(any(windows, unix, target_os = "macos", target_os = "ios")))]
22 mod platform {
23 mod unknown;
24 pub use self::unknown::*;
25 }
26 #[cfg(target_os = "android")]
27 mod platform {
28 mod android;
29 pub use self::android::*;
30 }
31
32 /// Creates (if necessary) and returns path to **app-specific** data
33 /// **subdirectory** for provided data type and subdirectory path.
34 ///
35 /// The `path` parameter should be a valid relative path separated by
36 /// **forward slashes** (`/`).
37 ///
38 /// If the directory structure does not exist, this function will recursively
39 /// create the full hierarchy. Therefore, a result of `Ok` guarantees that the
40 /// returned path exists.
41 pub fn app_dir(t: AppDataType, app: &AppInfo, path: &str) -> Result<PathBuf, AppDirsError> {
42 let path = get_app_dir(t, app, path)?;
43 match fs::create_dir_all(&path) {
44 Ok(..) => Ok(path),
45 Err(e) => Err(e.into()),
46 }
47 }
48
49 /// Returns (but **does not create**) path to **app-specific** data
50 /// **subdirectory** for provided data type and subdirectory path.
51 ///
52 /// The `path` parameter should be a valid relative path separated by
53 /// **forward slashes** (`/`).
54 ///
55 /// A result of `Ok` means that we determined where the data SHOULD go, but
56 /// it DOES NOT guarantee that the directory actually exists. (See
57 /// [`app_dir`](fn.app_dir.html).)
58 pub fn get_app_dir(t: AppDataType, app: &AppInfo, path: &str) -> Result<PathBuf, AppDirsError> {
59 if app.author.is_empty() || app.name.is_empty() {
60 return Err(AppDirsError::InvalidAppInfo);
61 }
62 get_app_root(t, app).map(|mut root| {
63 for component in path.split('/').filter(|s| !s.is_empty()) {
64 root.push(utils::sanitized(component));
65 }
66 root
67 })
68 }
69
70 /// Creates (if necessary) and returns path to **app-specific** data
71 /// directory for provided data type.
72 ///
73 /// If the directory structure does not exist, this function will recursively
74 /// create the full hierarchy. Therefore, a result of `Ok` guarantees that the
75 /// returned path exists.
76 pub fn app_root(t: AppDataType, app: &AppInfo) -> Result<PathBuf, AppDirsError> {
77 let path = get_app_root(t, app)?;
78 match fs::create_dir_all(&path) {
79 Ok(..) => Ok(path),
80 Err(e) => Err(e.into()),
81 }
82 }
83
84 /// Returns (but **does not create**) path to **app-specific** data directory
85 /// for provided data type.
86 ///
87 /// A result of `Ok` means that we determined where the data SHOULD go, but
88 /// it DOES NOT guarantee that the directory actually exists. (See
89 /// [`app_root`](fn.app_root.html).)
90 pub fn get_app_root(t: AppDataType, app: &AppInfo) -> Result<PathBuf, AppDirsError> {
91 if app.author.is_empty() || app.name.is_empty() {
92 return Err(AppDirsError::InvalidAppInfo);
93 }
94 get_data_root(t).map(|mut root| {
95 if platform::USE_AUTHOR {
96 root.push(utils::sanitized(app.author));
97 }
98 root.push(utils::sanitized(app.name));
99 root
100 })
101 }
102
103 /// Creates (if necessary) and returns path to **top-level** data directory
104 /// for provided data type.
105 ///
106 /// If the directory structure does not exist, this function will recursively
107 /// create the full hierarchy. Therefore, a result of `Ok` guarantees that the
108 /// returned path exists.
109 pub fn data_root(t: AppDataType) -> Result<PathBuf, AppDirsError> {
110 let path = platform::get_app_dir(t)?;
111 match fs::create_dir_all(&path) {
112 Ok(..) => Ok(path),
113 Err(e) => Err(e.into()),
114 }
115 }
116
117 /// Returns (but **does not create**) path to **top-level** data directory for
118 /// provided data type.
119 ///
120 /// A result of `Ok` means that we determined where the data SHOULD go, but
121 /// it DOES NOT guarantee that the directory actually exists. (See
122 /// [`data_root`](fn.data_root.html).)
123 pub fn get_data_root(t: AppDataType) -> Result<PathBuf, AppDirsError> {
124 platform::get_app_dir(t)
125 }