1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//! # Utilities and supporting functions.
//!
//! `utils` is a collection of helpful tools that may be required throughout the rest of the API.

use std::fmt;

/// Used to return objects from the API to the end-user.
pub type Result<T> = std::result::Result<T, CBAdvError>;

/// Types of errors that can occur.
#[derive(Debug)]
pub enum CBAdvError {
    /// Unable to parse JSON successfully.
    BadParse(String),
    /// Non-200 status code received.
    BadStatus(String),
    /// Could not connect to the service.
    BadConnection(String),
    /// Nothing to do.
    NothingToDo(String),
    /// Unable to locate resource.
    NotFound(String),
    /// General unknown error.
    Unknown(String),
}

impl fmt::Display for CBAdvError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            CBAdvError::Unknown(value) => write!(f, "unknown error occured: {}", value),
            CBAdvError::BadParse(value) => write!(f, "could not parse: {}", value),
            CBAdvError::NothingToDo(value) => write!(f, "nothing to do: {}", value),
            CBAdvError::NotFound(value) => write!(f, "could not find: {}", value),
            CBAdvError::BadStatus(value) => write!(f, "non-zero status occurred: {}", value),
            CBAdvError::BadConnection(value) => write!(f, "could not connect: {}", value),
        }
    }
}

/// Prints out a debug message, wraps `println!` macro.
#[macro_export]
macro_rules! debugln {
    ($fmt:expr $(, $($arg:tt)*)?) => {
        println!(concat!("[DEBUG] ", $fmt), $($($arg)*)?);
    };
}