lazydns 0.2.63

A light and fast DNS server/forwarder implementation in Rust
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
//! Plugin factory registration system
//!
//! Manages plugin type registration and factory lookup.
//! This module provides the infrastructure for registering plugin factories
//! and creating plugin instances from configuration.

use crate::config::types::PluginConfig;
use crate::plugin::Plugin;
use std::sync::Arc;
use tracing::debug;

/// Plugin factory trait for self-registering plugins
///
/// Implement this trait directly on your plugin type to enable automatic
/// registration without needing a separate factory struct.
///
/// # Example
///
/// ```ignore
/// use lazydns::plugin::factory::PluginFactory;
/// use lazydns::config::types::PluginConfig;
/// use std::sync::Arc;
/// use async_trait::async_trait;
///
/// #[derive(Debug)]
/// struct MyPlugin { config: String }
///
/// impl PluginFactory for MyPlugin {
///     fn create(&self, config: &PluginConfig) -> crate::Result<Arc<dyn lazydns::plugin::Plugin>> {
///         // Create plugin from config
///         Ok(Arc::new(Self { config: "default".to_string() }))
///     }
///
///     fn plugin_type(&self) -> &'static str {
///         "my_plugin"
///     }
///
///     fn aliases(&self) -> &'static [&'static str] {
///         &[]
///     }
/// }
/// ```
pub trait PluginFactory: Send + Sync {
    fn create(&self, config: &PluginConfig) -> crate::Result<Arc<dyn Plugin>>;
    fn plugin_type(&self) -> &'static str;
    fn aliases(&self) -> &'static [&'static str];
}

/// Exec plugin factory trait for exec plugins
///
/// Similar to PluginFactory but specifically for exec plugins that implement ExecPlugin.
pub trait ExecPluginFactory: Send + Sync {
    fn create(&self, prefix: &str, exec_str: &str) -> crate::Result<Arc<dyn Plugin>>;
    fn plugin_type(&self) -> &'static str;
    fn aliases(&self) -> &'static [&'static str];
}

/// Distributed slice for collecting plugin factories
#[linkme::distributed_slice]
pub static PLUGIN_FACTORIES_SLICE: [fn() -> Arc<dyn PluginFactory>];

/// Distributed slice for collecting exec plugin factories
#[linkme::distributed_slice]
pub static EXEC_PLUGIN_FACTORIES_SLICE: [fn() -> Arc<dyn ExecPluginFactory>];

/// Get a plugin factory by type name
pub fn get_plugin_factory(plugin_type: &str) -> Option<Arc<dyn PluginFactory>> {
    // Search through the distributed slice for a factory with matching type
    for factory_constructor in PLUGIN_FACTORIES_SLICE {
        let factory = factory_constructor();
        if factory.plugin_type() == plugin_type {
            return Some(factory);
        }
        // Also check aliases
        for alias in factory.aliases() {
            if *alias == plugin_type {
                return Some(factory);
            }
        }
    }
    None
}

/// Get an exec plugin factory by type name
pub fn get_exec_plugin_factory(plugin_type: &str) -> Option<Arc<dyn ExecPluginFactory>> {
    // Search through the distributed slice for a factory with matching type
    for factory_constructor in EXEC_PLUGIN_FACTORIES_SLICE {
        let factory = factory_constructor();
        if factory.plugin_type() == plugin_type {
            return Some(factory);
        }
        // Also check aliases
        for alias in factory.aliases() {
            if *alias == plugin_type {
                return Some(factory);
            }
        }
    }
    None
}

/// Get all registered plugin types
pub fn get_all_plugin_types() -> Vec<String> {
    let mut types: Vec<String> = PLUGIN_FACTORIES_SLICE
        .iter()
        .flat_map(|factory_constructor| {
            let factory = factory_constructor();
            let mut names = vec![factory.plugin_type().to_string()];
            names.extend(factory.aliases().iter().map(|s| s.to_string()));
            names
        })
        .collect::<std::collections::HashSet<_>>()
        .into_iter()
        .collect();

    types.sort();
    types
}

/// Get all registered exec plugin types
pub fn get_all_exec_plugin_types() -> Vec<String> {
    let mut types: Vec<String> = EXEC_PLUGIN_FACTORIES_SLICE
        .iter()
        .flat_map(|factory_constructor| {
            let factory = factory_constructor();
            let mut names = vec![factory.plugin_type().to_string()];
            names.extend(factory.aliases().iter().map(|s| s.to_string()));
            names
        })
        .collect::<std::collections::HashSet<_>>()
        .into_iter()
        .collect();

    types.sort();
    types
}

