adaptive-pipeline-bootstrap 2.0.0

Bootstrap module for optimized, adaptive pipeline crate - handles entry point, DI, signals, and platform abstraction
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
<!--
Adaptive Pipeline
Copyright (c) 2025 Michael Gardner, A Bit of Help, Inc.
SPDX-License-Identifier: BSD-3-Clause
See LICENSE file in the project root.
-->

# adaptive-pipeline-bootstrap

[![License](https://img.shields.io/badge/License-BSD_3--Clause-blue.svg)](https://opensource.org/licenses/BSD-3-Clause)
[![crates.io](https://img.shields.io/crates/v/adaptive-pipeline-bootstrap.svg)](https://crates.io/crates/adaptive-pipeline-bootstrap)
[![Documentation](https://docs.rs/adaptive-pipeline-bootstrap/badge.svg)](https://docs.rs/adaptive-pipeline-bootstrap)

**Bootstrap and platform abstraction layer for the Adaptive Pipeline** - Handles application entry points, dependency injection, signal handling, and cross-platform operations.

## ๐ŸŽฏ Overview

This crate sits **outside the enterprise application layers** and provides the foundational infrastructure needed to bootstrap and run Rust applications with:

- **Platform Abstraction** - Unified API for Unix and Windows
- **Signal Handling** - Graceful shutdown for SIGTERM, SIGINT, SIGHUP
- **CLI Parsing** - Secure argument validation with clap
- **Dependency Injection** - Composition root for wiring services
- **Shutdown Coordination** - CancellationToken-based graceful teardown
- **Exit Code Mapping** - Unix sysexits.h standard codes

### Design Philosophy

- **๐Ÿšช Entry Point** - Application lifecycle management
- **๐ŸŒ Cross-Platform** - Write once, run on Unix/Windows
- **๐Ÿ”’ Security** - Input validation, path traversal prevention
- **โ™ป๏ธ Reusable** - Can bootstrap any Rust CLI application
- **๐Ÿงช Testable** - Trait-based with mock implementations

## ๐Ÿ“ฆ Installation

Add this to your `Cargo.toml`:

```toml
[dependencies]
adaptive-pipeline-bootstrap = "1.0"
```

## ๐Ÿ—๏ธ Architecture Position

Bootstrap is the outermost layer that initializes the application:

```
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚    BOOTSTRAP (This Crate)                 โ”‚
โ”‚  - Entry Point                            โ”‚
โ”‚  - DI Container (Composition Root)        โ”‚
โ”‚  - Platform Abstraction                   โ”‚
โ”‚  - Signal Handling                        โ”‚
โ”‚  - Secure Arg Parsing                     โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                   โ”‚
                   โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚         APPLICATION LAYER                 โ”‚
โ”‚  - Use Cases                              โ”‚
โ”‚  - Application Services                   โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                   โ”‚
                   โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚           DOMAIN LAYER                    โ”‚
โ”‚  - Business Logic                         โ”‚
โ”‚  - Domain Services                        โ”‚
โ”‚  - Entities & Value Objects               โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                   โ–ฒ
                   โ”‚
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚       INFRASTRUCTURE LAYER                โ”‚
โ”‚  - Adapters                               โ”‚
โ”‚  - Repositories                           โ”‚
โ”‚  - External Services                      โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
```

## ๐Ÿ“š Usage Examples

### Basic CLI Bootstrapping

```rust
use bootstrap::{bootstrap_cli, result_to_exit_code};

#[tokio::main]
async fn main() -> std::process::ExitCode {
    // Parse and validate CLI arguments
    let cli = match bootstrap::bootstrap_cli() {
        Ok(cli) => cli,
        Err(e) => {
            eprintln!("CLI Error: {}", e);
            return std::process::ExitCode::from(65); // EX_DATAERR
        }
    };

    // Run application with validated config
    let result = run_application(cli).await;

    // Map result to Unix exit code
    result_to_exit_code(result)
}

async fn run_application(cli: bootstrap::ValidatedCli) -> Result<(), String> {
    println!("Running with config: {:?}", cli);
    Ok(())
}
```

### Platform Abstraction

```rust
use bootstrap::platform::create_platform;

// Get platform-specific implementation
let platform = create_platform();

// Cross-platform API
println!("Platform: {}", platform.platform_name());
println!("CPU cores: {}", platform.cpu_count());
println!("Page size: {} bytes", platform.page_size());

// Memory information
let total = platform.total_memory()?;
let available = platform.available_memory()?;
println!("Memory: {}/{} GB", available / 1_000_000_000, total / 1_000_000_000);

// Platform-specific constants
println!("Line separator: {:?}", platform.line_separator());
println!("Path separator: {:?}", platform.path_separator());
```

### Signal Handling

```rust
use bootstrap::signals::create_signal_handler;
use bootstrap::shutdown::ShutdownCoordinator;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Set up graceful shutdown
    let coordinator = ShutdownCoordinator::new();
    let signal_handler = create_signal_handler(coordinator.token());

    // Start signal monitoring
    tokio::spawn(async move {
        signal_handler.wait_for_signal().await;
        println!("Received shutdown signal - cleaning up...");
    });

    // Run application with cancellation
    run_with_cancellation(coordinator.token()).await?;

    // Wait for graceful shutdown (with timeout)
    coordinator.wait_for_completion(30).await?;

    Ok(())
}
```

### CLI Validation

```rust
use bootstrap::cli::{parse_and_validate, ValidatedCli};

// Parse with automatic validation
let cli = parse_and_validate()?;

// All inputs are validated:
// - No SQL injection patterns
// - No path traversal (../)
// - No dangerous shell characters
// - Numbers within allowed ranges
// - Paths are sanitized

match &cli.command {
    ValidatedCommand::Process { input, output, .. } => {
        // Paths are already validated and safe
        println!("Processing {} -> {}", input.display(), output.display());
    }
    _ => {}
}
```

### Exit Code Mapping

```rust
use bootstrap::exit_code::{map_error_to_exit_code, ExitCode};

fn main() -> std::process::ExitCode {
    let result = dangerous_operation();

    match result {
        Ok(_) => ExitCode::Success.into(),
        Err(e) => {
            // Automatic mapping to Unix exit codes
            let exit_code = map_error_to_exit_code(&e);
            eprintln!("Error: {}", e);
            exit_code.into()
        }
    }
}

// Maps error messages to sysexits.h codes:
// - "file not found" -> EX_NOINPUT (66)
// - "invalid data" -> EX_DATAERR (65)
// - "I/O error" -> EX_IOERR (74)
```

## ๐Ÿ”ง Module Overview

### Platform Abstraction (`platform`)

Provides unified cross-platform API:

```rust
pub trait Platform: Send + Sync {
    // System Information
    fn page_size(&self) -> usize;
    fn cpu_count(&self) -> usize;
    fn total_memory(&self) -> Result<u64, PlatformError>;
    fn available_memory(&self) -> Result<u64, PlatformError>;

    // Platform Constants
    fn line_separator(&self) -> &'static str;
    fn path_separator(&self) -> char;
    fn platform_name(&self) -> &'static str;
    fn temp_dir(&self) -> PathBuf;

    // Security & Permissions
    fn is_elevated(&self) -> bool;
    fn set_permissions(&self, path: &Path, mode: u32) -> Result<(), PlatformError>;
    fn is_executable(&self, path: &Path) -> bool;

    // File Operations
    async fn sync_file(&self, file: &tokio::fs::File) -> Result<(), PlatformError>;
}
```

**Implementations:**
- `UnixPlatform` - Uses libc, /proc, /sys
- `WindowsPlatform` - Uses winapi, with stubs for non-Windows

### Signal Handling (`signals`)

Cross-platform graceful shutdown:

```rust
pub trait SignalHandler: Send + Sync {
    async fn wait_for_signal(&self);
    fn shutdown_requested(&self) -> bool;
}

// Unix: SIGTERM, SIGINT, SIGHUP
// Windows: Ctrl+C, Ctrl+Break
let handler = create_signal_handler(cancellation_token);
```

### CLI Parsing (`cli`)

Secure argument validation with clap:

```rust
pub struct ValidatedCli {
    pub command: ValidatedCommand,
    pub verbose: bool,
    pub config: Option<PathBuf>,
    pub cpu_threads: Option<usize>,
    pub io_threads: Option<usize>,
    pub storage_type: Option<String>,
    pub channel_depth: usize,
}

// Security validations:
// - SQL injection prevention
// - Path traversal checks
// - Shell command injection blocking
// - Numeric range validation
```

### Shutdown Coordination (`shutdown`)

CancellationToken-based coordination:

```rust
pub struct ShutdownCoordinator {
    token: CancellationToken,
    completed: Arc<AtomicBool>,
}

impl ShutdownCoordinator {
    pub fn new() -> Self;
    pub fn token(&self) -> CancellationToken;
    pub fn initiate(&self);
    pub async fn wait_for_completion(&self, timeout_secs: u64) -> Result<()>;
    pub fn mark_complete(&self);
}
```

### Exit Codes (`exit_code`)

Unix sysexits.h standard codes:

```rust
pub enum ExitCode {
    Success = 0,
    Error = 1,
    DataErr = 65,      // Invalid data
    NoInput = 66,      // File not found
    Software = 70,     // Internal error
    IoErr = 74,        // I/O error
}

// Automatic error message mapping
let code = map_error_to_exit_code("file not found");
assert_eq!(code, ExitCode::NoInput);
```

## ๐ŸŽฏ Key Features

### Cross-Platform Compatibility

**Compile-time platform selection:**
```rust
#[cfg(unix)]
pub use unix::UnixPlatform;

#[cfg(windows)]
pub use windows::WindowsPlatform;
```

**Runtime platform detection:**
```rust
let platform = create_platform();
if platform.platform_name() == "macos" {
    // macOS-specific logic
}
```

### Security Validations

All CLI inputs are validated for security:

```rust
// โŒ Rejected patterns:
// - "../../../etc/passwd"     (path traversal)
// - "'; DROP TABLE users; --" (SQL injection)
// - "$(rm -rf /)"            (shell injection)
// - "\x00\x01\x02"           (binary data)

// โœ… Accepted inputs:
// - "/valid/path/to/file.txt"
// - "my-pipeline-name"
// - "8"  (numeric validation)
```

### Graceful Shutdown

**Multi-stage shutdown:**
```
1. Signal received (SIGTERM/SIGINT)
2. CancellationToken triggered
3. Tasks check token and cleanup
4. Coordinator waits for completion
5. Timeout enforcement (default: 30s)
6. Process exits cleanly
```

## ๐Ÿงช Testing

Bootstrap includes testable abstractions:

```rust
#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_platform_detection() {
        let platform = create_platform();
        assert!(platform.cpu_count() >= 1);
        assert!(platform.page_size() >= 512);
    }

    #[tokio::test]
    async fn test_graceful_shutdown() {
        let coordinator = ShutdownCoordinator::new();
        let token = coordinator.token();

        coordinator.initiate();
        assert!(token.is_cancelled());
    }
}
```

## ๐Ÿ“Š Dependencies

Minimal dependencies for cross-platform support:

- **tokio** - Async runtime
- **async-trait** - Async trait support
- **thiserror / anyhow** - Error handling
- **clap** - CLI argument parsing
- **tracing** - Structured logging

**Platform-specific:**
- Unix: `libc`
- Windows: `winapi`

## ๐Ÿ”— Related Crates

- **[adaptive-pipeline]../adaptive-pipeline** - Application layer and CLI
- **[adaptive-pipeline-domain]../adaptive-pipeline-domain** - Pure business logic

## ๐Ÿ“„ License

BSD 3-Clause License - see [LICENSE](../LICENSE) file for details.

## ๐Ÿค Contributing

Contributions should focus on:
- โœ… Cross-platform compatibility
- โœ… Security hardening
- โœ… Signal handling improvements
- โœ… Platform abstraction enhancements
- โŒ Not business logic (belongs in domain)
- โŒ Not application logic (belongs in application layer)

---

**Cross-Platform Foundation | Secure by Default | Production-Ready**