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
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
use http::{post, get};
use serde_json;
use serde_json::Value;
use errors::Error;
use std::collections::HashMap;
use std::collections::BTreeMap;
use hyper::Url;



pub struct Mango {
    url: Url,
}

#[derive(Serialize)]
pub struct NewIndex<'a> {
    #[serde(rename = "type")]
    json_type: &'a str,
    index: HashMap<&'a str, Vec<&'a str>>,
}

#[derive(Deserialize)]
pub struct IndexDef {
    pub fields: Vec<BTreeMap<String, String>>,
}

#[derive(Deserialize)]
pub struct IndexDoc {
    pub ddoc: Option<String>,
    pub name: String,
    #[serde(rename = "type")]
    pub field_type: String,
    pub def: IndexDef,
}

#[derive(Deserialize)]
pub struct IndexList {
    pub total_rows: u32,
    pub indexes: Vec<IndexDoc>,
}

#[derive(Deserialize)]
pub struct QueryResult {
    pub warning: Option<String>,
    pub docs: Vec<BTreeMap<String, serde_json::Value>>,
}

impl Mango {
    fn new<S>(url_str: S) -> Result<Mango, Error>
        where S: Into<String>
    {
        match Url::parse(&url_str.into()) {
            Ok(url) => Ok(Mango { url: url }),
            Err(err) => Err(Error::from(err)),
        }
    }

    pub fn create_index(&mut self, fields: &[&str]) -> Result<bool, Error> {
        let mut index_fields: HashMap<&str, Vec<&str>> = HashMap::new();
        index_fields.insert("fields", fields.to_vec());
        let index = NewIndex {
            json_type: "json",
            index: index_fields,
        };

        let body = match serde_json::to_string(&index) {
            Ok(json_str) => json_str,
            Err(err) => return Err(Error::from(err)),
        };

        println!("self.url {:?}", self.url);

        let url = try!(Url::parse(&format!("{}/_index", self.url.to_string())));
        try!(post(&url, &body));
        Ok(true)
    }

    pub fn list_indexes(&mut self) -> Result<IndexList, Error> {
        let url = try!(Url::parse(&format!("{}/_index", self.url.to_string())));
        let resp = try!(get(&url));

        match serde_json::from_str::<IndexList>(&resp) {
            Ok(ind) => Ok(ind),
            Err(err) => Err(Error::from(err)),
        }
    }

    pub fn query_index(&mut self, query: Value) -> Result<QueryResult, Error> {
        let url = try!(Url::parse(&format!("{}/_find", self.url.to_string())));

        let body = match serde_json::to_string(&query) {
            Ok(json_str) => json_str,
            Err(err) => return Err(Error::from(err)),
        };

        println!("self.url {:?} {:?}", self.url, body);
        let resp = try!(post(&url, &body));
        let result = try!(serde_json::from_str::<QueryResult>(&resp));
        Ok(result)
    }
}


/// The entry point for each Mango Smoothie request
pub fn database(url: &str) -> Result<Mango, Error> {
    Mango::new(url)
}