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
//! # fastn-context
//!
//! Type-safe async context propagation for Rust applications with hierarchical task management
//! and graceful cancellation support.
//!
//! ## Overview
//!
//! `fastn-context` provides a robust context management system designed for async Rust applications.
//! It enables hierarchical context propagation, graceful cancellation, and comprehensive status
//! tracking across your application's task tree.
//!
//! ## Key Features
//!
//! - **Hierarchical Context Management**: Create parent-child relationships between contexts
//! - **Graceful Cancellation**: Built on `tokio::CancellationToken` for clean shutdowns
//! - **Status Tracking**: Monitor the state and progress of operations across your application
//! - **Minimal Overhead**: Lightweight design with efficient async operations
//! - **Easy Integration**: Simple APIs that integrate seamlessly with existing Rust async code
//!
//! ## Quick Start
//!
//! ### Basic Usage
//!
//! ```rust
//! use fastn_context::Context;
//! use tokio::time::{sleep, Duration};
//!
//! #[tokio::main]
//! async fn main() {
//! let ctx = Context::builder("my-app").build();
//!
//! // Spawn a task with context
//! let child_ctx = ctx.child("worker");
//! tokio::spawn(async move {
//! // Your async work here
//! sleep(Duration::from_millis(100)).await;
//! println!("Work completed");
//! });
//!
//! // Monitor status
//! let status = fastn_context::status().await;
//! println!("Active contexts: {}", status.contexts.len());
//! }
//! ```
//!
//! ### Using the `#[main]` Macro
//!
//! For applications that need automatic context setup:
//!
//! ```rust
//! use fastn_context::main;
//!
//! #[main]
//! async fn main() {
//! // Global context is automatically available
//! let ctx = fastn_context::global().await;
//! println!("App: {}", ctx.name());
//! }
//! ```
//!
//! ### Cancellation and Shutdown
//!
//! ```rust
//! use fastn_context::Context;
//! use tokio::select;
//! use tokio::time::{sleep, Duration};
//!
//! #[tokio::main]
//! async fn main() {
//! let ctx = Context::builder("my-app").build();
//!
//! let child_ctx = ctx.child("worker");
//! let token = child_ctx.cancellation_token();
//!
//! tokio::spawn(async move {
//! select! {
//! _ = token.cancelled() => {
//! println!("Task was cancelled");
//! }
//! _ = sleep(Duration::from_secs(10)) => {
//! println!("Task completed normally");
//! }
//! }
//! });
//!
//! // Cancel after 1 second
//! sleep(Duration::from_secs(1)).await;
//! child_ctx.cancel();
//! }
//! ```
//!
//! ## Architecture
//!
//! The crate is built around three main components:
//!
//! - [`Context`]: The core context type for hierarchical task management
//! - [`ContextStatus`]: Status information and monitoring capabilities
//! - [`Status`]: Global status snapshots of the entire context tree
//!
//! ## Integration with fastn Applications
//!
//! This crate was extracted from the [fastn](https://github.com/fastn-stack/fastn) web framework
//! to enable broader adoption of its context management patterns. It's designed to work
//! seamlessly with fastn applications while being useful for any async Rust project.
use tokio as _; // used by main macro
use tokio_util as _; // used for cancellation tokens
pub use ;
pub use ;
// Re-export main macro
pub use main;