| Crates.io | ufw-rule-parser |
| lib.rs | ufw-rule-parser |
| version | 0.1.0 |
| created_at | 2025-11-19 19:10:51.992649+00 |
| updated_at | 2025-11-19 19:10:51.992649+00 |
| description | parser for a ufw-like firewall rule, including internal/external address keywords. |
| homepage | https://github.com/dddd/firewall-parser |
| repository | https://github.com/dddd/firewall-parser |
| max_upload_size | |
| id | 1940623 |
| size | 37,569 |
ufw-rule-parser is a parser for a small ufw-like firewall rule language.
it uses the pest parsing library and a custom grammar defined in grammar.pest.
it supports address-based rules, service-based rules, and special address keywords such as internal and external.
allow ssh
allow in on eth0 from internal to external port 443 proto tcp
deny out to 8.8.8.8 port 53 proto udp
parse a rules file and print the structured output:
cargo run -- parse examples/sample.rules
output as json format:
cargo run -- parse examples/sample.rules --json
write json output to a file:
cargo run -- parse examples/sample.rules --json --output rules.json
short form for output flag:
cargo run -- parse examples/sample.rules --json -o rules.json
show help:
cargo run -- --help
cargo run -- parse --help
show credits:
cargo run -- credits
the parser supports two output formats:
--json flag to enable.when using --output or -o flag, json is written to the specified file instead of stdout.
the --output flag requires the --json flag to be set.
src/grammar.pest defines the grammar used by the parsersrc/lib.rs exposes the parser api and typed ast structuressrc/main.rs implements the cli interfacetests/grammar_tests.rs contains unit tests for each grammar ruletests/parser_tests.rs contains integration tests for ast parsingexamples/ contains sample rule files for testingfile
└── line* (zero or more lines)
├── service_rule
│ └── action + ident
├── addr_rule
│ ├── action (required)
│ ├── direction? (optional)
│ ├── interface_clause? (optional)
│ └── (from_clause | to_clause | port_clause | proto_clause)+ (one or more)
└── COMMENT? (optional comment)
the grammar in grammar.pest defines rules using pest syntax:
action: matches allow, deny, reject, or limitdirection: matches in or outident: matches identifiers with letters, numbers, underscores, and dashesip: matches ip addresses and cidr notationaddr: matches any, internal, external, or ip addressesport_clause: matches port followed by a numberproto_clause: matches proto followed by tcp, udp, or anyinterface_clause: matches on followed by an identifierfrom_clause and to_clause: match from or to followed by an addressaddr_rule: combines action, optional direction, optional interface, and one or more clausesservice_rule: matches action followed by an identifierline: matches a rule with optional comment, or just a commentfile: matches multiple lines from start to end of inputthe grammar supports:
allow, deny, reject, limitin, outon eth0any, internal, external, or ip/cidrallow sshthe parser reads input text and matches it against the grammar rules.
pest produces a structured tree of pairs that describe which rule matched each part of the input.
the library converts these pairs into typed rust structures (firewallrule, servicerule, addressrule).
integration tests verify that the grammar accepts valid rules and rejects invalid ones.
run all tests:
cargo test
format code:
cargo fmt
lint code:
cargo clippy --all-features --all-targets -- -D warnings
use the makefile for common tasks:
make test
make fmt
make clippy
make run