app_path/app_path/deprecated.rs
1use crate::AppPath;
2
3impl AppPath {
4 /// Creates parent directories needed for this file path.
5 ///
6 /// This method creates all parent directories for a file path, making it ready
7 /// for file creation. It does not create the file itself.
8 ///
9 /// **Use this when you know the path represents a file and you want to prepare
10 /// the directory structure for writing the file.**
11 ///
12 /// # Examples
13 ///
14 /// ```rust
15 /// use app_path::AppPath;
16 /// use std::env;
17 ///
18 /// let temp_dir = env::temp_dir().join("app_path_example");
19 ///
20 /// // Prepare directories for a config file
21 /// let config_file = AppPath::new(temp_dir.join("config/app.toml"));
22 /// config_file.ensure_parent_dirs()?; // Creates config/ directory
23 ///
24 /// // Now you can write the file
25 /// std::fs::write(&config_file, "key = value")?;
26 /// assert!(config_file.exists());
27 ///
28 /// // Prepare directories for a log file
29 /// let log_file = AppPath::new(temp_dir.join("logs/2024/app.log"));
30 /// log_file.ensure_parent_dirs()?; // Creates logs/2024/ directories
31 ///
32 /// # std::fs::remove_dir_all(&temp_dir).ok();
33 /// # Ok::<(), Box<dyn std::error::Error>>(())
34 /// ```
35 #[deprecated(
36 since = "0.2.4",
37 note = "Use `create_parents()` instead for clearer intent"
38 )]
39 #[inline]
40 pub fn ensure_parent_dirs(&self) -> std::io::Result<()> {
41 if let Some(parent) = self.parent() {
42 std::fs::create_dir_all(&parent)
43 } else {
44 Ok(())
45 }
46 }
47
48 /// Creates this path as a directory, including all parent directories.
49 ///
50 /// This method treats the path as a directory and creates it along with
51 /// all necessary parent directories. The created directory will exist
52 /// after this call succeeds.
53 ///
54 /// **Use this when you know the path represents a directory that should be created.**
55 ///
56 /// # Behavior
57 ///
58 /// - **Creates the directory itself**: Unlike `ensure_parent_dirs()`, this creates the full path as a directory
59 /// - **Creates all parents**: Any missing parent directories are created automatically
60 /// - **Idempotent**: Safe to call multiple times - won't fail if directory already exists
61 /// - **Atomic-like**: Either all directories are created or the operation fails
62 ///
63 /// # Examples
64 ///
65 /// ## Basic Directory Creation
66 ///
67 /// ```rust
68 /// use app_path::AppPath;
69 /// use std::env;
70 ///
71 /// let temp_dir = env::temp_dir().join("app_path_dir_example");
72 ///
73 /// // Create a cache directory
74 /// let cache_dir = AppPath::new(temp_dir.join("cache"));
75 /// cache_dir.ensure_dir_exists()?; // Creates cache/ directory
76 /// assert!(cache_dir.exists());
77 /// assert!(cache_dir.is_dir());
78 ///
79 /// # std::fs::remove_dir_all(&temp_dir).ok();
80 /// # Ok::<(), Box<dyn std::error::Error>>(())
81 /// ```
82 ///
83 /// ## Nested Directory Structures
84 ///
85 /// ```rust
86 /// use app_path::AppPath;
87 /// use std::env;
88 ///
89 /// let temp_dir = env::temp_dir().join("app_path_nested_example");
90 ///
91 /// // Create deeply nested directories
92 /// let deep_dir = AppPath::new(temp_dir.join("data/backups/daily"));
93 /// deep_dir.ensure_dir_exists()?; // Creates data/backups/daily/ directories
94 /// assert!(deep_dir.exists());
95 /// assert!(deep_dir.is_dir());
96 ///
97 /// // All parent directories are also created
98 /// let backups_dir = AppPath::new(temp_dir.join("data/backups"));
99 /// assert!(backups_dir.exists());
100 /// assert!(backups_dir.is_dir());
101 ///
102 /// # std::fs::remove_dir_all(&temp_dir).ok();
103 /// # Ok::<(), Box<dyn std::error::Error>>(())
104 /// ```
105 ///
106 /// ## Practical Application Setup
107 ///
108 /// ```rust
109 /// use app_path::AppPath;
110 /// use std::env;
111 ///
112 /// let temp_dir = env::temp_dir().join("app_setup_example");
113 ///
114 /// // Set up application directory structure
115 /// let config_dir = AppPath::new(temp_dir.join("config"));
116 /// let data_dir = AppPath::new(temp_dir.join("data"));
117 /// let cache_dir = AppPath::new(temp_dir.join("cache"));
118 /// let logs_dir = AppPath::new(temp_dir.join("logs"));
119 ///
120 /// // Create all directories
121 /// config_dir.ensure_dir_exists()?;
122 /// data_dir.ensure_dir_exists()?;
123 /// cache_dir.ensure_dir_exists()?;
124 /// logs_dir.ensure_dir_exists()?;
125 ///
126 /// // Now create subdirectories
127 /// let daily_logs = logs_dir.join("daily");
128 /// daily_logs.ensure_dir_exists()?;
129 ///
130 /// // Verify structure
131 /// assert!(config_dir.is_dir());
132 /// assert!(data_dir.is_dir());
133 /// assert!(cache_dir.is_dir());
134 /// assert!(logs_dir.is_dir());
135 /// assert!(daily_logs.is_dir());
136 ///
137 /// # std::fs::remove_dir_all(&temp_dir).ok();
138 /// # Ok::<(), Box<dyn std::error::Error>>(())
139 /// ```
140 ///
141 /// ## Comparison with `create_parents()`
142 ///
143 /// ```rust
144 /// use app_path::AppPath;
145 /// use std::env;
146 ///
147 /// let temp_dir = env::temp_dir().join("app_comparison_example");
148 ///
149 /// let file_path = AppPath::new(temp_dir.join("logs/app.log"));
150 /// let dir_path = AppPath::new(temp_dir.join("logs"));
151 ///
152 /// // For files: prepare parent directories
153 /// file_path.create_parents()?; // Creates logs/ directory
154 /// assert!(dir_path.exists()); // logs/ directory exists
155 /// assert!(!file_path.exists()); // app.log file does NOT exist
156 ///
157 /// // For directories: create the directory itself
158 /// dir_path.create_dir()?; // Creates logs/ directory (idempotent)
159 /// assert!(dir_path.exists()); // logs/ directory exists
160 /// assert!(dir_path.is_dir()); // and it's definitely a directory
161 ///
162 /// # std::fs::remove_dir_all(&temp_dir).ok();
163 /// # Ok::<(), Box<dyn std::error::Error>>(())
164 /// ```
165 #[deprecated(
166 since = "0.2.4",
167 note = "Use `create_dir()` instead for clearer intent"
168 )]
169 #[inline]
170 pub fn ensure_dir_exists(&self) -> std::io::Result<()> {
171 std::fs::create_dir_all(self)
172 }
173
174 /// Creates all directories needed for this path.
175 ///
176 /// **DEPRECATED**: Use [`create_parents()`](Self::create_parents) for file paths
177 /// or [`create_dir()`](Self::create_dir) for directory paths instead.
178 /// This method name was confusing as it didn't always create directories for the path itself.
179 ///
180 /// This method intelligently determines whether the path represents a file
181 /// or directory and creates the appropriate directories:
182 /// - **For existing directories**: does nothing (already exists)
183 /// - **For existing files**: creates parent directories if needed
184 /// - **For non-existing paths**: treats as file path and creates parent directories
185 ///
186 /// # Migration Guide
187 ///
188 /// ```rust
189 /// use app_path::AppPath;
190 ///
191 /// let file_path = AppPath::new("logs/app.log");
192 /// let dir_path = AppPath::new("cache");
193 ///
194 /// // Old (deprecated):
195 /// // file_path.create_dir_all()?;
196 /// // dir_path.create_dir_all()?; // This was confusing!
197 ///
198 /// // New (clear):
199 /// file_path.create_parents()?; // Creates logs/ for the file
200 /// dir_path.create_dir()?; // Creates cache/ directory
201 /// # Ok::<(), Box<dyn std::error::Error>>(())
202 /// ```
203 #[deprecated(
204 since = "0.2.2",
205 note = "Use `create_parents()` for file paths or `create_dir()` for directory paths instead"
206 )]
207 #[inline]
208 pub fn create_dir_all(&self) -> std::io::Result<()> {
209 if let Some(parent) = self.full_path.parent() {
210 std::fs::create_dir_all(parent)?;
211 }
212 Ok(())
213 }
214}