summer 0.5.1

Rust microservice framework like spring boot in java
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
#![doc = include_str!("../../Plugin.md")]

/// Component definition
pub mod component;
/// Lazy component loading for circular dependencies
pub mod lazy;
/// Service is a special Component that supports dependency injection at compile time
pub mod service;

use crate::error::Result;
use crate::{app::AppBuilder, error::AppError};
use async_trait::async_trait;
use component::ComponentRef;
pub use lazy::LazyComponent;
pub use service::Service;
use std::{
    any::{self, Any},
    ops::Deref,
    sync::Arc,
};

// Define inventory collection for auto-registered plugins
inventory::collect!(&'static dyn Plugin);

/// Submit a component plugin for automatic registration
///
/// This macro wraps `inventory::submit!` to avoid users needing to
/// directly depend on the `inventory` crate.
///
/// # Example
/// ```ignore
/// submit_component_plugin!(MyComponentPlugin);
/// ```
#[macro_export]
macro_rules! submit_component_plugin {
    ($ty:ident) => {
        $crate::submit_inventory! {
            &$ty as &dyn $crate::plugin::Plugin
        }
    };
}

/// Plugin Reference
#[derive(Clone)]
pub struct PluginRef(Arc<dyn Plugin>);

/// Defined plugin interface
#[async_trait]
pub trait Plugin: Any + Send + Sync {
    /// Configures the `App` to which this plugin is added.
    async fn build(&self, _app: &mut AppBuilder) {}

    /// Configures the `App` to which this plugin is added.
    /// The immediately plugin will not be added to the registry,
    /// and the plugin cannot obtain components registered in the registry.
    fn immediately_build(&self, _app: &mut AppBuilder) {}

    /// Configures a name for the [`Plugin`] which is primarily used for checking plugin
    /// uniqueness and debugging.
    fn name(&self) -> &str {
        std::any::type_name::<Self>()
    }

    /// A list of plugins to depend on. The plugin will be built after the plugins in this list.
    fn dependencies(&self) -> Vec<&str> {
        vec![]
    }

    /// Whether the plugin should be built immediately when added
    fn immediately(&self) -> bool {
        false
    }
}

impl PluginRef {
    pub(crate) fn new<T: Plugin>(plugin: T) -> Self {
        Self(Arc::new(plugin))
    }
}

impl Deref for PluginRef {
    type Target = dyn Plugin;

    fn deref(&self) -> &Self::Target {
        &*self.0
    }
}

/// Component Registry
pub trait ComponentRegistry {
    /// Get the component reference of the specified type
    fn get_component_ref<T>(&self) -> Option<ComponentRef<T>>
    where
        T: Any + Send + Sync;

    /// Get the component reference of the specified type.
    /// If the component does not exist, it will panic.
    fn get_expect_component_ref<T>(&self) -> ComponentRef<T>
    where
        T: Clone + Send + Sync + 'static,
    {
        self.get_component_ref().unwrap_or_else(|| {
            panic!(
                "{} component not exists in registry",
                std::any::type_name::<T>()
            )
        })
    }

    /// Get the component reference of the specified type.
    /// If the component does not exist, it will return AppError::ComponentNotExist.
    fn try_get_component_ref<T>(&self) -> Result<ComponentRef<T>>
    where
        T: Clone + Send + Sync + 'static,
    {
        self.get_component_ref()
            .ok_or_else(|| AppError::ComponentNotExist(std::any::type_name::<T>()))
    }

    /// Get the component of the specified type
    fn get_component<T>(&self) -> Option<T>
    where
        T: Clone + Send + Sync + 'static;

    /// Get the component of the specified type.
    /// If the component does not exist, it will panic.
    fn get_expect_component<T>(&self) -> T
    where
        T: Clone + Send + Sync + 'static,
    {
        self.get_component().unwrap_or_else(|| {
            panic!(
                "{} component not exists in registry",
                std::any::type_name::<T>()
            )
        })
    }

    /// Get the component of the specified type.
    /// If the component does not exist, it will return AppError::ComponentNotExist.
    fn try_get_component<T>(&self) -> Result<T>
    where
        T: Clone + Send + Sync + 'static,
    {
        self.get_component()
            .ok_or_else(|| AppError::ComponentNotExist(std::any::type_name::<T>()))
    }

    /// Is there a component of the specified type in the registry?
    fn has_component<T>(&self) -> bool
    where
        T: Any + Send + Sync;
}

/// Mutable Component Registry
pub trait MutableComponentRegistry: ComponentRegistry {
    /// Add component to the registry
    fn add_component<C>(&mut self, component: C) -> &mut Self
    where
        C: Clone + any::Any + Send + Sync;
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::app::AppBuilder;

    #[derive(Clone)]
    struct TestComponent {
        value: i32,
    }

    #[derive(Clone)]
    struct AnotherComponent {
        name: String,
    }

    #[tokio::test]
    async fn test_component_registry_add_and_get() {
        let mut app = AppBuilder::default();

        let test_comp = TestComponent { value: 42 };
        app.add_component(test_comp);

        // Should be able to get the component
        let retrieved = app.get_component::<TestComponent>();
        assert!(retrieved.is_some());
        assert_eq!(retrieved.unwrap().value, 42);
    }

    #[tokio::test]
    async fn test_component_registry_get_nonexistent() {
        let app = AppBuilder::default();

        // Should return None for non-existent component
        let retrieved = app.get_component::<TestComponent>();
        assert!(retrieved.is_none());
    }

    #[tokio::test]
    async fn test_component_registry_has_component() {
        let mut app = AppBuilder::default();

        assert!(!app.has_component::<TestComponent>());

        app.add_component(TestComponent { value: 100 });

        assert!(app.has_component::<TestComponent>());
        assert!(!app.has_component::<AnotherComponent>());
    }

    #[tokio::test]
    async fn test_component_registry_multiple_types() {
        let mut app = AppBuilder::default();

        app.add_component(TestComponent { value: 1 });
        app.add_component(AnotherComponent {
            name: "test".to_string(),
        });

        // Both should be retrievable
        let test_comp = app.get_component::<TestComponent>();
        let another_comp = app.get_component::<AnotherComponent>();

        assert!(test_comp.is_some());
        assert!(another_comp.is_some());
        assert_eq!(test_comp.unwrap().value, 1);
        assert_eq!(another_comp.unwrap().name, "test");
    }

    #[tokio::test]
    async fn test_get_component_ref() {
        let mut app = AppBuilder::default();
        app.add_component(TestComponent { value: 999 });

        let comp_ref = app.get_component_ref::<TestComponent>();
        assert!(comp_ref.is_some());

        let comp_ref = comp_ref.unwrap();
        assert_eq!(comp_ref.value, 999);
    }

    #[tokio::test]
    async fn test_try_get_component_success() {
        let mut app = AppBuilder::default();
        app.add_component(TestComponent { value: 50 });

        let result = app.try_get_component::<TestComponent>();
        assert!(result.is_ok());
        assert_eq!(result.unwrap().value, 50);
    }

    #[tokio::test]
    async fn test_try_get_component_failure() {
        let app = AppBuilder::default();

        let result = app.try_get_component::<TestComponent>();
        assert!(result.is_err());
    }

    #[tokio::test]
    #[should_panic(expected = "component not exists in registry")]
    async fn test_get_expect_component_panic() {
        let app = AppBuilder::default();
        let _comp = app.get_expect_component::<TestComponent>();
    }

    #[tokio::test]
    async fn test_get_expect_component_success() {
        let mut app = AppBuilder::default();
        app.add_component(TestComponent { value: 777 });

        let comp = app.get_expect_component::<TestComponent>();
        assert_eq!(comp.value, 777);
    }

    // Plugin tests
    struct TestPlugin {
        name: &'static str,
    }

    #[async_trait]
    impl Plugin for TestPlugin {
        async fn build(&self, app: &mut AppBuilder) {
            app.add_component(TestComponent { value: 123 });
        }

        fn name(&self) -> &str {
            self.name
        }
    }

    struct DependentPlugin;

    #[async_trait]
    impl Plugin for DependentPlugin {
        async fn build(&self, app: &mut AppBuilder) {
            // This plugin depends on TestPlugin's component
            let test_comp = app.get_component::<TestComponent>();
            assert!(test_comp.is_some());

            app.add_component(AnotherComponent {
                name: format!("dependent_{}", test_comp.unwrap().value),
            });
        }

        fn name(&self) -> &str {
            "DependentPlugin"
        }

        fn dependencies(&self) -> Vec<&str> {
            vec!["TestPlugin"]
        }
    }

    #[tokio::test]
    async fn test_plugin_registration() {
        let mut app = AppBuilder::default();
        app.add_plugin(TestPlugin { name: "TestPlugin" });

        assert!(app.is_plugin_added::<TestPlugin>());
    }

    #[tokio::test]
    async fn test_plugin_build_adds_component() {
        let mut app = AppBuilder::default();
        app.add_plugin(TestPlugin { name: "TestPlugin" });

        // Don't call build() to avoid tracing initialization conflict
        // Instead, manually trigger plugin build
        let plugin = TestPlugin { name: "TestPlugin" };
        plugin.build(&mut app).await;

        let comp = app.get_component::<TestComponent>();
        assert!(comp.is_some());
        assert_eq!(comp.unwrap().value, 123);
    }

    #[tokio::test]
    async fn test_plugin_dependencies_order() {
        use std::sync::Once;
        static INIT: Once = Once::new();

        // Only initialize tracing once for all tests
        INIT.call_once(|| {
            let _ = tracing_subscriber::fmt().try_init();
        });

        let mut app = AppBuilder::default();

        // Add in reverse order - dependency resolution should handle this
        app.add_plugin(DependentPlugin);
        app.add_plugin(TestPlugin { name: "TestPlugin" });

        // Manually build plugins to test dependency order
        let test_plugin = TestPlugin { name: "TestPlugin" };
        test_plugin.build(&mut app).await;

        let dependent_plugin = DependentPlugin;
        dependent_plugin.build(&mut app).await;

        // Both components should exist
        assert!(app.has_component::<TestComponent>());
        assert!(app.has_component::<AnotherComponent>());

        let another = app.get_component::<AnotherComponent>().unwrap();
        assert_eq!(another.name, "dependent_123");
    }

    struct ImmediatePlugin;

    #[async_trait]
    impl Plugin for ImmediatePlugin {
        fn immediately_build(&self, app: &mut AppBuilder) {
            app.add_component(TestComponent { value: 999 });
        }

        fn immediately(&self) -> bool {
            true
        }

        fn name(&self) -> &str {
            "ImmediatePlugin"
        }
    }

    #[tokio::test]
    async fn test_immediate_plugin() {
        let mut app = AppBuilder::default();

        // Immediate plugin should build right away
        app.add_plugin(ImmediatePlugin);

        // Component should be available immediately
        assert!(app.has_component::<TestComponent>());
        let comp = app.get_component::<TestComponent>().unwrap();
        assert_eq!(comp.value, 999);
    }

    #[tokio::test]
    #[should_panic(expected = "plugin was already added")]
    async fn test_duplicate_plugin_panic() {
        let mut app = AppBuilder::default();
        app.add_plugin(TestPlugin { name: "TestPlugin" });
        app.add_plugin(TestPlugin { name: "TestPlugin" }); // Should panic
    }

    #[tokio::test]
    #[should_panic(expected = "component was already added")]
    async fn test_duplicate_component_panic() {
        let mut app = AppBuilder::default();
        app.add_component(TestComponent { value: 1 });
        app.add_component(TestComponent { value: 2 }); // Should panic
    }
}