libedgegrid 0.0.1

This library implements an Authentication handler for the Akamai OPEN EdgeGrid Authentication scheme in Rust
extern crate curl;
#[macro_use] extern crate log;
extern crate url;
extern crate rustc_serialize;
extern crate sodium_sys;
extern crate time;

use sodium_sys::crypto::utils::init;
use std::sync::{Once,ONCE_INIT};

pub mod auth;
pub mod ccu;
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, RustcDecodable, RustcEncodable)]
#[allow(non_snake_case)]
pub struct JSONError {
    title: String,
    detail: String,
}

#[derive(Debug, Default)]
pub struct ApiError {
    json_err: JSONError,
    body: String
}

impl From<rustc_serialize::json::DecoderError> for ApiError {
    fn from(e: rustc_serialize::json::DecoderError) -> ApiError {
        ApiError {
            json_err: JSONError {
                title: String::from("JSON DecoderError"),
                detail: format!("{:?}", e),
            },
            ..Default::default()
        }
    }
}

impl From<rustc_serialize::json::EncoderError> for ApiError {
    fn from(e: rustc_serialize::json::EncoderError) -> ApiError {
        ApiError {
            json_err: JSONError {
                title: String::from("JSON EncoderError"),
                detail: format!("{:?}", e),
            },
            ..Default::default()
        }
    }
}

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

impl From<curl::ErrCode> for ApiError {
    fn from(e: curl::ErrCode) -> ApiError {
        ApiError {
            json_err: JSONError {
                title: String::from("curl error"),
                detail: format!("{:?}", e),
            },
            ..Default::default()
        }
    }
}

#[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);
}