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
//! A universal tool for writing files and creating directory structures.
//!
//! This module simplifies data storage by dynamically constructing paths from
//! a base directory (Home or Root) and a list of subdirectories.
/**
* write_file.rs
*
* (C) Copyright 2026 AtlantisOS Project
* by @NachtsternBuild
*
* License: GNU GENERAL PUBLIC LICENSE Version 3
*/
use UserDirs;
use fs;
use PathBuf;
/// Writes content to a file and creates the entire directory structure if necessary.
///
/// This function is highly flexible: It can operate either in the user's home directory
/// or at any specified location (e.g., in the root filesystem).
///
/// # How it works
/// 1. **Determine base path:** If `base_path` is `None`, the home directory is used.
/// 2. **Path construction:** The segments from `path_list` are appended to the base path.
/// 3. **Directory creation:** Any missing subdirectories are automatically created.
/// 4. **Write file:** The content is finally written to the file (overwriting existing data).
///
/// # Arguments
/// * `filename` - The name of the file to be created.
/// * `content` - The text content to be saved.
/// * `base_path` - Optional starting point. `None` corresponds to the home directory (`~/`).
/// * `path_list` - An array of subdirectories (e.g., `&["Documents", "Projects"]`).
///
/// # Error Handling
/// Returns a `std::io::Result`. Errors occur if the home directory
/// cannot be determined or if write permissions are missing in the destination directory.
///
/// # Usage:
///
/// ```rust
/// use std::path::PathBuf;
///
/// // Create ~/Atlantis/logs/session.log
/// let _ = write_file(
/// "session.log",
/// "Log-Content...",
/// None,
/// &["Atlantis", "logs"]
/// );
///
/// // Create /etc/atlantis/config.toml
/// let _ = write_file(
/// "config.toml",
/// "# Config",
/// Some(PathBuf::from("/")),
/// &["etc", "atlantis"]
/// );
/// ```