Skip to content

Commit f5f8fe4

Browse files
committed
Stop using error_chain! in examples
1 parent 8548959 commit f5f8fe4

File tree

5 files changed

+31
-81
lines changed

5 files changed

+31
-81
lines changed

examples/add_anchor.rs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,15 @@
66
// option. This file may not be copied, modified, or distributed
77
// except according to those terms.
88

9-
#[macro_use]
10-
extern crate error_chain;
11-
129
use pfctl::PfCtl;
1310
use std::env;
1411

15-
error_chain! {}
16-
quick_main!(run);
17-
18-
fn run() -> Result<()> {
19-
let mut pf = PfCtl::new().chain_err(|| "Unable to connect to PF")?;
12+
fn main() -> Result<(), Box<dyn std::error::Error>> {
13+
let mut pf = PfCtl::new()?;
2014

2115
for anchor_name in env::args().skip(1) {
22-
pf.try_add_anchor(&anchor_name, pfctl::AnchorKind::Filter)
23-
.chain_err(|| "Unable to add filter anchor")?;
24-
pf.try_add_anchor(&anchor_name, pfctl::AnchorKind::Redirect)
25-
.chain_err(|| "Unable to add redirect anchor")?;
16+
pf.try_add_anchor(&anchor_name, pfctl::AnchorKind::Filter)?;
17+
pf.try_add_anchor(&anchor_name, pfctl::AnchorKind::Redirect)?;
2618

2719
println!("Added {} as both a redirect and filter anchor", anchor_name);
2820
}

examples/add_rules.rs

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,15 @@
66
// option. This file may not be copied, modified, or distributed
77
// except according to those terms.
88

9-
#[macro_use]
10-
extern crate error_chain;
11-
129
use pfctl::{ipnetwork, FilterRuleBuilder, PfCtl, RedirectRuleBuilder};
1310
use std::net::Ipv4Addr;
1411

15-
error_chain! {}
16-
quick_main!(run);
17-
1812
static ANCHOR_NAME: &str = "test.anchor";
1913

20-
fn run() -> Result<()> {
21-
let mut pf = PfCtl::new().chain_err(|| "Unable to connect to PF")?;
22-
pf.try_add_anchor(ANCHOR_NAME, pfctl::AnchorKind::Filter)
23-
.chain_err(|| "Unable to add test filter anchor")?;
24-
pf.try_add_anchor(ANCHOR_NAME, pfctl::AnchorKind::Redirect)
25-
.chain_err(|| "Unable to add test redirect anchor")?;
14+
fn main() -> Result<(), Box<dyn std::error::Error>> {
15+
let mut pf = PfCtl::new()?;
16+
pf.try_add_anchor(ANCHOR_NAME, pfctl::AnchorKind::Filter)?;
17+
pf.try_add_anchor(ANCHOR_NAME, pfctl::AnchorKind::Redirect)?;
2618

2719
// Create the firewall rule instances
2820
let pass_all_rule = FilterRuleBuilder::default()
@@ -94,24 +86,15 @@ fn run() -> Result<()> {
9486
.unwrap();
9587

9688
// Add the rules to the test anchor
97-
pf.add_rule(ANCHOR_NAME, &pass_all_rule)
98-
.chain_err(|| "Unable to add rule")?;
99-
pf.add_rule(ANCHOR_NAME, &pass_all_ipv4_quick_rule)
100-
.chain_err(|| "Unable to add rule")?;
101-
pf.add_rule(ANCHOR_NAME, &pass_all_ipv6_on_utun0_rule)
102-
.chain_err(|| "Unable to add rule")?;
103-
pf.add_rule(ANCHOR_NAME, &block_a_private_net_rule)
104-
.chain_err(|| "Unable to add rule")?;
105-
pf.add_rule(ANCHOR_NAME, &pass_all_icmp_echo_req)
106-
.chain_err(|| "Unable to add rule")?;
107-
pf.add_rule(ANCHOR_NAME, &pass_all_icmp_port_unreach)
108-
.chain_err(|| "Unable to add rule")?;
109-
pf.add_rule(ANCHOR_NAME, &pass_all_icmp_timex_transit)
110-
.chain_err(|| "Unable to add rule")?;
111-
pf.add_rule(ANCHOR_NAME, &pass_all_icmp_timex_reassembly)
112-
.chain_err(|| "Unable to add rule")?;
113-
pf.add_redirect_rule(ANCHOR_NAME, &redirect_incoming_tcp_from_port_3000_to_4000)
114-
.chain_err(|| "Unable to add redirect rule")?;
89+
pf.add_rule(ANCHOR_NAME, &pass_all_rule)?;
90+
pf.add_rule(ANCHOR_NAME, &pass_all_ipv4_quick_rule)?;
91+
pf.add_rule(ANCHOR_NAME, &pass_all_ipv6_on_utun0_rule)?;
92+
pf.add_rule(ANCHOR_NAME, &block_a_private_net_rule)?;
93+
pf.add_rule(ANCHOR_NAME, &pass_all_icmp_echo_req)?;
94+
pf.add_rule(ANCHOR_NAME, &pass_all_icmp_port_unreach)?;
95+
pf.add_rule(ANCHOR_NAME, &pass_all_icmp_timex_transit)?;
96+
pf.add_rule(ANCHOR_NAME, &pass_all_icmp_timex_reassembly)?;
97+
pf.add_redirect_rule(ANCHOR_NAME, &redirect_incoming_tcp_from_port_3000_to_4000)?;
11598

11699
println!("Added a bunch of rules to the {} anchor.", ANCHOR_NAME);
117100
println!("Run this command to remove them:");

examples/enable.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,17 @@
66
// option. This file may not be copied, modified, or distributed
77
// except according to those terms.
88

9-
#[macro_use]
10-
extern crate error_chain;
11-
129
use pfctl::PfCtl;
1310

14-
error_chain! {}
15-
quick_main!(run);
16-
17-
fn run() -> Result<()> {
11+
fn main() -> Result<(), Box<dyn std::error::Error>> {
1812
// Create a handle to the firewall. This opens the file /dev/pf and requires root.
19-
let mut pf = PfCtl::new().chain_err(|| "Unable to connect to PF")?;
13+
let mut pf = PfCtl::new()?;
2014

2115
// Try to enable the firewall. Equivalent to the CLI command "pfctl -e".
2216
match pf.enable() {
2317
Ok(_) => println!("Enabled PF"),
2418
Err(pfctl::Error(pfctl::ErrorKind::StateAlreadyActive, _)) => (),
25-
err => err.chain_err(|| "Unable to enable PF")?,
19+
err => err?,
2620
}
2721
Ok(())
2822
}

examples/flush_rules.rs

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,17 @@
66
// option. This file may not be copied, modified, or distributed
77
// except according to those terms.
88

9-
#[macro_use]
10-
extern crate error_chain;
11-
129
use pfctl::PfCtl;
1310
use std::env;
1411

15-
error_chain! {}
16-
quick_main!(run);
17-
18-
fn run() -> Result<()> {
19-
let mut pf = PfCtl::new().chain_err(|| "Unable to connect to PF")?;
12+
fn main() -> Result<(), Box<dyn std::error::Error>> {
13+
let mut pf = PfCtl::new()?;
2014

2115
for anchor_name in env::args().skip(1) {
22-
match pf.flush_rules(&anchor_name, pfctl::RulesetKind::Filter) {
23-
Ok(_) => println!("Flushed filter rules under anchor {}", anchor_name),
24-
err => err.chain_err(|| "Unable to flush filter rules")?,
25-
}
26-
match pf.flush_rules(&anchor_name, pfctl::RulesetKind::Redirect) {
27-
Ok(_) => println!("Flushed redirect rules under anchor {}", anchor_name),
28-
err => err.chain_err(|| "Unable to flush redirect rules")?,
29-
}
16+
pf.flush_rules(&anchor_name, pfctl::RulesetKind::Filter)?;
17+
println!("Flushed filter rules under anchor {}", anchor_name);
18+
pf.flush_rules(&anchor_name, pfctl::RulesetKind::Redirect)?;
19+
println!("Flushed redirect rules under anchor {}", anchor_name);
3020
}
3121
Ok(())
3222
}

examples/transaction.rs

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,15 @@
66
// option. This file may not be copied, modified, or distributed
77
// except according to those terms.
88

9-
#[macro_use]
10-
extern crate error_chain;
11-
129
use pfctl::PfCtl;
1310
use std::net::Ipv4Addr;
1411

15-
error_chain! {}
16-
quick_main!(run);
17-
1812
static ANCHOR_NAME: &str = "test.anchor";
1913

20-
fn run() -> Result<()> {
21-
let mut pf = PfCtl::new().chain_err(|| "Unable to connect to PF")?;
22-
pf.try_add_anchor(ANCHOR_NAME, pfctl::AnchorKind::Filter)
23-
.chain_err(|| "Unable to add test filter anchor")?;
24-
pf.try_add_anchor(ANCHOR_NAME, pfctl::AnchorKind::Redirect)
25-
.chain_err(|| "Unable to add test redirect anchor")?;
14+
fn main() -> Result<(), Box<dyn std::error::Error>> {
15+
let mut pf = PfCtl::new()?;
16+
pf.try_add_anchor(ANCHOR_NAME, pfctl::AnchorKind::Filter)?;
17+
pf.try_add_anchor(ANCHOR_NAME, pfctl::AnchorKind::Redirect)?;
2618

2719
// Create some firewall rules that we want to set in one atomic transaction.
2820
let trans_rule1 = pfctl::FilterRuleBuilder::default()
@@ -50,8 +42,7 @@ fn run() -> Result<()> {
5042

5143
// Execute the transaction. This will OVERWRITE any existing rules under this anchor as it's
5244
// a set operation, not an add operation.
53-
pf.set_rules(ANCHOR_NAME, trans_change)
54-
.chain_err(|| "Unable to set rules")?;
45+
pf.set_rules(ANCHOR_NAME, trans_change)?;
5546

5647
println!("Added a bunch of rules to the {} anchor.", ANCHOR_NAME);
5748
println!("Run this command to remove them:");

0 commit comments

Comments
 (0)