Expand description
Plugin SDK for building nginx-lint WASM plugins
This crate provides everything needed to create custom lint rules as WASM plugins for nginx-lint.
§Getting Started
- Create a library crate with
crate-type = ["cdylib", "rlib"] - Implement the
Plugintrait - Register with
export_component_plugin! - Build with
cargo build --target wasm32-unknown-unknown --release
§Modules
- [
types] - Core types:Plugin,PluginSpec,LintError,Fix,ConfigExt,DirectiveExt helpers- Utility functions for common checks (domain names, URLs, etc.)testing- Test runner and builder:testing::PluginTestRunner,testing::TestCasenative-native::NativePluginRuleadapter for running plugins without WASMprelude- Convenient re-exports foruse nginx_lint_plugin::prelude::*
§API Versioning
Plugins declare the API version they use via PluginSpec::api_version.
This allows the host to support multiple output formats for backward compatibility.
PluginSpec::new() automatically sets the current API version (API_VERSION).
§Example
use nginx_lint_plugin::prelude::*;
#[derive(Default)]
struct MyRule;
impl Plugin for MyRule {
fn spec(&self) -> PluginSpec {
PluginSpec::new("my-custom-rule", "custom", "My custom lint rule")
.with_severity("warning")
.with_why("Explain why this rule matters.")
.with_bad_example("server {\n dangerous_directive on;\n}")
.with_good_example("server {\n # dangerous_directive removed\n}")
}
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.directive.is("dangerous_directive") {
errors.push(
err.warning_at("Avoid using dangerous_directive", ctx.directive)
);
}
}
errors
}
}
// export_component_plugin!(MyRule); // Required for WASM build
// Verify it works
let plugin = MyRule;
let config = nginx_lint_plugin::parse_string("dangerous_directive on;").unwrap();
let errors = plugin.check(&config, "test.conf");
assert_eq!(errors.len(), 1);Re-exports§
pub use nginx_lint_common::parser;
Modules§
- helpers
- Helper functions for plugin development
- native
- Native plugin adapter.
- prelude
- Prelude module for convenient imports.
- testing
- Testing utilities for plugin development.
Macros§
- export_
component_ plugin - Macro to export a plugin as a WIT component
- fixtures_
dir - Macro to get the fixtures directory path relative to the plugin’s Cargo.toml
Structs§
- AllDirectives
With Context Iter - Iterator over all directives with their parent context.
- Argument
- A single argument to a directive.
- Block
- A brace-delimited block (
{ … }). - Comment
- A comment (# …)
- Config
- Root node of a parsed nginx configuration file.
- Directive
- A directive — either a simple directive (
listen 80;) or a block directive (server { … }). - Directive
With Context - A directive paired with its parent block context.
- Error
Builder - Builder for creating LintError with pre-filled rule and category
- Fix
- Represents a fix that can be applied to automatically resolve a lint error.
- Lint
Error - A lint error reported by a plugin.
- Plugin
Spec - Plugin metadata describing a lint rule.
- Position
- A position (line, column, byte offset) in the source text.
- Span
- A half-open source range defined by a start and end
Position.
Enums§
- Argument
Value - The kind and value of a directive argument.
- Config
Item - An item in the configuration (directive, comment, or blank line).
- Severity
- Severity level for lint errors
Constants§
- API_
VERSION - Current API version for the plugin SDK
Traits§
- Argument
Ext - Extension trait for Argument to add source reconstruction
- Config
Ext - Extension trait for
Configproviding iteration and include-context helpers. - Directive
Ext - Extension trait for
Directiveproviding inspection and fix-generation helpers. - Plugin
- Trait that all plugins must implement.
Functions§
- parse_
string - Parse nginx configuration from a string