/// Initialize all plugin and exec plugin factories
/// This function should be called early in program initialization.
pub fn init() {
    initialize_all_plugin_factories();
    initialize_all_exec_plugin_factories();
}

/// Initialize all plugin factories
///
/// This function ensures that all plugin factory registrations are triggered.
/// It should be called early in program initialization, before any plugins
/// are created from configuration.
///
/// The function works by iterating over all factories in the distributed slice
/// and registering them automatically.
///
/// # Example
///
/// ```rust
/// use lazydns::plugin::factory::initialize_all_plugin_factories;
///
/// // Initialize plugin factories before loading config
/// initialize_all_plugin_factories();
/// ```
pub fn initialize_all_plugin_factories() {
    use once_cell::sync::OnceCell;

    static INIT: OnceCell<()> = OnceCell::new();

    INIT.get_or_init(|| {
        // Force initialization by accessing the distributed slice
        // This ensures all linkme-registered factories are available
        let _ = PLUGIN_FACTORIES_SLICE.len();

        let types = get_all_plugin_types();
        debug!("Initialized {} plugin factories: {:?}", types.len(), types);
    });
}

/// Initialize all exec plugin factories
///
/// Similar to initialize_all_plugin_factories but for exec plugins.
fn initialize_all_exec_plugin_factories() {
    use once_cell::sync::OnceCell;

    static EXEC_INIT: OnceCell<()> = OnceCell::new();

    EXEC_INIT.get_or_init(|| {
        // Force initialization by accessing the distributed slice
        // This ensures all linkme-registered factories are available
        let _ = EXEC_PLUGIN_FACTORIES_SLICE.len();

        let types = get_all_exec_plugin_types();
        debug!(
            "Initialized {} exec plugin factories: {:?}",
            types.len(),
            types
        );
    });
}

/// Backward compatibility macro for registering plugin factories
///
/// This macro is deprecated - use `#[derive(RegisterPlugin)]` instead.
///
/// # Example
///
/// ```ignore
/// register_plugin_builder!(MyPlugin);
/// // Equivalent to:
/// #[derive(RegisterPlugin)]
/// struct MyPlugin;
/// ```
#[macro_export]
#[deprecated(since = "0.2.61", note = "Use #[derive(RegisterPlugin)] instead.")]
macro_rules! register_plugin_builder {
    ($plugin_type:ty) => {
        compile_error!(
            "register_plugin_builder! is deprecated. Use #[derive(RegisterPlugin)] instead."
        );
    };
}

