Skip to content

Commit

Permalink
ch8: add misc error examples
Browse files Browse the repository at this point in the history
  • Loading branch information
timClicks committed Jun 30, 2022
1 parent c35ea86 commit ef93402
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 0 deletions.
7 changes: 7 additions & 0 deletions ch8/misc/io-error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use std::fs::File;

fn main() -> Result<(), std::io::Error> {
let _f = File::open("invisible.txt")?;

Ok(())
}
43 changes: 43 additions & 0 deletions ch8/misc/mac-addresses.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use std::fmt;
use std::fmt::Display;

#[derive(Debug)]
struct MacAddress([u8; 6]);

impl Display for MacAddress {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let octet = self.0;
write!(
f,
"{:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}",
octet[0], octet[1], octet[2], octet[3], octet[4], octet[5]
)
}
}

impl MacAddress {
fn is_local(&self) -> bool {
(self.0[0] & 0b_0000_0010) == 0
}

fn is_universal(&self) -> bool {
!self.is_local()
}

fn is_unicast(&self) -> bool {
(self.0[0] & 0b_0000_0001) == 0
}

fn is_multicast(&self) -> bool {
!self.is_unicast()
}
}

fn main() -> Result<(), std::io::Error> {
let mac = MacAddress([0x6, 0, 0, 0, 0, 0]);
println!("mac: {}", mac);
println!("mac: {:?}", mac);
println!("universal?: {:?}", mac.is_universal());

Ok(())
}
11 changes: 11 additions & 0 deletions ch8/misc/multierror.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use std::fs::File;
use std::net::Ipv6Addr;

fn main() -> Result<(), std::io::Error> {
let _f = File::open("invisible.txt")?;

let _localhost = "::1"
.parse::<Ipv6Addr>()?;

Ok(())
}
10 changes: 10 additions & 0 deletions ch8/misc/traiterror.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use std::fs::File;
use std::error::Error;
use std::net::Ipv6Addr;

fn main() -> Result<(), Box<dyn Error>> {
let _f = File::open("invisible.txt")?;
let _localhost = "::1".parse::<Ipv6Addr>()?;

Ok(())
}
24 changes: 24 additions & 0 deletions ch8/misc/wraperror.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use std::io;
use std::fmt;
use std::net;
use std::fs::File;
use std::net::Ipv6Addr;

#[derive(Debug)]
enum UpstreamError{
IO(io::Error),
Parsing(net::AddrParseError),
}

impl fmt::Display for UpstreamError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?}", self) // <1> Implement Display in terms of Debug
}
}

fn main() -> Result<(), UpstreamError> {
let _f = File::open("invisible.txt").map_err(UpstreamError::IO)?;
let _localhost = "::1".parse::<Ipv6Addr>().map_err(UpstreamError::Parsing)?;

Ok(())
}
36 changes: 36 additions & 0 deletions ch8/misc/wraperror2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use std::io;
use std::fmt;
use std::net;
use std::fs::File;
use std::net::Ipv6Addr;

#[derive(Debug)]
enum UpstreamError{
IO(io::Error),
Parsing(net::AddrParseError),
}

impl fmt::Display for UpstreamError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?}", self) // <1> Implement Display in terms of Debug
}
}

impl From<io::Error> for UpstreamError {
fn from(error: io::Error) -> Self {
UpstreamError::IO(error)
}
}

impl From<net::AddrParseError> for UpstreamError {
fn from(error: net::AddrParseError) -> Self {
UpstreamError::Parsing(error)
}
}

fn main() -> Result<(), UpstreamError> {
let _f = File::open("invisible.txt").map_err(UpstreamError::IO)?;
let _localhost = "::1".parse::<Ipv6Addr>().map_err(UpstreamError::Parsing)?;

Ok(())
}

0 comments on commit ef93402

Please sign in to comment.