forge_runtime/lib.rs
1//! ForgeKit runtime layer - File watching and caching.
2//!
3//! This crate provides runtime services for the ForgeKit SDK:
4//!
5//! - File watching with `notify` crate
6//! - Incremental indexing
7//! - Query caching
8//! - Connection pooling
9//!
10//! # Status
11//!
12//! This crate is currently a stub. Full implementation is planned for v0.3.
13
14use std::time::Duration;
15
16/// Runtime configuration for indexing and caching.
17#[derive(Clone, Debug)]
18pub struct RuntimeConfig {
19 /// Enable file watching
20 pub watch_enabled: bool,
21 /// Cache TTL for symbol queries
22 pub symbol_cache_ttl: Duration,
23 /// Cache TTL for CFG queries
24 pub cfg_cache_ttl: Duration,
25 /// Maximum cache size
26 pub max_cache_size: usize,
27}
28
29impl Default for RuntimeConfig {
30 fn default() -> Self {
31 Self {
32 watch_enabled: false,
33 symbol_cache_ttl: Duration::from_secs(300),
34 cfg_cache_ttl: Duration::from_secs(600),
35 max_cache_size: 10_000,
36 }
37 }
38}
39
40/// Runtime statistics.
41#[derive(Clone, Debug)]
42pub struct RuntimeStats {
43 /// Current number of cached entries
44 pub cache_size: usize,
45 /// Whether file watcher is active
46 pub watch_active: bool,
47 /// Number of reindex operations performed
48 pub reindex_count: u64,
49}
50
51/// ForgeKit runtime for automatic reindexing and caching.
52///
53/// # Examples
54///
55/// ```rust,no_run
56/// use forge_runtime::ForgeRuntime;
57///
58/// # #[tokio::main]
59/// # async fn main() -> anyhow::Result<()> {
60/// # let runtime = ForgeRuntime::new("./my-project").await?;
61/// #
62/// # // Start file watcher
63/// # runtime.watch().await?;
64/// #
65/// # Ok(())
66/// # }
67/// ```
68pub struct ForgeRuntime {
69 pub config: RuntimeConfig,
70}
71
72impl ForgeRuntime {
73 /// Creates a new runtime with default configuration.
74 ///
75 /// # Arguments
76 ///
77 /// * `codebase_path` - Path to codebase
78 pub async fn new(_codebase_path: impl AsRef<std::path::Path>) -> anyhow::Result<Self> {
79 // TODO: Implement runtime initialization
80 Ok(Self {
81 config: RuntimeConfig::default(),
82 })
83 }
84
85 /// Creates a new runtime with custom configuration.
86 pub async fn with_config(
87 _codebase_path: impl AsRef<std::path::Path>,
88 config: RuntimeConfig,
89 ) -> anyhow::Result<Self> {
90 // TODO: Implement runtime initialization
91 Ok(Self { config })
92 }
93
94 /// Starts the file watcher for automatic reindexing.
95 ///
96 /// This will monitor the codebase for changes and trigger
97 /// reindexing as needed.
98 pub async fn watch(&self) -> anyhow::Result<()> {
99 // TODO: Implement file watching
100 Err(anyhow::anyhow!("File watching not yet implemented"))
101 }
102
103 /// Clears all caches.
104 pub async fn clear_cache(&self) -> anyhow::Result<()> {
105 // TODO: Implement cache clearing
106 Err(anyhow::anyhow!("Cache not yet implemented"))
107 }
108
109 /// Gets runtime statistics.
110 pub fn stats(&self) -> RuntimeStats {
111 RuntimeStats {
112 cache_size: 0,
113 watch_active: false,
114 reindex_count: 0,
115 }
116 }
117}
118
119#[cfg(test)]
120mod tests {
121 use super::*;
122
123 #[tokio::test]
124 async fn test_runtime_config_default() {
125 let config = RuntimeConfig::default();
126 assert_eq!(config.symbol_cache_ttl, Duration::from_secs(300));
127 }
128
129 #[tokio::test]
130 async fn test_runtime_creation() {
131 let temp = tempfile::tempdir().unwrap();
132 let runtime = ForgeRuntime::new(temp.path()).await.unwrap();
133 let stats = runtime.stats();
134
135 assert_eq!(stats.cache_size, 0);
136 assert!(!stats.watch_active);
137 }
138}