/// Backward compatibility macro for registering exec plugin factories
///
/// This macro is deprecated - use `#[derive(RegisterExecPlugin)]` instead.
///
/// # Example
///
/// ```ignore
/// register_exec_plugin_builder!(MyExecPlugin);
/// // Equivalent to:
/// #[derive(RegisterExecPlugin)]
/// struct MyExecPlugin;
/// ```
#[macro_export]
#[deprecated(since = "0.2.61", note = "Use #[derive(RegisterExecPlugin)] instead.")]
macro_rules! register_exec_plugin_builder {
    ($plugin_type:ty) => {
        compile_error!("register_exec_plugin_builder! is deprecated. Use #[derive(RegisterExecPlugin)] instead.");
    };
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::types::PluginConfig;
    use crate::plugin::Context;
    use crate::{RegisterExecPlugin, RegisterPlugin};
    use async_trait::async_trait;
    use std::sync::Arc;

    // Test that plugins deriving #[derive(RegisterPlugin)] are discoverable
    #[test]
    fn test_derive_register_plugin_discovery() {
        // Define a test plugin type and derive registration
        #[derive(Debug, RegisterPlugin)]
        struct MyMacroPlugin;

        #[async_trait]
        impl crate::plugin::traits::Plugin for MyMacroPlugin {
            async fn execute(&self, _ctx: &mut Context) -> crate::Result<()> {
                Ok(())
            }

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

            fn init(_config: &PluginConfig) -> crate::Result<Arc<dyn crate::plugin::Plugin>> {
                Ok(Arc::new(MyMacroPlugin))
            }
        }

        // Initialize factories and verify discovery
        initialize_all_plugin_factories();
        let types = get_all_plugin_types();
        // Derived name from `MyMacroPlugin` -> `my_macro`
        assert!(
            types.iter().any(|t| t == "my_macro"),
            "Expected derived factory name 'my_macro' present"
        );
        assert!(
            get_plugin_factory("my_macro").is_some(),
            "Factory for 'my_macro' should be found"
        );

        // Ensure factory can create an instance
        let factory = get_plugin_factory("my_macro").unwrap();
        let plugin = factory
            .create(&PluginConfig::new("my_macro".to_string()))
            .unwrap();
        assert_eq!(plugin.name(), "my_macro_plugin");
    }

    // Test that exec plugins deriving #[derive(RegisterExecPlugin)] are discoverable
    #[test]
    fn test_derive_register_exec_plugin_discovery() {
        #[derive(Debug, RegisterExecPlugin)]
        struct MyExecPlugin;

        impl crate::plugin::traits::ExecPlugin for MyExecPlugin {
            fn quick_setup(
                _prefix: &str,
                _exec_str: &str,
            ) -> crate::Result<Arc<dyn crate::plugin::Plugin>> {
                Ok(Arc::new(MyExecPlugin))
            }
        }

        #[async_trait]
        impl crate::plugin::traits::Plugin for MyExecPlugin {
            async fn execute(&self, _ctx: &mut Context) -> crate::Result<()> {
                Ok(())
            }

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

            fn init(_config: &PluginConfig) -> crate::Result<Arc<dyn crate::plugin::Plugin>> {
                // Not used for exec quick_setup
                Err(crate::Error::Config("not supported".to_string()))
            }

            fn aliases() -> &'static [&'static str] {
                &[]
            }
        }

        initialize_all_exec_plugin_factories();
        let exec_types = get_all_exec_plugin_types();
        // Derived name: MyExecPlugin -> strip suffix -> MyExec -> my_exec
        assert!(
            exec_types.iter().any(|t| t == "my_exec"),
            "Expected exec factory name 'my_exec' present"
        );
        assert!(
            get_exec_plugin_factory("my_exec").is_some(),
            "Exec factory for 'my_exec' should be found"
        );

        let exec_factory = get_exec_plugin_factory("my_exec").unwrap();
        let plugin = exec_factory.create("my_exec", "arg").unwrap();
        assert_eq!(plugin.name(), "my_exec_plugin");
    }

    #[test]
    fn test_alias_matching_plugin_factory() {
        #[derive(Debug, RegisterPlugin)]
        struct MyAliasPlugin;

        #[async_trait]
        impl crate::plugin::traits::Plugin for MyAliasPlugin {
            async fn execute(&self, _ctx: &mut Context) -> crate::Result<()> {
                Ok(())
            }

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

            fn init(_config: &PluginConfig) -> crate::Result<Arc<dyn crate::plugin::Plugin>> {
                Ok(Arc::new(MyAliasPlugin))
            }

            fn aliases() -> &'static [&'static str] {
                &["alias_one", "other"]
            }
        }

        initialize_all_plugin_factories();
        // Derived canonical name
        assert!(get_plugin_factory("my_alias").is_some());
        // Aliases should resolve to the same factory
        assert!(get_plugin_factory("alias_one").is_some());
        assert!(get_plugin_factory("other").is_some());

        let f1 = get_plugin_factory("alias_one").unwrap();
        let inst = f1
            .create(&PluginConfig::new("alias_one".to_string()))
            .unwrap();
        assert_eq!(inst.name(), "my_alias_plugin");
    }

    #[test]
    fn test_alias_matching_exec_plugin_factory() {
        #[derive(Debug, RegisterExecPlugin)]
        struct MyAliasExecPlugin;

        impl crate::plugin::traits::ExecPlugin for MyAliasExecPlugin {
            fn quick_setup(
                _prefix: &str,
                _exec_str: &str,
            ) -> crate::Result<Arc<dyn crate::plugin::Plugin>> {
                Ok(Arc::new(MyAliasExecPlugin))
            }
        }

        #[async_trait]
        impl crate::plugin::traits::Plugin for MyAliasExecPlugin {
            async fn execute(&self, _ctx: &mut Context) -> crate::Result<()> {
                Ok(())
            }
            fn name(&self) -> &str {
                "my_alias_exec_plugin"
            }
            fn init(_config: &PluginConfig) -> crate::Result<Arc<dyn crate::plugin::Plugin>> {
                Err(crate::Error::Config("not supported".to_string()))
            }
            fn aliases() -> &'static [&'static str] {
                &["alias_exec"]
            }
        }

        initialize_all_exec_plugin_factories();
        assert!(get_exec_plugin_factory("my_alias_exec").is_some());
        assert!(get_exec_plugin_factory("alias_exec").is_some());

        let f = get_exec_plugin_factory("alias_exec").unwrap();
        let p = f.create("alias_exec", "x").unwrap();
        assert_eq!(p.name(), "my_alias_exec_plugin");
    }
}