use rand::prelude::*;
use std::fmt::{self, Display, Formatter};
use woah::prelude::*;
type RequestResult<T> = Result<T, LocalError, FatalError>;
fn main() {
match get_data() {
Success(data) => println!("{}", data),
LocalErr(e) => eprintln!("error: {}", e),
FatalErr(e) => eprintln!("error: {}", e),
}
}
fn get_data() -> RequestResult<String> {
match do_http_request()? {
Ok(data) => Success(data),
Err(e) => {
eprintln!("error: {}... retrying", e);
get_data()
}
}
}
fn do_http_request() -> RequestResult<String> {
if random() {
LocalErr(LocalError::RequestTimedOut)
} else {
FatalErr(FatalError::RequestFailed)
}
}
#[derive(Debug)]
enum LocalError {
RequestTimedOut,
}
#[derive(Debug)]
enum FatalError {
RequestFailed,
}
impl Display for LocalError {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
LocalError::RequestTimedOut => write!(f, "request timed out"),
}
}
}
impl Display for FatalError {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
FatalError::RequestFailed => write!(f, "request failed"),
}
}
}