pub trait Plugin: Default {
// Required methods
fn spec(&self) -> PluginSpec;
fn check(&self, config: &Config, path: &str) -> Vec<LintError>;
// Provided method
fn relevant_directives(&self) -> Option<&'static [&'static str]> { ... }
}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.
Provided Methods§
Sourcefn relevant_directives(&self) -> Option<&'static [&'static str]>
fn relevant_directives(&self) -> Option<&'static [&'static str]>
Declare the directive names this plugin’s check()
reads, if it only reads a fixed, known set.
When overridden, the host builds config from a snapshot pruned to
only those directive names (plus the ancestor directives needed for
block-context queries like is_inside
to keep working) instead of the whole file, which is faster the less
of the file is relevant. Comments and blank lines are always
omitted from the pruned config, so do not override this if check
reads ConfigItem::Comment or ConfigItem::BlankLine — the
default (None) gives check the complete, unpruned config, as
before this method existed.
This is purely a guest-side, non-breaking hint: it is never part of the WIT component interface, so overriding it does not change what a plugin exports or its compatibility with any host version.
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.