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/// use crate::Maintain::Source::Build::Error;
65/// fn read_file(path: &Path) -> Result<String, Error> {
66/// fs::read_to_string(path)?
67/// }
68/// ```
69//
70// Example 2: Handling errors
71/// ```rust
72/// use crate::Maintain::Source::Build::Error;
73/// match result {
74/// Ok(_) => println!("Success"),
75/// Err(Error::Io(e)) => println!("IO error: {}", e),
76/// Err(Error::Missing(path)) => println!("Missing: {}", path.display()),
77/// Err(e) => println!("Error: {}", e),
78/// }
79/// ```
80//
81//=============================================================================//
82// IMPLEMENTATION
83//=============================================================================//
84
85use std::{io, path::PathBuf};
86use thiserror::Error;
87
88/// Represents all possible errors that can occur during the build script's
89/// execution.
90///
91/// This enum provides a comprehensive error type for the build orchestration
92/// system, covering all failure modes from IO operations to parsing errors
93/// to command execution failures. Each variant includes relevant context
94/// to help diagnose the issue.
95///
96/// The enum derives `Error` from the `thiserror` crate, which provides
97/// automatic implementations of the `Error` trait and error display formatting.
98///
99/// # Automatic Conversions
100///
101/// This type implements `From` for several error types, allowing the `?`
102/// operator to automatically convert library-specific errors into `Error`:
103/// - `io::Error` → `Error::Io`
104/// - `toml_edit::TomlError` → `Error::Edit`
105/// - `toml::de::Error` → `Error::Parse`
106/// - `serde_json::Error` → `Error::Json`
107/// - `json5::Error` → `Error::Jsonfive`
108/// - `std::string::FromUtf8Error` → `Error::Utf`
109#[derive(Error, Debug)]
110pub enum Error {
111    /// IO operation error.
112    ///
113    /// This variant wraps standard Rust IO errors that can occur during
114    /// file operations like reading, writing, copying, or deleting files.
115    #[error("IO: {0}")]
116    Io(#[from] io::Error),
117
118    /// Toml editing error.
119    ///
120    /// This variant wraps errors that occur when manipulating TOML documents
121    /// using the `toml_edit` library, such as invalid mutations or parsing
122    /// issues when reading TOML content.
123    #[error("Toml Editing: {0}")]
124    Edit(#[from] toml_edit::TomlError),
125
126    /// Toml deserialization error.
127    ///
128    /// This variant wraps errors that occur when parsing TOML files into
129    /// Rust structs using the `toml` library's deserialization functionality.
130    #[error("Toml Parsing: {0}")]
131    Parse(#[from] toml::de::Error),
132
133    /// JSON parsing/error.
134    ///
135    /// This variant wraps errors that occur when parsing or validating
136    /// standard JSON files using the `serde_json` library.
137    #[error("Json: {0}")]
138    Json(#[from] serde_json::Error),
139
140    /// JSON5 parsing/error.
141    ///
142    /// This variant wraps errors that occur when parsing or validating
143    /// JSON5 files (a more flexible JSON format) using the `json5` library.
144    #[error("Json5: {0}")]
145    Jsonfive(#[from] json5::Error),
146
147    /// Missing directory error.
148    ///
149    /// This variant is used when a required directory does not exist at
150    /// the specified path. The path is included for debugging purposes,
151    #[error("Missing Directory: {0}")]
152    Missing(PathBuf),
153
154    /// Shell command execution failure.
155    ///
156    /// This variant is used when the final build command fails to execute
157    /// successfully. The exit status is included to provide information
158    /// about the failure.
159    #[error("Command Failed: {0}")]
160    Shell(std::process::ExitStatus),
161
162    /// No command provided error.
163    ///
164    /// This variant is used when the build script is invoked without
165    /// specifying a build command to execute.
166    #[error("No Command Provided")]
167    NoCommand,
168
169    /// Tauri configuration file not found error.
170    ///
171    /// This variant is used when neither `tauri.conf.json` nor
172    /// `tauri.conf.json5` can be found in the project directory.
173    #[error("Tauri Configuration File Not Found")]
174    Config,
175
176    /// Backup file already exists error.
177    ///
178    /// This variant is used when the Guard attempts to create a backup but
179    /// a backup file already exists at the target location. This prevents
180    /// overwriting existing backups and ensures data safety.
181    #[error("Backup File Exists: {0}")]
182    Exists(PathBuf),
183
184    /// UTF-8 conversion error.
185    ///
186    /// This variant wraps errors that occur when converting byte vectors
187    /// to UTF-8 strings, particularly when dealing with file contents or
188    /// command output.
189    #[error("UTF-8 Conversion: {0}")]
190    Utf(#[from] std::string::FromUtf8Error),
191
192    /// Missing environment variable error.
193    ///
194    /// This variant is used when a required environment variable is not
195    /// set. The variable name is included in the error message for easy
196    /// identification and resolution.
197    #[error("Environment Variable Missing: {0}")]
198    Environment(String),
199}