Skip to content

Commit

Permalink
Merge branch 'master' of https:/KodrAus/elasticsearch-rs
Browse files Browse the repository at this point in the history
  • Loading branch information
KodrAus committed Mar 8, 2016
2 parents 7ab9ef5 + 31d3015 commit eb1832f
Show file tree
Hide file tree
Showing 33 changed files with 569 additions and 472 deletions.
26 changes: 18 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ The `elastic_hyper` client is a thin layer over `hyper`; it just maps functions
For serialisation though, the `elastic_macros` crate provides the `json!` macro for serialising abitrary rust-like code to json.
The deserialisation story is a work in progress.

Add `elastic_hyper` and `elastic_macros` to your `Cargo.toml`:

```
[dependencies]
elastic_hyper = "*"
elastic_macros = "*"
```

Ping the availability of your cluster:

```rust
Expand Down Expand Up @@ -86,11 +94,21 @@ Right now, it's used by `elastic_hyper` to build the client, but could also be u

### elastic_hyper

[![Latest Version](https://img.shields.io/crates/v/elastic_hyper.svg)](https://crates.io/crates/elastic_hyper)

[Docs](http://kodraus.github.io/rustdoc/elastic_hyper/)
[Issues](https:/KodrAus/elasticsearch-rs/labels/hyper)

Provides a [hyper](https:/hyperium/hyper) implementation of the Elasticsearch REST API. This is the current client that works purely through JSON. This crate is responsible for the `gen` in `elastic_codegen` and builds its own source and tests.

### elastic_macros

[![Latest Version](https://img.shields.io/crates/v/elastic_macros.svg)](https://crates.io/crates/elastic_macros)

[Docs](http://kodraus.github.io/rustdoc/elastic_macros/)

Provides compiler plugins and macros for the `elastic_types` crate, such as parsing a date format to an array of [Items](https:/lifthrasiir/rust-chrono/blob/master/src/format/mod.rs#L161) at compile-time for efficient runtime date parsing.

### elastic_types

[Docs](http://kodraus.github.io/rustdoc/elastic_types/)
Expand All @@ -99,11 +117,3 @@ Provides a [hyper](https:/hyperium/hyper) implementation of the Elas
Provides rust implementations of the main [Elasticsearch types](https://www.elastic.co/guide/en/elasticsearch/reference/1.4/mapping-core-types.html) (like `date`) and responses/errors. This crate is not required for working with `elastic_hyper`, but does have a lot of utility, especially for designing your document types.

The `elastic_types` crate tries not to reinvent the wheel wherever possible and relies on some common dependencies for types, such as [chrono](https:/lifthrasiir/rust-chrono) for dates and [rust-geo](https:/georust/rust-geo) for geometry.

### elastic_macros

[![Latest Version](https://img.shields.io/crates/v/elastic_macros.svg)](https://crates.io/crates/elastic_macros)

[Docs](http://kodraus.github.io/rustdoc/elastic_macros/)

Provides compiler plugins and macros for the `elastic_types` crate, such as parsing a date format to an array of [Items](https:/lifthrasiir/rust-chrono/blob/master/src/format/mod.rs#L161) at compile-time for efficient runtime date parsing.
6 changes: 3 additions & 3 deletions codegen/src/api/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ impl HttpVerb {
/// let verb = HttpVerb::parse("GET");
/// assert!(verb == HttpVerb::Get);
/// ```
pub fn parse(_method: &str) -> HttpVerb {
match _method {
pub fn parse(method: &str) -> HttpVerb {
match method {
"HEAD" => HttpVerb::Head,
"POST" => HttpVerb::Post,
"PUT" => HttpVerb::Put,
Expand Down Expand Up @@ -438,7 +438,7 @@ impl Param {
}

/// Get the `Type` for the `Param`.
pub fn get_type<'a>(&'a self) -> Type<'a> {
pub fn get_type(&self) -> Type {
match self._type {
Some(ref t) => Type::parse(t, &self.options),
None => Type::parse("unknown", &self.options)
Expand Down
6 changes: 3 additions & 3 deletions codegen/src/api/gen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub mod parse {
let (remainder, param) = take_while1(part_start, |c| c != b'}');

if param.len() > 0 {
parts.push(param.to_string());
parts.push(param.to_owned());
}

parse_path_param_parts(remainder, parts);
Expand All @@ -57,7 +57,7 @@ pub mod parse {
let skip_param = shift_while(remainder, |c| c != b'}');

if part.len() > 0 {
parts.push(part.to_string());
parts.push(part.to_owned());
}

if skip_param.len() != 0 {
Expand Down Expand Up @@ -107,7 +107,7 @@ pub mod parse {
let (remainder, part) = take_while1(path, |c| c != b'.');

if part.len() > 0 {
parts.push(part.to_string());
parts.push(part.to_owned());
}

if remainder.len() != 0 {
Expand Down
28 changes: 11 additions & 17 deletions codegen/src/api/gen/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,7 @@ impl error::Error for ApiGenError {
}

fn cause(&self) -> Option<&error::Error> {
match self.kind {
ApiGenErrorKind::Parse(_) => None,
ApiGenErrorKind::Other(_) => None
}
None
}
}

Expand All @@ -74,7 +71,7 @@ impl From<String> for ApiGenError {

impl api::Endpoint {
/// Gets the name of the Endpoint if it's set or returns an empty string.
pub fn get_name<'a>(&'a self) -> &'a str {
pub fn get_name(&self) -> &str {
match self.name {
Some(ref n) => n,
None => ""
Expand Down Expand Up @@ -106,21 +103,18 @@ impl api::Endpoint {
/// - `post_index_type`
///
/// This is to try and prevent collisions with the names where not a lot of info about each endpoint is available.
pub fn get_fns<'a>(&'a self) -> Result<Vec<UrlFn<'a>>, ApiGenError> {
pub fn get_fns(&self) -> Result<Vec<UrlFn>, ApiGenError> {
let mut fns = Vec::new();
for path in &self.url.paths {
//Parse the params used by this path
let mut fn_parts = BTreeMap::new();
let params = try!(parse_path_params(&path));

for param in params.iter() {
for param in &params {
let param = param.to_owned();
match self.url.parts.get(&param) {
Some(part) => {
let _ = fn_parts.insert(param, part);
},
None => ()
};
if let Some(part) = self.url.parts.get(&param) {
fn_parts.insert(param, part);
}
}

//Return a function for each method on the url
Expand Down Expand Up @@ -362,7 +356,7 @@ pub fn url_push_decl<'a, I, K>(url_base: Ident, url_parts: I, param_parts: K) ->
//Sum the url parts
let mut url_iter = url_part_ids
.iter()
.map(|&ident| ident);
.cloned();

let mut add_expr = len_add(
len_expr(ident_expr(url_base)),
Expand All @@ -375,7 +369,7 @@ pub fn url_push_decl<'a, I, K>(url_base: Ident, url_parts: I, param_parts: K) ->
//Sum the url params
let mut param_part_ids = Vec::new();
for url_param in param_parts {
param_part_ids.push(url_param.clone());
param_part_ids.push(url_param);
add_expr = len_add(add_expr, len_expr(ident_expr(url_param)));
}

Expand Down Expand Up @@ -451,8 +445,8 @@ pub fn url_push_decl<'a, I, K>(url_base: Ident, url_parts: I, param_parts: K) ->

//Thread through each url part and param, pushing a part, then a param to keep the url in order
let (mut part_iter, mut param_iter) = (
url_part_ids.iter().map(|&ident| ident),
param_part_ids.iter().map(|&ident| ident)
url_part_ids.iter().cloned(),
param_part_ids.iter().cloned()
);
let mut cont = true;
while cont {
Expand Down
4 changes: 2 additions & 2 deletions codegen/src/api/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ pub fn from_reader<R>(rdr: &mut R) -> ParseResult<Endpoint> where R: Read {
let (name, tree) = try!(
data.iter()
.next()
.ok_or("unexpected format".to_string())
.ok_or("unexpected format".to_owned())
);

//Deserialise the api ast and set the name
Expand All @@ -122,7 +122,7 @@ pub fn from_reader<R>(rdr: &mut R) -> ParseResult<Endpoint> where R: Read {

Ok(endpoint)
},
_ => Err(ParseError::from("unexpected format".to_string()))
_ => Err(ParseError::from("unexpected format".to_owned()))
}
}

Expand Down
15 changes: 6 additions & 9 deletions codegen/src/emit/default/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,13 @@ impl <'a, E> Emitter<'a> for CtxtFreeEmitter<E> where E: From<EmitError> {
type CtxtBrw = ();
type Error = E;

fn get_cx(&self) {
fn get_cx(&self) { }

}

fn emit<Emittable, EmError, W>(&self, e: &'a Emittable, writer: &'a mut W) -> Result<(), Self::Error> where
Emittable: Emit<Self::CtxtBrw, EmError>,
EmError: Into<EmitError>,
W: Write {
let cx = self.get_cx();
emit!(cx, e, writer)
#[allow(let_unit_value)]
fn emit<Emittable, EmError, W>(&self, e: &'a Emittable, writer: &'a mut W) -> Result<(), Self::Error>
where Emittable: Emit<Self::CtxtBrw, EmError>, EmError: Into<EmitError>, W: Write {
let cx = self.get_cx();
emit!(cx, e, writer)
}

fn emit_str<W>(&self, e: &str, writer: &mut W) -> Result<(), Self::Error> where W: Write {
Expand Down
2 changes: 1 addition & 1 deletion codegen/src/emit/default/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ impl Emit<(), EmitError> for AsRef<String> {

impl <'a> Emit<(), EmitError> for &'a str {
fn emit(&self, _: ()) -> Result<String, EmitError> {
Ok(self.to_string())
Ok((*self).to_owned())
}
}

Expand Down
10 changes: 5 additions & 5 deletions codegen/src/emit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ mod macros {
);

$writer.write_all(&emitted.into_bytes()[..]).map_err(|e| {
let _e: EmitError = e.into();
_e.into()
let err: EmitError = e.into();
err.into()
})
}
}
Expand All @@ -30,8 +30,8 @@ mod macros {
($emittable:ident, $writer:ident) => {
{
$writer.write_all($emittable.as_bytes()).map_err(|e| {
let _e: EmitError = e.into();
_e.into()
let err: EmitError = e.into();
err.into()
})
}
}
Expand Down Expand Up @@ -124,7 +124,7 @@ impl error::Error for EmitError {
fn description(&self) -> &str {
match self.kind {
EmitErrorKind::Io(ref err) => err.description(),
EmitErrorKind::Other(ref err) => &err[..]
EmitErrorKind::Other(ref err) => &err
}
}

Expand Down
5 changes: 2 additions & 3 deletions codegen/src/gen/rust/fun.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,8 @@ impl Fn {
self.stmts.extend(body.stmts.to_vec());

//Set the return type if the function takes one
match self.decl.output {
FunctionRetTy::Ty(_) => self.expr = body.expr.to_owned(),
_ => ()
if let FunctionRetTy::Ty(_) = self.decl.output {
self.expr = body.expr.to_owned();
}

self
Expand Down
66 changes: 33 additions & 33 deletions codegen/src/gen/rust/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,40 +12,40 @@ pub use self::fun::*;
pub use self::ty::*;

pub mod parse {
//! Rust Codegen parsers.
//! Rust Codegen parsers.

use ::parse::*;
use ::parse::*;

/// Parses a Rust path to its segments.
///
/// The path is split by '::' and each segment is added in order.
///
/// # Examples
///
/// Parse a path:
///
/// ```
/// use elastic_codegen::gen::rust::parse::parse_path;
///
/// let parsed = parse_path("crate::mod_a::mod_b::fn");
/// ```
pub fn parse_path(path: &str) -> Vec<String> {
let mut parts = Vec::new();
parse_path_parts(path.as_bytes(), &mut parts);
parts
}
/// Parses a Rust path to its segments.
///
/// The path is split by '::' and each segment is added in order.
///
/// # Examples
///
/// Parse a path:
///
/// ```
/// use elastic_codegen::gen::rust::parse::parse_path;
///
/// let parsed = parse_path("crate::mod_a::mod_b::fn");
/// ```
pub fn parse_path(path: &str) -> Vec<String> {
let mut parts = Vec::new();
parse_path_parts(path.as_bytes(), &mut parts);
parts
}

fn parse_path_parts(path: &[u8], parts: &mut Vec<String>) {
if path.len() == 0 {
return;
}
let trim_colons = shift_while(path, |c| c == b':');
let (remainder, seg) = take_while1(trim_colons, |c| c != b':');
parts.push(seg.to_string());
parse_path_parts(remainder, parts);
}
fn parse_path_parts(path: &[u8], parts: &mut Vec<String>) {
if path.len() == 0 {
return;
}
let trim_colons = shift_while(path, |c| c == b':');
let (remainder, seg) = take_while1(trim_colons, |c| c != b':');
parts.push(seg.to_owned());
parse_path_parts(remainder, parts);
}
}
14 changes: 6 additions & 8 deletions codegen/src/gen/rust/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,26 +60,24 @@ pub fn ty_ptr<T>(mutbl: Mutability, lifetime: Option<Lifetime>, opts: TyPathOpts

/// Get the full-path name of a type.
pub fn type_of<'a, T>() -> &'a str {
let t =
unsafe {
type_name::<T>()
};
t
unsafe {
type_name::<T>()
}
}

fn _type_of<T>(opts: TyPathOpts) -> String {
match opts {
TyPathOpts::Full => type_of::<T>().to_string(),
TyPathOpts::Full => type_of::<T>().to_owned(),
TyPathOpts::NameOnly => {
let mut parts = parse_path(type_of::<T>());
parts.pop().unwrap_or(String::new())
parts.pop().unwrap_or_default()
}
}
}

/// Get the full-path name of a type inferred from the argument.
pub fn infer_type_of<T>(_: &T) -> &str {
type_of::<T>()
type_of::<T>()
}

/// The kind of path to use in the type Ident.
Expand Down
Loading

1 comment on commit eb1832f

@KodrAus
Copy link
Member Author

@KodrAus KodrAus commented on eb1832f Mar 8, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#59

Please sign in to comment.