knusbaum_consul/
lib.rs

1//! Rust client libray for [Consul](http://consul.io/) HTTP API
2//!
3//! # Usage
4//!
5//! This crate is [on crates.io](https://crates.io/crates/consul) and
6//! can be used by adding `consul` to the dependencies in your
7//! project's `Cargo.toml`.
8//!
9//! ```toml
10//! [dependencies]
11//! consul = "*"
12//! ```
13//!
14//! and this to your crate root:
15//!
16//! ```rust
17//! extern crate consul;
18//! ```
19//! # Examples
20//! ```rust
21//! use std::collections::HashMap;
22//! use consul::{Client, Service};
23//!
24//! let client = Client::new("http://127.0.0.1:8500");
25//! let services: HashMap<String, Service> = client.agent.services().unwrap();
26//! println!("{:?}", services);
27//! ```
28//!
29
30#![crate_name = "knusbaum_consul"]
31#![crate_type = "lib"]
32
33#[macro_use]
34extern crate serde_derive;
35extern crate serde;
36extern crate serde_json;
37extern crate reqwest;
38
39/// public api
40pub use agent::{Agent, AgentMember};
41pub use catalog::{Catalog, ServiceNode};
42pub use health::Health;
43pub use client::Client;
44pub use keystore::Keystore;
45pub use session::Session;
46pub use structs::{Node, Service, HealthService, RegisterService, TtlHealthCheck};
47
48mod agent;
49mod catalog;
50mod structs;
51mod health;
52mod client;
53mod session;
54mod keystore;
55mod request;
56mod error;
57
58use serde_json::Value;
59pub use error::ConsulResult;
60
61#[inline]
62pub fn get_string(json_data: &Value, path: &[&str]) -> Option<String> {
63    let mut pointer_str = String::new();
64    for entry in path.iter() {
65        pointer_str = format!("{}/{}", pointer_str, entry);
66    }
67
68    json_data.pointer(&pointer_str)
69        .and_then(|value| {
70            value.as_str()
71                .and_then(|value| Some(value.to_owned()))
72        })
73}
74
75#[inline]
76pub fn find_path<'a>(json_data: &'a Value, path: &[&str]) -> Option<&'a Value> {
77    let mut pointer_str = String::new();
78    for entry in path.iter() {
79        pointer_str = format!("{}/{}", pointer_str, entry);
80    }
81
82    json_data.pointer(&pointer_str)
83}