Skip to content

Commit

Permalink
docs(readme): update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
hack3ric committed Nov 26, 2024
1 parent edeef98 commit e288e5b
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 31 deletions.
34 changes: 18 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,24 +59,26 @@ Check out the `tests/` directory for more usage examples.
This example applies a ruleset that creates and deletes a table to nftables.

```rust
use nft::{batch::Batch, helper, schema, types};
use nftables::{batch::Batch, helper, schema, types};

/// Applies a ruleset to nftables.
fn test_apply_ruleset() {
let ruleset = example_ruleset();
nft::helper::apply_ruleset(&ruleset, None, None).unwrap();
helper::apply_ruleset(&ruleset, None, None).unwrap();
}

fn example_ruleset() -> schema::Nftables {
fn example_ruleset() -> schema::Nftables<'static> {
let mut batch = Batch::new();
batch.add(schema::NfListObject::Table(schema::Table::new(
types::NfFamily::IP,
"test-table-01".to_string(),
)));
batch.delete(schema::NfListObject::Table(schema::Table::new(
types::NfFamily::IP,
"test-table-01".to_string(),
)));
batch.add(schema::NfListObject::Table(schema::Table {
family: types::NfFamily::IP,
name: "test-table-01".into(),
..Default::default()
}));
batch.delete(schema::NfListObject::Table(schema::Table {
family: types::NfFamily::IP,
name: "test-table-01".into(),
..Default::default()
}));
batch.to_nftables()
}
```
Expand All @@ -90,16 +92,16 @@ fn test_chain_table_rule_inet() {
// nft add table inet some_inet_table
// nft add chain inet some_inet_table some_inet_chain '{ type filter hook forward priority 0; policy accept; }'
let expected: Nftables = Nftables {
objects: vec![
objects: Cow::Borrowed(&[
NfObject::CmdObject(NfCmd::Add(NfListObject::Table(Table {
family: NfFamily::INet,
name: "some_inet_table".to_string(),
name: Cow::Borrowed("some_inet_table"),
handle: None,
}))),
NfObject::CmdObject(NfCmd::Add(NfListObject::Chain(Chain {
family: NfFamily::INet,
table: "some_inet_table".to_string(),
name: "some_inet_chain".to_string(),
table: Cow::Borrowed("some_inet_table"),
name: Cow::Borrowed("some_inet_chain"),
newname: None,
handle: None,
_type: Some(NfChainType::Filter),
Expand All @@ -108,7 +110,7 @@ fn test_chain_table_rule_inet() {
dev: None,
policy: Some(NfChainPolicy::Accept),
}))),
],
]),
};
let json = json!({"nftables":[{"add":{"table":{"family":"inet","name":"some_inet_table"}}},{"add":{"chain":{"family":"inet","table":"some_inet_table","name":"some_inet_chain","type":"filter","hook":"forward","policy":"accept"}}}]});
println!("{}", &json);
Expand Down
2 changes: 1 addition & 1 deletion src/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub struct Batch<'a> {
data: Vec<NfObject<'a>>,
}

impl<'a> Default for Batch<'a> {
impl Default for Batch<'_> {
fn default() -> Self {
Self::new()
}
Expand Down
26 changes: 13 additions & 13 deletions src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ pub struct Table<'a> {
}

/// Default table.
impl<'a> Default for Table<'a> {
impl Default for Table<'_> {
fn default() -> Self {
Table {
family: DEFAULT_FAMILY,
Expand Down Expand Up @@ -226,7 +226,7 @@ pub struct Chain<'a> {
}

/// Default Chain.
impl<'a> Default for Chain<'a> {
impl Default for Chain<'_> {
fn default() -> Self {
Chain {
family: DEFAULT_FAMILY,
Expand Down Expand Up @@ -276,7 +276,7 @@ pub struct Rule<'a> {
}

/// Default rule with no expressions.
impl<'a> Default for Rule<'a> {
impl Default for Rule<'_> {
fn default() -> Self {
Rule {
family: DEFAULT_FAMILY,
Expand Down Expand Up @@ -336,7 +336,7 @@ pub struct Set<'a> {
}

/// Default set `"myset"` with type `ipv4_addr`.
impl<'a> Default for Set<'a> {
impl Default for Set<'_> {
fn default() -> Self {
Set {
family: DEFAULT_FAMILY,
Expand Down Expand Up @@ -405,7 +405,7 @@ pub struct Map<'a> {
}

/// Default map "mymap" that maps ipv4addrs.
impl<'a> Default for Map<'a> {
impl Default for Map<'_> {
fn default() -> Self {
Map {
family: DEFAULT_FAMILY,
Expand Down Expand Up @@ -521,7 +521,7 @@ pub struct Element<'a> {
}

/// Default manipulation element for [set](Set) "myset".
impl<'a> Default for Element<'a> {
impl Default for Element<'_> {
fn default() -> Self {
Element {
family: DEFAULT_FAMILY,
Expand Down Expand Up @@ -565,7 +565,7 @@ pub struct FlowTable<'a> {
}

/// Default [flowtable](FlowTable) named "myflowtable".
impl<'a> Default for FlowTable<'a> {
impl Default for FlowTable<'_> {
fn default() -> Self {
FlowTable {
family: DEFAULT_FAMILY,
Expand Down Expand Up @@ -604,7 +604,7 @@ pub struct Counter<'a> {
}

/// Default [counter](Counter) named "mycounter".
impl<'a> Default for Counter<'a> {
impl Default for Counter<'_> {
fn default() -> Self {
Counter {
family: DEFAULT_FAMILY,
Expand Down Expand Up @@ -651,7 +651,7 @@ pub struct Quota<'a> {
}

/// Default [quota](Quota) named "myquota".
impl<'a> Default for Quota<'a> {
impl Default for Quota<'_> {
fn default() -> Self {
Quota {
family: DEFAULT_FAMILY,
Expand Down Expand Up @@ -692,7 +692,7 @@ pub struct CTHelper<'a> {
}

/// Default ftp [ct helper](CTHelper) named "mycthelper".
impl<'a> Default for CTHelper<'a> {
impl Default for CTHelper<'_> {
fn default() -> Self {
CTHelper {
family: DEFAULT_FAMILY,
Expand Down Expand Up @@ -744,7 +744,7 @@ pub struct Limit<'a> {
}

/// Default [limit](Limit) named "mylimit".
impl<'a> Default for Limit<'a> {
impl Default for Limit<'_> {
fn default() -> Self {
Limit {
family: DEFAULT_FAMILY,
Expand Down Expand Up @@ -811,7 +811,7 @@ pub struct MetainfoObject<'a> {
}

/// Default (empty) [metainfo object](MetainfoObject).
impl<'a> Default for MetainfoObject<'a> {
impl Default for MetainfoObject<'_> {
fn default() -> Self {
MetainfoObject {
version: None,
Expand Down Expand Up @@ -852,7 +852,7 @@ pub struct CTTimeout<'a> {
}

/// Default [ct timeout](CTTimeout) named "mycttimeout"
impl<'a> Default for CTTimeout<'a> {
impl Default for CTTimeout<'_> {
fn default() -> Self {
CTTimeout {
family: DEFAULT_FAMILY,
Expand Down
2 changes: 1 addition & 1 deletion src/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ pub struct Log<'a> {
pub flags: Option<HashSet<LogFlag>>,
}

impl<'a> Log<'a> {
impl Log<'_> {
pub fn new(group: Option<u32>) -> Self {
Log {
prefix: None,
Expand Down

0 comments on commit e288e5b

Please sign in to comment.