Skip to content

Commit ef93402

Browse files
committed
ch8: add misc error examples
1 parent c35ea86 commit ef93402

File tree

6 files changed

+131
-0
lines changed

6 files changed

+131
-0
lines changed

ch8/misc/io-error.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
use std::fs::File;
2+
3+
fn main() -> Result<(), std::io::Error> {
4+
let _f = File::open("invisible.txt")?;
5+
6+
Ok(())
7+
}

ch8/misc/mac-addresses.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
use std::fmt;
2+
use std::fmt::Display;
3+
4+
#[derive(Debug)]
5+
struct MacAddress([u8; 6]);
6+
7+
impl Display for MacAddress {
8+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
9+
let octet = self.0;
10+
write!(
11+
f,
12+
"{:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}",
13+
octet[0], octet[1], octet[2], octet[3], octet[4], octet[5]
14+
)
15+
}
16+
}
17+
18+
impl MacAddress {
19+
fn is_local(&self) -> bool {
20+
(self.0[0] & 0b_0000_0010) == 0
21+
}
22+
23+
fn is_universal(&self) -> bool {
24+
!self.is_local()
25+
}
26+
27+
fn is_unicast(&self) -> bool {
28+
(self.0[0] & 0b_0000_0001) == 0
29+
}
30+
31+
fn is_multicast(&self) -> bool {
32+
!self.is_unicast()
33+
}
34+
}
35+
36+
fn main() -> Result<(), std::io::Error> {
37+
let mac = MacAddress([0x6, 0, 0, 0, 0, 0]);
38+
println!("mac: {}", mac);
39+
println!("mac: {:?}", mac);
40+
println!("universal?: {:?}", mac.is_universal());
41+
42+
Ok(())
43+
}

ch8/misc/multierror.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
use std::fs::File;
2+
use std::net::Ipv6Addr;
3+
4+
fn main() -> Result<(), std::io::Error> {
5+
let _f = File::open("invisible.txt")?;
6+
7+
let _localhost = "::1"
8+
.parse::<Ipv6Addr>()?;
9+
10+
Ok(())
11+
}

ch8/misc/traiterror.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
use std::fs::File;
2+
use std::error::Error;
3+
use std::net::Ipv6Addr;
4+
5+
fn main() -> Result<(), Box<dyn Error>> {
6+
let _f = File::open("invisible.txt")?;
7+
let _localhost = "::1".parse::<Ipv6Addr>()?;
8+
9+
Ok(())
10+
}

ch8/misc/wraperror.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use std::io;
2+
use std::fmt;
3+
use std::net;
4+
use std::fs::File;
5+
use std::net::Ipv6Addr;
6+
7+
#[derive(Debug)]
8+
enum UpstreamError{
9+
IO(io::Error),
10+
Parsing(net::AddrParseError),
11+
}
12+
13+
impl fmt::Display for UpstreamError {
14+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
15+
write!(f, "{:?}", self) // <1> Implement Display in terms of Debug
16+
}
17+
}
18+
19+
fn main() -> Result<(), UpstreamError> {
20+
let _f = File::open("invisible.txt").map_err(UpstreamError::IO)?;
21+
let _localhost = "::1".parse::<Ipv6Addr>().map_err(UpstreamError::Parsing)?;
22+
23+
Ok(())
24+
}

ch8/misc/wraperror2.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
use std::io;
2+
use std::fmt;
3+
use std::net;
4+
use std::fs::File;
5+
use std::net::Ipv6Addr;
6+
7+
#[derive(Debug)]
8+
enum UpstreamError{
9+
IO(io::Error),
10+
Parsing(net::AddrParseError),
11+
}
12+
13+
impl fmt::Display for UpstreamError {
14+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
15+
write!(f, "{:?}", self) // <1> Implement Display in terms of Debug
16+
}
17+
}
18+
19+
impl From<io::Error> for UpstreamError {
20+
fn from(error: io::Error) -> Self {
21+
UpstreamError::IO(error)
22+
}
23+
}
24+
25+
impl From<net::AddrParseError> for UpstreamError {
26+
fn from(error: net::AddrParseError) -> Self {
27+
UpstreamError::Parsing(error)
28+
}
29+
}
30+
31+
fn main() -> Result<(), UpstreamError> {
32+
let _f = File::open("invisible.txt").map_err(UpstreamError::IO)?;
33+
let _localhost = "::1".parse::<Ipv6Addr>().map_err(UpstreamError::Parsing)?;
34+
35+
Ok(())
36+
}

0 commit comments

Comments
 (0)