Debian Codegraph @

src/common.rs

raw symbols SCIP
1 /// Struct that holds information about your app.
2 ///
3 /// It's recommended to create a single `const` instance of `AppInfo`:
4 ///
5 /// ```
6 /// use app_dirs2::AppInfo;
7 /// const APP_INFO: AppInfo = AppInfo{name: "Awesome App", author: "Dedicated Dev"};
8 /// ```
9 ///
10 /// # Caveats
11 /// Functions in this library sanitize any characters that could be
12 /// non-filename-safe from `name` and `author`. The resulting paths will be
13 /// more human-readable if you stick to **letters, numbers, spaces, hyphens,
14 /// underscores, and periods** for both properties.
15 ///
16 /// The `author` property is currently only used by Windows, as macOS and *nix
17 /// specifications don't require it. Make sure your `name` string is unique!
18 #[derive(Clone, Debug, PartialEq, Eq, Hash)]
19 pub struct AppInfo {
20 /// Name of your app (e.g. "Hearthstone").
21 pub name: &'static str,
22 /// Author of your app (e.g. "Blizzard").
23 pub author: &'static str,
24 }
25
26 /// Enum specifying the type of app data you want to store.
27 ///
28 /// **Different platforms are NOT guaranteed to distinguish between each data
29 /// type.** Keep this in mind when choosing data file paths.
30 ///
31 /// Example: Windows does not supported shared application data and does not
32 /// distinguish between config and data. Therefore, on Windows, all variants
33 /// except `UserCache` return the same path.
34 #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
35 pub enum AppDataType {
36 /// User-specific app configuration data.
37 UserConfig,
38 /// User-specific arbitrary app data.
39 UserData,
40 /// User-specific app cache data.
41 UserCache,
42 /// System-wide arbitrary app data.
43 SharedData,
44 /// System-wide app configuration data.
45 SharedConfig,
46 }
47
48 impl AppDataType {
49 /// Returns `true` for non-user-specific data types.
50 pub fn is_shared(&self) -> bool {
51 use crate::AppDataType::*;
52 matches!(self, SharedData | SharedConfig)
53 }
54 }
55
56 const ERR_NOT_SUPPORTED: &str = "App data directories not supported";
57 const ERR_INVALID_APP_INFO: &str = "Invalid app name or author";
58
59 /// Error type for any `app_dirs` operation.
60 #[derive(Debug)]
61 pub enum AppDirsError {
62 /// An I/O error occurred during the operation.
63 Io(std::io::Error),
64 /// App-specific directories are not properly supported by the system
65 /// (e.g. required environment variables don't exist).
66 NotSupported,
67 /// App info given to this library was invalid (e.g. app name or author
68 /// were empty).
69 InvalidAppInfo,
70 }
71
72 impl std::fmt::Display for AppDirsError {
73 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
74 use crate::AppDirsError::*;
75 match *self {
76 Io(ref e) => std::fmt::Display::fmt(e, f),
77 NotSupported => f.write_str(ERR_NOT_SUPPORTED),
78 InvalidAppInfo => f.write_str(ERR_INVALID_APP_INFO),
79 }
80 }
81 }
82
83 impl std::error::Error for AppDirsError {
84 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
85 use crate::AppDirsError::*;
86 match *self {
87 Io(ref e) => Some(e),
88 NotSupported => None,
89 InvalidAppInfo => None,
90 }
91 }
92 }
93
94 impl From<std::io::Error> for AppDirsError {
95 fn from(e: std::io::Error) -> Self {
96 AppDirsError::Io(e)
97 }
98 }