arch-toolkit 0.3.0

Complete Rust toolkit for Arch Linux package management
Documentation
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
//! Environment variable configuration for arch-toolkit.
//!
//! This module provides utilities for reading configuration from environment variables,
//! allowing zero-code configuration for CI/CD pipelines, Docker containers, and runtime adjustments.

#[cfg(feature = "aur")]
use std::time::Duration;

/// What: Read timeout from `ARCH_TOOLKIT_TIMEOUT` environment variable.
///
/// Inputs: None
///
/// Output:
/// - `Option<Duration>` containing the timeout if the variable is set and valid, `None` otherwise
///
/// Details:
/// - Reads `ARCH_TOOLKIT_TIMEOUT` as seconds (u64)
/// - Returns `None` if variable is not set or cannot be parsed
/// - Invalid values are silently ignored (returns `None`)
#[cfg(feature = "aur")]
#[must_use]
pub fn env_timeout() -> Option<Duration> {
    std::env::var("ARCH_TOOLKIT_TIMEOUT")
        .ok()
        .and_then(|v| v.parse::<u64>().ok())
        .map(Duration::from_secs)
}

/// What: Read user agent from `ARCH_TOOLKIT_USER_AGENT` environment variable.
///
/// Inputs: None
///
/// Output:
/// - `Option<String>` containing the user agent if the variable is set, `None` otherwise
///
/// Details:
/// - Reads `ARCH_TOOLKIT_USER_AGENT` as a string
/// - Returns `None` if variable is not set
/// - Empty strings are treated as unset (returns `None`)
#[cfg(feature = "aur")]
#[must_use]
pub fn env_user_agent() -> Option<String> {
    std::env::var("ARCH_TOOLKIT_USER_AGENT")
        .ok()
        .filter(|s| !s.is_empty())
}

/// What: Read health check timeout from `ARCH_TOOLKIT_HEALTH_CHECK_TIMEOUT` environment variable.
///
/// Inputs: None
///
/// Output:
/// - `Option<Duration>` containing the timeout if the variable is set and valid, `None` otherwise
///
/// Details:
/// - Reads `ARCH_TOOLKIT_HEALTH_CHECK_TIMEOUT` as seconds (u64)
/// - Returns `None` if variable is not set or cannot be parsed
/// - Invalid values are silently ignored (returns `None`)
#[cfg(feature = "aur")]
#[must_use]
pub fn env_health_check_timeout() -> Option<Duration> {
    std::env::var("ARCH_TOOLKIT_HEALTH_CHECK_TIMEOUT")
        .ok()
        .and_then(|v| v.parse::<u64>().ok())
        .map(Duration::from_secs)
}

/// What: Read max retries from `ARCH_TOOLKIT_MAX_RETRIES` environment variable.
///
/// Inputs: None
///
/// Output:
/// - `Option<u32>` containing the max retries if the variable is set and valid, `None` otherwise
///
/// Details:
/// - Reads `ARCH_TOOLKIT_MAX_RETRIES` as u32
/// - Returns `None` if variable is not set or cannot be parsed
/// - Invalid values are silently ignored (returns `None`)
#[cfg(feature = "aur")]
#[must_use]
pub fn env_max_retries() -> Option<u32> {
    std::env::var("ARCH_TOOLKIT_MAX_RETRIES")
        .ok()
        .and_then(|v| v.parse::<u32>().ok())
}

/// What: Read retry enabled flag from `ARCH_TOOLKIT_RETRY_ENABLED` environment variable.
///
/// Inputs: None
///
/// Output:
/// - `Option<bool>` containing the flag if the variable is set and valid, `None` otherwise
///
/// Details:
/// - Reads `ARCH_TOOLKIT_RETRY_ENABLED` as boolean
/// - Accepts: "true", "1", "yes", "on" (case-insensitive) for `true`
/// - Accepts: "false", "0", "no", "off" (case-insensitive) for `false`
/// - Returns `None` if variable is not set or cannot be parsed
#[cfg(feature = "aur")]
#[must_use]
pub fn env_retry_enabled() -> Option<bool> {
    std::env::var("ARCH_TOOLKIT_RETRY_ENABLED")
        .ok()
        .and_then(|v| {
            let lower = v.to_lowercase();
            match lower.as_str() {
                "true" | "1" | "yes" | "on" => Some(true),
                "false" | "0" | "no" | "off" => Some(false),
                _ => None,
            }
        })
}

