Maintain/Build/Error.rs
1//=============================================================================//
2// File Path: Element/Maintain/Source/Build/Error.rs
3//=============================================================================//
4// Module: Error
5//
6// Brief Description: Build system error types and handling.
7//
8// RESPONSIBILITIES:
9// ================
10//
11// Primary:
12// - Define comprehensive error types for build operations
13// - Provide automatic error conversions from underlying libraries
14// - Support clear error messages for debugging build failures
15//
16// Secondary:
17// - Enable proper error propagation through the build system
18// - Provide context for build failure diagnosis
19//
20// ARCHITECTURAL ROLE:
21// ===================
22//
23// Position:
24// - Infrastructure/Error handling layer
25// - Error type definitions
26//
27// Dependencies (What this module requires):
28// - External crates: thiserror, std
29// - Internal modules: None
30// - Traits implemented: None
31//
32// Dependents (What depends on this module):
33// - Build orchestration functions
34// - File manipulation functions
35// - Guard implementation
36//
37// IMPLEMENTATION DETAILS:
38// =======================
39//
40// Design Patterns:
41// - Error handling pattern with thiserror derive macro
42// - From trait implementations for automatic error conversion
43//
44// Performance Considerations:
45// - Complexity: O(1) - enum variant selection
46// - Memory usage patterns: Enum discrimination + variant data
47// - Hot path optimizations: None
48//
49// Thread Safety:
50// - Thread-safe: Yes (immutable error enum)
51// - Synchronization mechanisms used: None
52// - Interior mutability considerations: None
53//
54// Error Handling:
55// - Error types returned: This module defines all build errors
56// - Recovery strategies: Propagate errors up; Guard restores files
57//
58// EXAMPLES:
59// =========
60//
61// Example 1: Returning an error
62/// ```rust
63/// use std::fs;
64///
65/// use crate::Maintain::Source::Build::Error;
66/// fn read_file(path:&Path) -> Result<String, Error> { fs::read_to_string(path)? }
67/// ```
68// Example 2: Handling errors
69/// ```rust
70/// use crate::Maintain::Source::Build::Error;
71/// match result {
72/// Ok(_) => println!("Success"),
73/// Err(Error::Io(e)) => println!("IO error: {}", e),
74/// Err(Error::Missing(path)) => println!("Missing: {}", path.display()),
75/// Err(e) => println!("Error: {}", e),
76/// }
77/// ```
78//
79//=============================================================================//
80// IMPLEMENTATION
81//=============================================================================//
82use std::{io, path::PathBuf};
83
84use thiserror::Error;
85
86/// Represents all possible errors that can occur during the build script's
87/// execution.
88///
89/// This enum provides a comprehensive error type for the build orchestration
90/// system, covering all failure modes from IO operations to parsing errors
91/// to command execution failures. Each variant includes relevant context
92/// to help diagnose the issue.
93///
94/// The enum derives `Error` from the `thiserror` crate, which provides
95/// automatic implementations of the `Error` trait and error display formatting.
96///
97/// # Automatic Conversions
98///
99/// This type implements `From` for several error types, allowing the `?`
100/// operator to automatically convert library-specific errors into `Error`:
101/// - `io::Error` → `Error::Io`
102/// - `toml_edit::TomlError` → `Error::Edit`
103/// - `toml::de::Error` → `Error::Parse`
104/// - `serde_json::Error` → `Error::Json`
105/// - `json5::Error` → `Error::Jsonfive`
106/// - `std::string::FromUtf8Error` → `Error::Utf`
107#[derive(Error, Debug)]
108pub enum Error {
109 /// IO operation error.
110 ///
111 /// This variant wraps standard Rust IO errors that can occur during
112 /// file operations like reading, writing, copying, or deleting files.
113 #[error("IO: {0}")]
114 Io(#[from] io::Error),
115
116 /// Toml editing error.
117 ///
118 /// This variant wraps errors that occur when manipulating TOML documents
119 /// using the `toml_edit` library, such as invalid mutations or parsing
120 /// issues when reading TOML content.
121 #[error("Toml Editing: {0}")]
122 Edit(#[from] toml_edit::TomlError),
123
124 /// Toml deserialization error.
125 ///
126 /// This variant wraps errors that occur when parsing TOML files into
127 /// Rust structs using the `toml` library's deserialization functionality.
128 #[error("Toml Parsing: {0}")]
129 Parse(#[from] toml::de::Error),
130
131 /// JSON parsing/error.
132 ///
133 /// This variant wraps errors that occur when parsing or validating
134 /// standard JSON files using the `serde_json` library.
135 #[error("Json: {0}")]
136 Json(#[from] serde_json::Error),
137
138 /// JSON5 parsing/error.
139 ///
140 /// This variant wraps errors that occur when parsing or validating
141 /// JSON5 files (a more flexible JSON format) using the `json5` library.
142 #[error("Json5: {0}")]
143 Jsonfive(#[from] json5::Error),
144
145 /// Missing directory error.
146 ///
147 /// This variant is used when a required directory does not exist at
148 /// the specified path. The path is included for debugging purposes,
149 #[error("Missing Directory: {0}")]
150 Missing(PathBuf),
151
152 /// Shell command execution failure.
153 ///
154 /// This variant is used when the final build command fails to execute
155 /// successfully. The exit status is included to provide information
156 /// about the failure.
157 #[error("Command Failed: {0}")]
158 Shell(std::process::ExitStatus),
159
160 /// No command provided error.
161 ///
162 /// This variant is used when the build script is invoked without
163 /// specifying a build command to execute.
164 #[error("No Command Provided")]
165 NoCommand,
166
167 /// Tauri configuration file not found error.
168 ///
169 /// This variant is used when neither `tauri.conf.json` nor
170 /// `tauri.conf.json5` can be found in the project directory.
171 #[error("Tauri Configuration File Not Found")]
172 Config,
173
174 /// Backup file already exists error.
175 ///
176 /// This variant is used when the Guard attempts to create a backup but
177 /// a backup file already exists at the target location. This prevents
178 /// overwriting existing backups and ensures data safety.
179 #[error("Backup File Exists: {0}")]
180 Exists(PathBuf),
181
182 /// UTF-8 conversion error.
183 ///
184 /// This variant wraps errors that occur when converting byte vectors
185 /// to UTF-8 strings, particularly when dealing with file contents or
186 /// command output.
187 #[error("UTF-8 Conversion: {0}")]
188 Utf(#[from] std::string::FromUtf8Error),
189
190 /// Missing environment variable error.
191 ///
192 /// This variant is used when a required environment variable is not
193 /// set. The variable name is included in the error message for easy
194 /// identification and resolution.
195 #[error("Environment Variable Missing: {0}")]
196 Environment(String),
197}