libedgegrid 0.0.2

This library implements an Authentication handler for the Akamai OPEN EdgeGrid Authentication scheme in Rust
#![cfg_attr(feature = "serde_macros", feature(custom_derive, plugin))]
#![cfg_attr(feature = "serde_macros", feature(custom_attribute))]
#![cfg_attr(feature = "serde_macros", plugin(serde_macros))]
extern crate curl;
#[macro_use] extern crate log;
extern crate url;
extern crate rustc_serialize;
extern crate serde;
extern crate serde_json;
extern crate sodium_sys;
extern crate time;

use curl::http;
use sodium_sys::crypto::utils::init;
use std::fmt;
use std::string;
use std::sync::{Once,ONCE_INIT};

mod request;
mod response;

pub mod auth;
#[cfg(feature = "ccu")] pub mod ccu;
#[cfg(feature = "luna")] pub mod luna;
pub mod version;

static START: Once = ONCE_INIT;

fn init() {
    START.call_once(|| {
        debug!("sodium_sys initialized");
        init::init();
    });
}

#[derive(Debug, Default)]
pub struct EdgeGridError {
    title: String,
    detail: String,
}

impl fmt::Display for EdgeGridError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}: {}", self.title, self.detail)
    }
}

impl From<rustc_serialize::json::DecoderError> for EdgeGridError {
    fn from(e: rustc_serialize::json::DecoderError) -> EdgeGridError {
        EdgeGridError {
            title: String::from("JSON DecoderError"),
            detail: format!("{:?}", e),
        }
    }
}

impl From<rustc_serialize::json::EncoderError> for EdgeGridError {
    fn from(e: rustc_serialize::json::EncoderError) -> EdgeGridError {
        EdgeGridError {
            title: String::from("JSON EncoderError"),
            detail: format!("{:?}", e),
        }
    }
}

impl From<serde_json::error::Error> for EdgeGridError {
    fn from(e: serde_json::error::Error) -> EdgeGridError {
        EdgeGridError {
            title: String::from("Serde JSON Error"),
            detail: format!("{:?}", e),
        }
    }
}

impl From<auth::EdgeGridAuthError> for EdgeGridError {
    fn from(e: auth::EdgeGridAuthError) -> EdgeGridError {
        EdgeGridError {
            title: String::from("EdgeGrid Authorization Header Error"),
            detail: format!("{:?}", e),
        }
    }
}

impl From<curl::ErrCode> for EdgeGridError {
    fn from(e: curl::ErrCode) -> EdgeGridError {
        EdgeGridError {
            title: String::from("curl error"),
            detail: format!("{:?}", e),
        }
    }
}

impl From<string::FromUtf8Error> for EdgeGridError {
    fn from(e: string::FromUtf8Error) -> EdgeGridError {
        EdgeGridError {
            title: String::from("FromUtf8Error"),
            detail: format!("{:?}", e),
        }
    }
}

pub type EdgeGridResponse = Result<http::Response, EdgeGridError>;
pub type EdgeGridResult = Result<String, EdgeGridError>;

#[cfg(all(unix,test))]
const TEST_VER: [u8; 18] = [
    27, 91, 51, 50,
    59, 49, 109, 108,
    105, 98, 101, 100,
    103, 101, 103, 114,
    105, 100
];

#[cfg(all(windows,test))]
const TEST_VER: [u8; 11] = [
    108, 105, 98, 101,
    100, 103, 101, 103,
    114, 105, 100
];

#[test]
#[cfg(unix)]
fn version() {
    use sodium_sys::crypto::utils::secmem;
    let version = version::version(false);
    let vb = version.as_bytes();
    assert!(secmem::memcmp(&vb[..18], &TEST_VER) == 0);
}

#[test]
#[cfg(windows)]
fn version() {
    use sodium_sys::crypto::utils::secmem;
    let version = version::version(false);
    let vb = version.as_bytes();
    assert!(secmem::memcmp(&vb[..11], &TEST_VER) == 0);
}