Skip to content

Commit f5c3042

Browse files
committed
Handle example errors with expect instead of error-chain
1 parent 7c49bc9 commit f5c3042

File tree

5 files changed

+29
-64
lines changed

5 files changed

+29
-64
lines changed

examples/add_anchor.rs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,18 @@
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() {
13+
let mut pf = PfCtl::new().expect("Unable to connect to PF");
2014

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

2721
println!("Added {} as both a redirect and filter anchor", anchor_name);
2822
}
29-
Ok(())
3023
}

examples/add_rules.rs

Lines changed: 13 additions & 20 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::{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")?;
14+
fn main() {
15+
let mut pf = PfCtl::new().expect("Unable to connect to PF");
2216
pf.try_add_anchor(ANCHOR_NAME, pfctl::AnchorKind::Filter)
23-
.chain_err(|| "Unable to add test filter anchor")?;
17+
.expect("Unable to add test filter anchor");
2418
pf.try_add_anchor(ANCHOR_NAME, pfctl::AnchorKind::Redirect)
25-
.chain_err(|| "Unable to add test redirect anchor")?;
19+
.expect("Unable to add test redirect anchor");
2620

2721
// Create the firewall rule instances
2822
let pass_all_rule = FilterRuleBuilder::default()
@@ -95,26 +89,25 @@ fn run() -> Result<()> {
9589

9690
// Add the rules to the test anchor
9791
pf.add_rule(ANCHOR_NAME, &pass_all_rule)
98-
.chain_err(|| "Unable to add rule")?;
92+
.expect("Unable to add rule");
9993
pf.add_rule(ANCHOR_NAME, &pass_all_ipv4_quick_rule)
100-
.chain_err(|| "Unable to add rule")?;
94+
.expect("Unable to add rule");
10195
pf.add_rule(ANCHOR_NAME, &pass_all_ipv6_on_utun0_rule)
102-
.chain_err(|| "Unable to add rule")?;
96+
.expect("Unable to add rule");
10397
pf.add_rule(ANCHOR_NAME, &block_a_private_net_rule)
104-
.chain_err(|| "Unable to add rule")?;
98+
.expect("Unable to add rule");
10599
pf.add_rule(ANCHOR_NAME, &pass_all_icmp_echo_req)
106-
.chain_err(|| "Unable to add rule")?;
100+
.expect("Unable to add rule");
107101
pf.add_rule(ANCHOR_NAME, &pass_all_icmp_port_unreach)
108-
.chain_err(|| "Unable to add rule")?;
102+
.expect("Unable to add rule");
109103
pf.add_rule(ANCHOR_NAME, &pass_all_icmp_timex_transit)
110-
.chain_err(|| "Unable to add rule")?;
104+
.expect("Unable to add rule");
111105
pf.add_rule(ANCHOR_NAME, &pass_all_icmp_timex_reassembly)
112-
.chain_err(|| "Unable to add rule")?;
106+
.expect("Unable to add rule");
113107
pf.add_redirect_rule(ANCHOR_NAME, &redirect_incoming_tcp_from_port_3000_to_4000)
114-
.chain_err(|| "Unable to add redirect rule")?;
108+
.expect("Unable to add redirect rule");
115109

116110
println!("Added a bunch of rules to the {} anchor.", ANCHOR_NAME);
117111
println!("Run this command to remove them:");
118112
println!("sudo pfctl -a {} -F all", ANCHOR_NAME);
119-
Ok(())
120113
}

examples/enable.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,16 @@
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() {
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().expect("Unable to connect to PF");
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.expect("Unable to enable PF"),
2620
}
27-
Ok(())
2821
}

examples/flush_rules.rs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,20 @@
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() {
13+
let mut pf = PfCtl::new().expect("Unable to connect to PF");
2014

2115
for anchor_name in env::args().skip(1) {
2216
match pf.flush_rules(&anchor_name, pfctl::RulesetKind::Filter) {
2317
Ok(_) => println!("Flushed filter rules under anchor {}", anchor_name),
24-
err => err.chain_err(|| "Unable to flush filter rules")?,
18+
err => err.expect("Unable to flush filter rules"),
2519
}
2620
match pf.flush_rules(&anchor_name, pfctl::RulesetKind::Redirect) {
2721
Ok(_) => println!("Flushed redirect rules under anchor {}", anchor_name),
28-
err => err.chain_err(|| "Unable to flush redirect rules")?,
22+
err => err.expect("Unable to flush redirect rules"),
2923
}
3024
}
31-
Ok(())
3225
}

examples/transaction.rs

Lines changed: 5 additions & 12 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
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")?;
14+
fn main() {
15+
let mut pf = PfCtl::new().expect("Unable to connect to PF");
2216
pf.try_add_anchor(ANCHOR_NAME, pfctl::AnchorKind::Filter)
23-
.chain_err(|| "Unable to add test filter anchor")?;
17+
.expect("Unable to add test filter anchor");
2418
pf.try_add_anchor(ANCHOR_NAME, pfctl::AnchorKind::Redirect)
25-
.chain_err(|| "Unable to add test redirect anchor")?;
19+
.expect("Unable to add test redirect anchor");
2620

2721
// Create some firewall rules that we want to set in one atomic transaction.
2822
let trans_rule1 = pfctl::FilterRuleBuilder::default()
@@ -51,10 +45,9 @@ fn run() -> Result<()> {
5145
// Execute the transaction. This will OVERWRITE any existing rules under this anchor as it's
5246
// a set operation, not an add operation.
5347
pf.set_rules(ANCHOR_NAME, trans_change)
54-
.chain_err(|| "Unable to set rules")?;
48+
.expect("Unable to set rules");
5549

5650
println!("Added a bunch of rules to the {} anchor.", ANCHOR_NAME);
5751
println!("Run this command to remove them:");
5852
println!("sudo pfctl -a {} -F all", ANCHOR_NAME);
59-
Ok(())
6053
}

0 commit comments

Comments
 (0)