/// What: Read retry initial delay from `ARCH_TOOLKIT_RETRY_INITIAL_DELAY_MS` environment variable.
///
/// Inputs: None
///
/// Output:
/// - `Option<u64>` containing the delay in milliseconds if the variable is set and valid, `None` otherwise
///
/// Details:
/// - Reads `ARCH_TOOLKIT_RETRY_INITIAL_DELAY_MS` as u64 (milliseconds)
/// - Returns `None` if variable is not set or cannot be parsed
/// - Invalid values are silently ignored (returns `None`)
#[cfg(feature = "aur")]
#[must_use]
pub fn env_retry_initial_delay_ms() -> Option<u64> {
    std::env::var("ARCH_TOOLKIT_RETRY_INITIAL_DELAY_MS")
        .ok()
        .and_then(|v| v.parse::<u64>().ok())
}

/// What: Read retry max delay from `ARCH_TOOLKIT_RETRY_MAX_DELAY_MS` environment variable.
///
/// Inputs: None
///
/// Output:
/// - `Option<u64>` containing the max delay in milliseconds if the variable is set and valid, `None` otherwise
///
/// Details:
/// - Reads `ARCH_TOOLKIT_RETRY_MAX_DELAY_MS` as u64 (milliseconds)
/// - Returns `None` if variable is not set or cannot be parsed
/// - Invalid values are silently ignored (returns `None`)
#[cfg(feature = "aur")]
#[must_use]
pub fn env_retry_max_delay_ms() -> Option<u64> {
    std::env::var("ARCH_TOOLKIT_RETRY_MAX_DELAY_MS")
        .ok()
        .and_then(|v| v.parse::<u64>().ok())
}

/// What: Read validation strict flag from `ARCH_TOOLKIT_VALIDATION_STRICT` environment variable.
///
/// Inputs: None
///
/// Output:
/// - `Option<bool>` containing the flag if the variable is set and valid, `None` otherwise
///
/// Details:
/// - Reads `ARCH_TOOLKIT_VALIDATION_STRICT` as boolean
/// - Accepts: "true", "1", "yes", "on" (case-insensitive) for `true`
/// - Accepts: "false", "0", "no", "off" (case-insensitive) for `false`
/// - Returns `None` if variable is not set or cannot be parsed
#[cfg(feature = "aur")]
#[must_use]
pub fn env_validation_strict() -> Option<bool> {
    std::env::var("ARCH_TOOLKIT_VALIDATION_STRICT")
        .ok()
        .and_then(|v| {
            let lower = v.to_lowercase();
            match lower.as_str() {
                "true" | "1" | "yes" | "on" => Some(true),
                "false" | "0" | "no" | "off" => Some(false),
                _ => None,
            }
        })
}

/// What: Read cache size from `ARCH_TOOLKIT_CACHE_SIZE` environment variable.
///
/// Inputs: None
///
/// Output:
/// - `Option<usize>` containing the cache size if the variable is set and valid, `None` otherwise
///
/// Details:
/// - Reads `ARCH_TOOLKIT_CACHE_SIZE` as usize
/// - Returns `None` if variable is not set or cannot be parsed
/// - Invalid values are silently ignored (returns `None`)
#[cfg(feature = "aur")]
#[must_use]
pub fn env_cache_size() -> Option<usize> {
    std::env::var("ARCH_TOOLKIT_CACHE_SIZE")
        .ok()
        .and_then(|v| v.parse::<usize>().ok())
}

