pf_foundation_utils 0.1.0

Foundation utilities for RAII patterns, resource management, and scoped operations
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
562
563
564
565
//! Scoped operations and callback patterns
//!
//! This module provides high-level APIs for scoped operations that guarantee
//! setup and cleanup. Built on top of the RAII patterns but providing more
//! ergonomic APIs for common use cases.
//!
//! ## Key Features
//!
//! - **Scoped Callbacks**: Execute code with guaranteed setup/cleanup
//! - **Builder Pattern**: Fluent APIs for complex scoped operations
//! - **Exception Safety**: Cleanup happens even during panics
//! - **Return Values**: Scoped operations can return values from callbacks
//! - **Composable**: Scoped operations can be nested and combined

use crate::raii::Guard;

/// Execute a function with a scoped context
///
/// This is the most common scoped operation pattern. It sets up a context,
/// executes a function with that context, and automatically cleans up.
///
/// # Type Parameters
/// - `T`: The type of context
/// - `F`: The type of function to execute
/// - `R`: The return type of the function
///
/// # Arguments
/// - `context`: The context value to set up
/// - `f`: The function to execute with the context
///
/// # Example
/// ```rust
/// use foundation_utils::scoped::with_context;
///
/// let result = with_context("my_context", |ctx| {
///     println!("Working with context: {}", ctx);
///     42
/// });
/// assert_eq!(result, 42);
/// ```
pub fn with_context<T, F, R>(context: T, f: F) -> R
where
    F: FnOnce(&T) -> R,
{
    f(&context)
}

/// Execute a function with scoped setup and cleanup
///
/// This pattern provides explicit setup and cleanup functions that are
/// guaranteed to be called even if the work function panics.
///
/// # Type Parameters
/// - `S`: Setup function type
/// - `W`: Work function type  
/// - `C`: Cleanup function type
/// - `T`: Resource type returned by setup
/// - `R`: Return type of work function
///
/// # Arguments
/// - `setup`: Function to set up the resource
/// - `work`: Function to do work with the resource
/// - `cleanup`: Function to clean up the resource
///
/// # Example
/// ```rust
/// use foundation_utils::scoped::with_setup_cleanup;
///
/// let result = with_setup_cleanup(
///     || "resource",                    // Setup
///     |resource| format!("used: {}", resource), // Work
///     |resource| println!("cleanup: {}", resource) // Cleanup
/// );
/// ```
pub fn with_setup_cleanup<S, W, C, T, R>(setup: S, work: W, cleanup: C) -> R
where
    S: FnOnce() -> T,
    W: FnOnce(&T) -> R,
    C: FnOnce(T),
{
    let resource = setup();
    let _guard = Guard::new(resource, cleanup);
    work(_guard.resource())
}

/// Execute a function with optional scoped setup and cleanup
///
/// This is useful when setup/cleanup is conditional based on runtime conditions.
///
/// # Example
/// ```rust
/// use foundation_utils::scoped::with_optional_scope;
///
/// let should_setup = true;
/// let result = with_optional_scope(
///     should_setup,
///     || "resource",                    // Setup (only if condition is true)
///     |resource| format!("used: {:?}", resource), // Work
///     |resource| println!("cleanup: {}", resource) // Cleanup
/// );
/// ```
pub fn with_optional_scope<S, W, C, T, R>(condition: bool, setup: S, work: W, cleanup: C) -> R
where
    S: FnOnce() -> T,
    W: FnOnce(Option<&T>) -> R,
    C: FnOnce(T),
{
    if condition {
        let resource = setup();
        let _guard = Guard::new(resource, cleanup);
        work(Some(_guard.resource()))
    } else {
        work(None)
    }
}

/// Trait for scoped callback operations
///
/// This trait provides a standard interface for types that support
/// scoped operations with automatic cleanup.
pub trait ScopedCallback<T> {
    /// Execute a function with scoped access to the resource
    fn with_scope<F, R>(self, f: F) -> R
    where
        F: FnOnce(&T) -> R;
}

