pub trait Plugin: Default {
// Required methods
fn spec(&self) -> PluginSpec;
fn check(&self, config: &Config, path: &str) -> Vec<LintError>;
}Expand description
Trait that all plugins must implement.
A plugin consists of two parts:
- Metadata (
spec()) describing the rule name, category, severity, and documentation - Logic (
check()) that inspects the parsed nginx config and reports errors
Plugins must also derive Default, which is used by export_component_plugin!
to instantiate the plugin.
§Example
use nginx_lint_plugin::prelude::*;
#[derive(Default)]
pub struct MyPlugin;
impl Plugin for MyPlugin {
fn spec(&self) -> PluginSpec {
PluginSpec::new("my-rule", "security", "Check for something")
.with_severity("warning")
}
fn check(&self, config: &Config, _path: &str) -> Vec<LintError> {
let mut errors = Vec::new();
let err = self.spec().error_builder();
for ctx in config.all_directives_with_context() {
if ctx.is_inside("http") && ctx.directive.is("bad_directive") {
errors.push(err.warning_at("Avoid bad_directive", ctx.directive));
}
}
errors
}
}
// export_component_plugin!(MyPlugin); // Required for WASM build
// Verify it works
let plugin = MyPlugin;
let config = nginx_lint_plugin::parse_string("http { bad_directive on; }").unwrap();
let errors = plugin.check(&config, "test.conf");
assert_eq!(errors.len(), 1);Required Methods§
Sourcefn spec(&self) -> PluginSpec
fn spec(&self) -> PluginSpec
Return plugin metadata.
This is called once at plugin load time. Use PluginSpec::new() to create
the spec, then chain builder methods like with_severity(),
with_why(), with_bad_example(), etc.
Sourcefn check(&self, config: &Config, path: &str) -> Vec<LintError>
fn check(&self, config: &Config, path: &str) -> Vec<LintError>
Check the configuration and return any lint errors.
Called once per file being linted. The config parameter contains the parsed
AST of the nginx configuration file. The path parameter is the file path
being checked (useful for error messages).
Use config.all_directives() for simple iteration
or config.all_directives_with_context()
when you need to know the parent block context.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.