/// Test-only helpers that make process-global environment mutation deterministic.
///
/// Details:
/// - `ARCH_TOOLKIT_*` variables are process-global, so concurrently running unit
///   tests would otherwise observe each other's writes. Every test that reads or
///   writes them must hold the guard returned by [`test_support::lock_env`].
#[cfg(test)]
#[allow(clippy::redundant_pub_crate)]
pub(crate) mod test_support {
    use std::sync::{Mutex, MutexGuard, OnceLock, PoisonError};

    /// Every `ARCH_TOOLKIT_*` variable owned by the crate configuration surface.
    ///
    /// Details:
    /// - The guard clears all of them on acquisition and restores them on drop,
    ///   so each test starts from a known-empty configuration environment.
    pub(crate) const MANAGED_VARS: &[&str] = &[
        "ARCH_TOOLKIT_TIMEOUT",
        "ARCH_TOOLKIT_USER_AGENT",
        "ARCH_TOOLKIT_HEALTH_CHECK_TIMEOUT",
        "ARCH_TOOLKIT_MAX_RETRIES",
        "ARCH_TOOLKIT_RETRY_ENABLED",
        "ARCH_TOOLKIT_RETRY_INITIAL_DELAY_MS",
        "ARCH_TOOLKIT_RETRY_MAX_DELAY_MS",
        "ARCH_TOOLKIT_VALIDATION_STRICT",
        "ARCH_TOOLKIT_CACHE_SIZE",
    ];

    /// Process-wide mutex serializing every environment-mutating test.
    static ENV_MUTEX: OnceLock<Mutex<()>> = OnceLock::new();

    /// What: RAII guard that owns exclusive access to the `ARCH_TOOLKIT_*` environment.
    ///
    /// Inputs:
    /// - Constructed only through [`lock_env`].
    ///
    /// Output:
    /// - Exclusive, restoring access to the managed environment variables.
    ///
    /// Details:
    /// - Holds the process-wide mutex for its whole lifetime.
    /// - Clears all managed variables on acquisition and restores the previously
    ///   observed values on drop, including for panicking tests.
    pub(crate) struct EnvGuard {
        /// Held lock keeping other environment tests out of the critical section.
        _lock: MutexGuard<'static, ()>,
        /// Values observed at acquisition time, restored on drop.
        saved: Vec<(&'static str, Option<String>)>,
    }

    #[allow(clippy::unused_self)]
    impl EnvGuard {
        /// What: Set a managed environment variable inside the guarded section.
        ///
        /// Inputs:
        /// - `key`: One of [`MANAGED_VARS`].
        /// - `value`: Raw value to expose to the configuration readers.
        ///
        /// Output:
        /// - The variable is visible to this process until the guard is dropped.
        ///
        /// Details:
        /// - Panics when `key` is not managed, because such a variable would not
        ///   be restored on drop and could leak into other tests.
        pub(crate) fn set(&self, key: &'static str, value: &str) {
            assert!(
                MANAGED_VARS.contains(&key),
                "{key} is not a managed ARCH_TOOLKIT_* test variable"
            );
            // SAFETY: the guard holds the process-wide environment mutex, so no
            // other test thread reads or writes the environment concurrently.
            unsafe {
                std::env::set_var(key, value);
            }
        }

        /// What: Remove a managed environment variable inside the guarded section.
        ///
        /// Inputs:
        /// - `key`: One of [`MANAGED_VARS`].
        ///
        /// Output:
        /// - The variable is unset for this process until the guard is dropped.
        ///
        /// Details:
        /// - Acquisition already clears every managed variable; this exists for
        ///   tests that unset a value in the middle of a scenario.
        pub(crate) fn remove(&self, key: &'static str) {
            assert!(
                MANAGED_VARS.contains(&key),
                "{key} is not a managed ARCH_TOOLKIT_* test variable"
            );
            // SAFETY: see `set`.
            unsafe {
                std::env::remove_var(key);
            }
        }
    }

    impl Drop for EnvGuard {
        fn drop(&mut self) {
            for (key, value) in &self.saved {
                // SAFETY: the guard still owns the environment mutex here.
                unsafe {
                    match value {
                        Some(previous) => std::env::set_var(key, previous),
                        None => std::env::remove_var(key),
                    }
                }
            }
        }
    }