/// Builder for creating complex scoped operations
///
/// This builder allows you to compose multiple scoped operations
/// with different setup/cleanup phases.
///
/// # Example
/// ```rust
/// use foundation_utils::scoped::ScopedBuilder;
///
/// let result = ScopedBuilder::new()
///     .with_resource("resource1", |r| println!("cleanup: {}", r))
///     .with_resource("resource2", |r| println!("cleanup: {}", r))
///     .execute(|resources| {
///         format!("used {} resources", resources.len())
///     });
/// ```
pub struct ScopedBuilder<T> {
    resources: Vec<T>,
    cleanups: Vec<Box<dyn FnOnce(T)>>,
}

impl<T> ScopedBuilder<T> {
    /// Create a new scoped builder
    pub fn new() -> Self {
        Self {
            resources: Vec::new(),
            cleanups: Vec::new(),
        }
    }

    /// Add a resource with cleanup to the scoped operation
    ///
    /// # Arguments
    /// - `resource`: The resource to manage
    /// - `cleanup`: Function to clean up the resource when the scope ends
    pub fn with_resource<F>(mut self, resource: T, cleanup: F) -> Self
    where
        F: FnOnce(T) + 'static,
    {
        self.resources.push(resource);
        self.cleanups.push(Box::new(cleanup));
        self
    }

    /// Execute the scoped operation with all resources
    ///
    /// All cleanup functions will be called in reverse order (LIFO)
    /// even if the work function panics.
    pub fn execute<F, R>(mut self, work: F) -> R
    where
        F: FnOnce(&[T]) -> R,
    {
        struct CleanupGuard<T> {
            resources: Vec<T>,
            cleanups: Vec<Box<dyn FnOnce(T)>>,
        }

        impl<T> Drop for CleanupGuard<T> {
            fn drop(&mut self) {
                while let (Some(resource), Some(cleanup)) =
                    (self.resources.pop(), self.cleanups.pop())
                {
                    cleanup(resource);
                }
            }
        }

        let guard = CleanupGuard {
            resources: std::mem::take(&mut self.resources),
            cleanups: std::mem::take(&mut self.cleanups),
        };

        work(&guard.resources)
    }
}

impl<T> Default for ScopedBuilder<T> {
    fn default() -> Self {
        Self::new()
    }
}

/// Scoped operation that automatically manages multiple contexts
///
/// This is a convenience function for managing multiple contexts
/// that need to be set up and torn down together.
///
/// # Example
/// ```rust
/// use foundation_utils::scoped::with_multiple_contexts;
///
/// let contexts = vec!["ctx1", "ctx2", "ctx3"];
/// let result = with_multiple_contexts(
///     contexts,
///     |ctx| println!("setup: {}", ctx),
///     |contexts| {
///         println!("Working with {} contexts", contexts.len());
///         42
///     },
///     |ctx| println!("cleanup: {}", ctx)
/// );
/// ```
pub fn with_multiple_contexts<T, S, W, C, R>(contexts: Vec<T>, setup: S, work: W, cleanup: C) -> R
where
    T: Clone,
    S: Fn(&T),
    W: FnOnce(&[T]) -> R,
    C: Fn(&T),
{
    // Set up all contexts
    for context in &contexts {
        setup(context);
    }

    // Create a guard that will clean up all contexts in reverse order
    let contexts_for_cleanup = contexts.clone();
    let _guard = Guard::new(contexts_for_cleanup, move |contexts| {
        for context in contexts.iter().rev() {
            cleanup(context);
        }
    });

    // Execute the work function
    work(&contexts)
}

/// Macro for creating scoped operations
///
/// This macro provides a convenient syntax for common scoped patterns.
///
/// # Example
/// ```rust
/// use foundation_utils::scoped_operation;
///
/// let result = scoped_operation! {
///     setup => || "resource",
///     work => |resource| format!("used: {}", resource),
///     cleanup => |resource| println!("cleanup: {}", resource)
/// };
/// ```
#[macro_export]
macro_rules! scoped_operation {
    (
        setup => $setup:expr_2021,
        work => $work:expr_2021,
        cleanup => $cleanup:expr_2021
    ) => {
        $crate::scoped::with_setup_cleanup($setup, $work, $cleanup)
    };
}

