Skip to main content

aft/
runtime_registry.rs

1use std::sync::Arc;
2
3use crate::context::{App, AppContext};
4
5pub type ProjectRuntime = AppContext;
6
7pub struct RuntimeRegistry {
8    app: Arc<App>,
9    single: ProjectRuntime,
10}
11
12impl RuntimeRegistry {
13    pub fn standalone(app: Arc<App>, rt: ProjectRuntime) -> Self {
14        Self { app, single: rt }
15    }
16
17    pub fn app(&self) -> Arc<App> {
18        Arc::clone(&self.app)
19    }
20
21    pub fn current(&self) -> &ProjectRuntime {
22        &self.single
23    }
24
25    pub fn current_mut(&mut self) -> &mut ProjectRuntime {
26        &mut self.single
27    }
28
29    pub fn iter(&self) -> impl Iterator<Item = &ProjectRuntime> {
30        std::iter::once(&self.single)
31    }
32}
33
34#[cfg(test)]
35mod tests {
36    use super::*;
37    use crate::{config::Config, parser::TreeSitterProvider};
38
39    #[test]
40    fn standalone_current_and_iter_return_single_runtime() {
41        let ctx = AppContext::new(Box::new(TreeSitterProvider::new()), Config::default());
42        let app = ctx.app();
43        let mut registry = RuntimeRegistry::standalone(Arc::clone(&app), ctx);
44        assert!(Arc::ptr_eq(&app, &registry.app()));
45        assert!(Arc::ptr_eq(&app, &registry.current().app()));
46
47        let current_ptr = registry.current() as *const ProjectRuntime;
48        let iter_ptrs = registry
49            .iter()
50            .map(|runtime| runtime as *const ProjectRuntime)
51            .collect::<Vec<_>>();
52
53        assert_eq!(iter_ptrs, vec![current_ptr]);
54
55        let current_mut_ptr = registry.current_mut() as *mut ProjectRuntime;
56        assert_eq!(current_mut_ptr as *const ProjectRuntime, current_ptr);
57    }
58}