    /// What: Acquire exclusive, restoring access to the `ARCH_TOOLKIT_*` environment.
    ///
    /// Inputs: None.
    ///
    /// Output:
    /// - An [`EnvGuard`] with every managed variable cleared.
    ///
    /// Details:
    /// - Recovers from mutex poisoning so one failing test cannot cascade into
    ///   unrelated failures; the guard restores state regardless.
    /// - Makes the environment tests deterministic under `cargo test` without
    ///   `--test-threads=1`.
    pub(crate) fn lock_env() -> EnvGuard {
        let lock = ENV_MUTEX
            .get_or_init(|| Mutex::new(()))
            .lock()
            .unwrap_or_else(PoisonError::into_inner);

        let saved = MANAGED_VARS
            .iter()
            .map(|key| (*key, std::env::var(key).ok()))
            .collect();

        for key in MANAGED_VARS {
            // SAFETY: the mutex is held, so no other test thread touches the environment.
            unsafe {
                std::env::remove_var(key);
            }
        }

        EnvGuard { _lock: lock, saved }
    }
}

#[cfg(test)]
#[cfg(feature = "aur")]
#[allow(clippy::significant_drop_tightening)]
mod tests {
    use super::test_support::lock_env;
    use super::*;

    #[test]
    fn test_env_timeout_valid() {
        let env = lock_env();
        env.set("ARCH_TOOLKIT_TIMEOUT", "60");
        assert_eq!(env_timeout(), Some(Duration::from_mins(1)));
    }

    #[test]
    fn test_env_timeout_invalid() {
        let env = lock_env();
        env.set("ARCH_TOOLKIT_TIMEOUT", "invalid");
        assert_eq!(env_timeout(), None);
    }

    #[test]
    fn test_env_timeout_missing() {
        let _env = lock_env();
        assert_eq!(env_timeout(), None);
    }

    #[test]
    fn test_env_user_agent_valid() {
        let env = lock_env();
        env.set("ARCH_TOOLKIT_USER_AGENT", "my-app/1.0");
        assert_eq!(env_user_agent(), Some("my-app/1.0".to_string()));
    }

    #[test]
    fn test_env_user_agent_empty() {
        let env = lock_env();
        env.set("ARCH_TOOLKIT_USER_AGENT", "");
        assert_eq!(env_user_agent(), None);
    }

    #[test]
    fn test_env_user_agent_missing() {
        let _env = lock_env();
        assert_eq!(env_user_agent(), None);
    }

    #[test]
    fn test_env_health_check_timeout_valid() {
        let env = lock_env();
        env.set("ARCH_TOOLKIT_HEALTH_CHECK_TIMEOUT", "10");
        assert_eq!(env_health_check_timeout(), Some(Duration::from_secs(10)));
    }

    #[test]
    fn test_env_health_check_timeout_missing() {
        let _env = lock_env();
        assert_eq!(env_health_check_timeout(), None);
    }

    #[test]
    fn test_env_max_retries_valid() {
        let env = lock_env();
        env.set("ARCH_TOOLKIT_MAX_RETRIES", "5");
        assert_eq!(env_max_retries(), Some(5));
    }

    #[test]
    fn test_env_max_retries_invalid() {
        let env = lock_env();
        env.set("ARCH_TOOLKIT_MAX_RETRIES", "invalid");
        assert_eq!(env_max_retries(), None);
    }

    #[test]
    fn test_env_max_retries_missing() {
        let _env = lock_env();
        assert_eq!(env_max_retries(), None);
    }

    #[test]
    fn test_env_retry_enabled_true() {
        let env = lock_env();
        for value in ["true", "TRUE", "True", "1", "yes", "YES", "on", "ON"] {
            env.set("ARCH_TOOLKIT_RETRY_ENABLED", value);
            assert_eq!(env_retry_enabled(), Some(true), "Failed for value: {value}");
        }
    }

