1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
use current_exe;
use PathBuf;
/// Error type for AppPath operations.
///
/// This enum represents the possible failures that can occur when working with
/// AppPath instances. These include both system-level failures and I/O errors.
///
/// # When These Errors Occur
///
/// - **`ExecutableNotFound`**: When [`std::env::current_exe()`] fails
/// - Very rare, but can happen in some embedded or heavily sandboxed environments
/// - May occur if the executable has been deleted while running
/// - Can happen in some containerized environments with unusual configurations
///
/// - **`InvalidExecutablePath`**: When the executable path is empty
/// - Extremely rare, indicates a corrupted or broken system
/// - May occur with custom or non-standard program loaders
///
/// - **`IoError`**: When I/O operations fail
/// - Directory creation fails due to insufficient permissions
/// - Disk space issues or filesystem errors
/// - Invalid path characters for the target filesystem
/// - Network filesystem problems
///
/// System-level errors are typically unrecoverable for portable applications,
/// while I/O errors may be recoverable depending on the specific cause.
///
/// # Examples
///
/// ```rust
/// use app_path::{AppPath, AppPathError};
///
/// // Handle errors explicitly
/// match AppPath::try_with("config.toml") {
/// Ok(config) => {
/// println!("Config path: {}", config.display());
/// }
/// Err(AppPathError::ExecutableNotFound(msg)) => {
/// eprintln!("Cannot find executable: {msg}");
/// // Fallback to alternative configuration
/// }
/// Err(AppPathError::InvalidExecutablePath(msg)) => {
/// eprintln!("Invalid executable path: {msg}");
/// // Fallback to alternative configuration
/// }
/// Err(AppPathError::IoError(io_err)) => {
/// eprintln!("I/O operation failed: {io_err}");
/// // Handle specific I/O error types
/// match io_err.kind() {
/// std::io::ErrorKind::PermissionDenied => {
/// eprintln!("Permission denied - check file permissions");
/// }
/// std::io::ErrorKind::NotFound => {
/// eprintln!("File or directory not found");
/// }
/// _ => eprintln!("Other I/O error: {io_err}"),
/// }
/// }
/// }
/// ```
/// Creates an IoError with path context for better debugging.
///
/// This implementation adds the file path to I/O error messages, making it easier
/// to identify which path caused the failure in complex directory operations.
///
/// # Examples
///
/// ```rust
/// use app_path::AppPathError;
/// use std::path::PathBuf;
///
/// let io_error = std::io::Error::new(std::io::ErrorKind::PermissionDenied, "access denied");
/// let path = PathBuf::from("/some/restricted/path");
/// let app_error = AppPathError::from((io_error, &path));
///
/// // Error message includes both the original error and the path
/// assert!(app_error.to_string().contains("access denied"));
/// assert!(app_error.to_string().contains("/some/restricted/path"));
/// ```
/// Try to determine the executable directory (fallible version).
///
/// This is the internal fallible initialization function that both the fallible
/// and infallible APIs use. It handles all the edge cases properly without
/// exposing them as errors to API users.
pub