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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
//! # Environment Setup Utilities for Frozen DuckDB Binary
//!
//! This module provides utilities for validating the frozen DuckDB environment,
//! including checking environment variables and verifying binary existence.
//! It ensures that the pre-built DuckDB binaries are properly configured
//! and accessible before attempting to use them.
//!
//! ## Key Features
//!
//! - **Environment validation**: Checks required environment variables
//! - **Binary verification**: Ensures DuckDB binaries exist and are accessible
//! - **Configuration helpers**: Easy access to configured paths
//! - **Error reporting**: Clear error messages for troubleshooting
//!
//! ## Usage Examples
//!
//! ### Basic Environment Check
//!
//! ```rust
//! use frozen_duckdb::env_setup;
//!
//! // Check if environment is properly configured
//! if env_setup::is_configured() {
//! println!("✅ Frozen DuckDB environment is ready!");
//! } else {
//! println!("❌ Please run: source prebuilt/setup_env.sh");
//! }
//! ```
//!
//! ### Get Configuration Paths
//!
//! ```rust
//! use frozen_duckdb::env_setup;
//!
//! // Get library directory
//! if let Some(lib_dir) = env_setup::get_lib_dir() {
//! println!("Library directory: {}", lib_dir);
//! }
//!
//! // Get include directory
//! if let Some(include_dir) = env_setup::get_include_dir() {
//! println!("Include directory: {}", include_dir);
//! }
//! ```
//!
//! ### Validate Binary Existence
//!
//! ```rust
//! use frozen_duckdb::env_setup;
//!
//! // Validate that binaries exist
//! match env_setup::validate_binary() {
//! Ok(()) => println!("✅ DuckDB binaries are available"),
//! Err(e) => println!("❌ Binary validation failed: {}", e),
//! }
//! ```
//!
//! ## Environment Variables
//!
//! The module expects the following environment variables to be set:
//!
//! - `DUCKDB_LIB_DIR`: Path to directory containing DuckDB library files
//! - `DUCKDB_INCLUDE_DIR`: Path to directory containing DuckDB header files
//!
//! These are typically set by running `source prebuilt/setup_env.sh`.
//!
//! ## Binary Validation
//!
//! The validation process checks for:
//!
//! - **x86_64 binary**: `libduckdb_x86_64.dylib` (55MB)
//! - **arm64 binary**: `libduckdb_arm64.dylib` (50MB)
//! - **Generic fallback**: `libduckdb.dylib` (if architecture-specific not found)
//!
//! At least one binary must be present for validation to succeed.
use Result;
use env;
use Path;
/// Checks if the frozen DuckDB environment is properly configured.
///
/// This function verifies that both required environment variables are set:
/// `DUCKDB_LIB_DIR` and `DUCKDB_INCLUDE_DIR`. These variables are typically
/// set by running `source prebuilt/setup_env.sh`.
///
/// # Returns
///
/// `true` if both required environment variables are set, `false` otherwise.
///
/// # Examples
///
/// ```rust
/// use frozen_duckdb::env_setup;
///
/// // Check configuration status
/// if env_setup::is_configured() {
/// println!("✅ Environment is properly configured");
/// } else {
/// println!("❌ Please run: source prebuilt/setup_env.sh");
/// }
/// ```
///
/// # Environment Variables Checked
///
/// - `DUCKDB_LIB_DIR`: Must be set to the directory containing DuckDB libraries
/// - `DUCKDB_INCLUDE_DIR`: Must be set to the directory containing DuckDB headers
///
/// # Performance
///
/// This function is optimized for frequent calls with minimal overhead.
/// Environment variable access is cached by the OS, so repeated calls
/// are very fast (<1μs).
/// Gets the configured DuckDB library directory path.
///
/// This function retrieves the value of the `DUCKDB_LIB_DIR` environment
/// variable, which should point to the directory containing the DuckDB
/// library files (`.dylib` files).
///
/// # Returns
///
/// `Some(String)` containing the library directory path if the environment
/// variable is set, `None` if it's not set or empty.
///
/// # Examples
///
/// ```rust
/// use frozen_duckdb::env_setup;
///
/// // Get library directory
/// if let Some(lib_dir) = env_setup::get_lib_dir() {
/// println!("Library directory: {}", lib_dir);
/// } else {
/// println!("DUCKDB_LIB_DIR not set");
/// }
/// ```
///
/// # Expected Directory Contents
///
/// The library directory should contain:
///
/// - `libduckdb_x86_64.dylib` (55MB) - Intel/AMD 64-bit binary
/// - `libduckdb_arm64.dylib` (50MB) - Apple Silicon/ARM 64-bit binary
/// - `libduckdb.dylib` - Generic fallback binary
///
/// # Error Handling
///
/// This function never fails - it returns `None` if the environment
/// variable is not set, rather than panicking or returning an error.
/// Gets the configured DuckDB include directory path.
///
/// This function retrieves the value of the `DUCKDB_INCLUDE_DIR` environment
/// variable, which should point to the directory containing the DuckDB
/// header files (`.h` and `.hpp` files).
///
/// # Returns
///
/// `Some(String)` containing the include directory path if the environment
/// variable is set, `None` if it's not set or empty.
///
/// # Examples
///
/// ```rust
/// use frozen_duckdb::env_setup;
///
/// // Get include directory
/// if let Some(include_dir) = env_setup::get_include_dir() {
/// println!("Include directory: {}", include_dir);
/// } else {
/// println!("DUCKDB_INCLUDE_DIR not set");
/// }
/// ```
///
/// # Expected Directory Contents
///
/// The include directory should contain:
///
/// - `duckdb.h` (186KB) - C header file
/// - `duckdb.hpp` (1.8MB) - C++ header file
///
/// # Error Handling
///
/// This function never fails - it returns `None` if the environment
/// variable is not set, rather than panicking or returning an error.
/// Validates that the frozen DuckDB binary exists and is accessible.
///
/// This function performs a comprehensive check to ensure that at least one
/// DuckDB binary is available in the configured library directory. It checks
/// for architecture-specific binaries first, then falls back to generic ones.
///
/// # Returns
///
/// `Ok(())` if at least one DuckDB binary is found and accessible,
/// `Err` with a descriptive error message if no binaries are found.
///
/// # Examples
///
/// ```rust
/// use frozen_duckdb::env_setup;
///
/// // Validate binary existence
/// match env_setup::validate_binary() {
/// Ok(()) => println!("✅ DuckDB binaries are available"),
/// Err(e) => println!("❌ Binary validation failed: {}", e),
/// }
/// ```
///
/// # Binary Search Order
///
/// The function checks for binaries in this order:
///
/// 1. `libduckdb_x86_64.dylib` - Intel/AMD 64-bit optimized binary
/// 2. `libduckdb_arm64.dylib` - Apple Silicon/ARM 64-bit optimized binary
/// 3. `libduckdb.dylib` - Generic fallback binary
///
/// At least one binary must be present for validation to succeed.
///
/// # Error Conditions
///
/// The function will return an error if:
///
/// - `DUCKDB_LIB_DIR` environment variable is not set
/// - The library directory does not exist
/// - No DuckDB binaries are found in the directory
/// - The directory exists but is not accessible
///
/// # Performance
///
/// This function performs file system operations and should be called
/// sparingly. Consider caching the result if you need to check multiple times.
///
/// # Safety
///
/// This function only checks for file existence and does not attempt to
/// load or execute the binaries. It's safe to call even if the binaries
/// are corrupted or incompatible with the current system.