    #[test]
    fn test_env_retry_enabled_false() {
        let env = lock_env();
        for value in ["false", "FALSE", "False", "0", "no", "NO", "off", "OFF"] {
            env.set("ARCH_TOOLKIT_RETRY_ENABLED", value);
            assert_eq!(
                env_retry_enabled(),
                Some(false),
                "Failed for value: {value}"
            );
        }
    }

    #[test]
    fn test_env_retry_enabled_invalid() {
        let env = lock_env();
        env.set("ARCH_TOOLKIT_RETRY_ENABLED", "maybe");
        assert_eq!(env_retry_enabled(), None);
    }

    #[test]
    fn test_env_retry_enabled_missing() {
        let _env = lock_env();
        assert_eq!(env_retry_enabled(), None);
    }

    #[test]
    fn test_env_retry_initial_delay_ms_valid() {
        let env = lock_env();
        env.set("ARCH_TOOLKIT_RETRY_INITIAL_DELAY_MS", "2000");
        assert_eq!(env_retry_initial_delay_ms(), Some(2000));
    }

    #[test]
    fn test_env_retry_initial_delay_ms_missing() {
        let _env = lock_env();
        assert_eq!(env_retry_initial_delay_ms(), None);
    }

    #[test]
    fn test_env_retry_max_delay_ms_valid() {
        let env = lock_env();
        env.set("ARCH_TOOLKIT_RETRY_MAX_DELAY_MS", "60000");
        assert_eq!(env_retry_max_delay_ms(), Some(60000));
    }

    #[test]
    fn test_env_retry_max_delay_ms_missing() {
        let _env = lock_env();
        assert_eq!(env_retry_max_delay_ms(), None);
    }

    #[test]
    fn test_env_validation_strict_true() {
        let env = lock_env();
        for value in ["true", "TRUE", "1", "yes", "on"] {
            env.set("ARCH_TOOLKIT_VALIDATION_STRICT", value);
            assert_eq!(
                env_validation_strict(),
                Some(true),
                "Failed for value: {value}"
            );
        }
    }

    #[test]
    fn test_env_validation_strict_false() {
        let env = lock_env();
        for value in ["false", "FALSE", "0", "no", "off"] {
            env.set("ARCH_TOOLKIT_VALIDATION_STRICT", value);
            assert_eq!(
                env_validation_strict(),
                Some(false),
                "Failed for value: {value}"
            );
        }
    }

    #[test]
    fn test_env_validation_strict_missing() {
        let _env = lock_env();
        assert_eq!(env_validation_strict(), None);
    }

    #[test]
    fn test_env_cache_size_valid() {
        let env = lock_env();
        env.set("ARCH_TOOLKIT_CACHE_SIZE", "200");
        assert_eq!(env_cache_size(), Some(200));
    }

    #[test]
    fn test_env_cache_size_invalid() {
        let env = lock_env();
        env.set("ARCH_TOOLKIT_CACHE_SIZE", "invalid");
        assert_eq!(env_cache_size(), None);
    }

    #[test]
    fn test_env_cache_size_missing() {
        let _env = lock_env();
        assert_eq!(env_cache_size(), None);
    }

    #[test]
    /// What: Verify the environment guard restores pre-existing values on drop.
    ///
    /// Inputs:
    /// - A managed variable set outside the guard, then overwritten inside it.
    ///
    /// Output:
    /// - The original value is observable again after the guard is dropped.
    ///
    /// Details:
    /// - Protects the isolation contract that keeps parallel test runs deterministic.
    fn test_env_guard_restores_previous_values() {
        let outer = lock_env();
        outer.set("ARCH_TOOLKIT_TIMEOUT", "111");

        {
            let inner_saved = std::env::var("ARCH_TOOLKIT_TIMEOUT").ok();
            assert_eq!(inner_saved.as_deref(), Some("111"));
        }

        outer.remove("ARCH_TOOLKIT_TIMEOUT");
        assert_eq!(env_timeout(), None);
    }
}