/// Macro for creating context-based scoped operations
///
/// # Example
/// ```rust
/// use foundation_utils::with_scoped_context;
///
/// let result = with_scoped_context!("my_context", |ctx| {
///     println!("Context: {}", ctx);
///     42
/// });
/// ```
#[macro_export]
macro_rules! with_scoped_context {
    ($context:expr_2021, $work:expr_2021) => {
        $crate::scoped::with_context($context, $work)
    };
}

/// Helper trait for making types support scoped operations
///
/// Implement this trait to add scoped operation support to your types.
pub trait IntoScoped {
    type Resource;

    /// Convert into a scoped resource
    fn into_scoped(self) -> Self::Resource;

    /// Execute a scoped operation with this resource
    fn scoped<F, R>(self, f: F) -> R
    where
        Self: Sized,
        F: FnOnce(&Self::Resource) -> R,
    {
        let resource = self.into_scoped();
        f(&resource)
    }
}

// Implement IntoScoped for common types
impl<T> IntoScoped for T {
    type Resource = T;

    fn into_scoped(self) -> Self::Resource {
        self
    }
}

/// Convenience function for scope-aware error handling
///
/// This function allows you to handle errors within a scoped operation
/// while still ensuring cleanup happens.
///
/// # Example
/// ```rust
/// use foundation_utils::scoped::with_error_scope;
///
/// let result = with_error_scope(
///     || Ok("resource"),
///     |resource| {
///         // Work that might fail
///         if resource.len() > 0 {
///             Ok(format!("processed: {}", resource))
///         } else {
///             Err("empty resource")
///         }
///     },
///     |resource| println!("cleanup: {}", resource)
/// );
/// ```
pub fn with_error_scope<S, W, C, T, R, E>(setup: S, work: W, cleanup: C) -> Result<R, E>
where
    S: FnOnce() -> Result<T, E>,
    W: FnOnce(&T) -> Result<R, E>,
    C: FnOnce(T),
{
    let resource = setup()?;
    let _guard = Guard::new(resource, cleanup);
    work(_guard.resource())
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::atomic::{AtomicUsize, Ordering};
    use std::sync::{Arc, Mutex};

    #[test]
    fn test_with_context() {
        let result = with_context(42, |ctx| *ctx + 10);
        assert_eq!(result, 52);
    }

    #[test]
    fn test_with_setup_cleanup() {
        let cleanup_called = Arc::new(AtomicUsize::new(0));
        let cleanup_called_clone = cleanup_called.clone();

        let result = with_setup_cleanup(
            || 42,
            |resource| *resource + 10,
            move |resource| {
                cleanup_called_clone.store(resource, Ordering::Relaxed);
            },
        );

        assert_eq!(result, 52);
        assert_eq!(cleanup_called.load(Ordering::Relaxed), 42);
    }

    #[test]
    fn test_with_optional_scope() {
        // Test with condition true
        let result = with_optional_scope(
            true,
            || 42,
            |resource| resource.map(|r| *r + 10).unwrap_or(0),
            |_| {}, // cleanup
        );
        assert_eq!(result, 52);

        // Test with condition false
        let result = with_optional_scope(
            false,
            || 42,
            |resource| resource.map(|r| *r + 10).unwrap_or(0),
            |_| {}, // cleanup
        );
        assert_eq!(result, 0);
    }

    #[test]
    fn test_scoped_builder() {
        let result = ScopedBuilder::new()
            .with_resource("test1", |_| {})
            .with_resource("test2", |_| {})
            .execute(|resources| resources.len());

        assert_eq!(result, 2);
    }

    #[test]
    fn test_with_multiple_contexts() {
        let setup_count = Arc::new(AtomicUsize::new(0));
        let cleanup_count = Arc::new(AtomicUsize::new(0));

        let setup_count_clone = setup_count.clone();
        let cleanup_count_clone = cleanup_count.clone();

        let contexts = vec![1, 2, 3];
        let result = with_multiple_contexts(
            contexts,
            move |_| {
                setup_count_clone.fetch_add(1, Ordering::Relaxed);
            },
            |contexts| contexts.len(),
            move |_| {
                cleanup_count_clone.fetch_add(1, Ordering::Relaxed);
            },
        );

        assert_eq!(result, 3);
        assert_eq!(setup_count.load(Ordering::Relaxed), 3);
        assert_eq!(cleanup_count.load(Ordering::Relaxed), 3);
    }

    #[test]
    fn test_with_error_scope() {
        // Test success case
        let result: Result<i32, &str> = with_error_scope(
            || Ok(42),
            |resource| Ok(*resource + 10),
            |_| {}, // cleanup
        );
        assert_eq!(result, Ok(52));

        // Test error in setup
        let result: Result<i32, &str> = with_error_scope(
            || Err("setup failed"),
            |resource| Ok(*resource + 10),
            |_: i32| {}, // cleanup
        );
        assert_eq!(result, Err("setup failed"));

        // Test error in work
        let result: Result<i32, &str> = with_error_scope(
            || Ok(42),
            |_| Err("work failed"),
            |_| {}, // cleanup should still happen
        );
        assert_eq!(result, Err("work failed"));
    }

    #[test]
    fn test_into_scoped() {
        let result = 42.scoped(|resource| *resource + 10);
        assert_eq!(result, 52);
    }

    #[test]
    fn test_panic_safety_in_scoped_operations() {
        let cleanup_called = Arc::new(AtomicUsize::new(0));
        let cleanup_called_clone = cleanup_called.clone();

        let result = std::panic::catch_unwind(|| {
            with_setup_cleanup(
                || 42,
                |_| panic!("test panic"),
                move |resource| {
                    cleanup_called_clone.store(resource, Ordering::Relaxed);
                },
            )
        });

        assert!(result.is_err());
        assert_eq!(cleanup_called.load(Ordering::Relaxed), 42);
    }

    #[test]
    fn test_scoped_builder_cleanups_called() {
        let counter = Arc::new(AtomicUsize::new(0));
        let c1 = counter.clone();
        let c2 = counter.clone();
        let c3 = counter.clone();

        let result = ScopedBuilder::new()
            .with_resource(10, move |_| {
                c1.fetch_add(1, Ordering::SeqCst);
            })
            .with_resource(20, move |_| {
                c2.fetch_add(1, Ordering::SeqCst);
            })
            .with_resource(30, move |_| {
                c3.fetch_add(1, Ordering::SeqCst);
            })
            .execute(|resources| {
                assert_eq!(resources, &[10, 20, 30]);
                resources.iter().sum::<i32>()
            });

        assert_eq!(result, 60);
        assert_eq!(counter.load(Ordering::SeqCst), 3);
    }

    #[test]
    fn test_scoped_builder_lifo_cleanup_order() {
        let order = Arc::new(Mutex::new(Vec::new()));
        let o1 = order.clone();
        let o2 = order.clone();
        let o3 = order.clone();

        ScopedBuilder::new()
            .with_resource("first", move |r| {
                o1.lock().unwrap().push(r);
            })
            .with_resource("second", move |r| {
                o2.lock().unwrap().push(r);
            })
            .with_resource("third", move |r| {
                o3.lock().unwrap().push(r);
            })
            .execute(|_| {});

        let cleaned = order.lock().unwrap();
        assert_eq!(&*cleaned, &["third", "second", "first"]);
    }

    #[test]
    fn test_scoped_builder_panic_safety() {
        let counter = Arc::new(AtomicUsize::new(0));
        let c1 = counter.clone();
        let c2 = counter.clone();

        let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
            ScopedBuilder::new()
                .with_resource(1, move |_| {
                    c1.fetch_add(1, Ordering::SeqCst);
                })
                .with_resource(2, move |_| {
                    c2.fetch_add(1, Ordering::SeqCst);
                })
                .execute(|_| panic!("work panicked"))
        }));

        assert!(result.is_err());
        assert_eq!(counter.load(Ordering::SeqCst), 2);
    }
}