Skip to content

Commit eb1832f

Browse files
committed
2 parents 7ab9ef5 + 31d3015 commit eb1832f

33 files changed

+569
-472
lines changed

README.md

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ The `elastic_hyper` client is a thin layer over `hyper`; it just maps functions
1616
For serialisation though, the `elastic_macros` crate provides the `json!` macro for serialising abitrary rust-like code to json.
1717
The deserialisation story is a work in progress.
1818

19+
Add `elastic_hyper` and `elastic_macros` to your `Cargo.toml`:
20+
21+
```
22+
[dependencies]
23+
elastic_hyper = "*"
24+
elastic_macros = "*"
25+
```
26+
1927
Ping the availability of your cluster:
2028

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

8795
### elastic_hyper
8896

97+
[![Latest Version](https://img.shields.io/crates/v/elastic_hyper.svg)](https://crates.io/crates/elastic_hyper)
98+
8999
[Docs](http://kodraus.github.io/rustdoc/elastic_hyper/)
90100
[Issues](https://github.com/KodrAus/elasticsearch-rs/labels/hyper)
91101

92102
Provides a [hyper](https://github.com/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.
93103

104+
### elastic_macros
105+
106+
[![Latest Version](https://img.shields.io/crates/v/elastic_macros.svg)](https://crates.io/crates/elastic_macros)
107+
108+
[Docs](http://kodraus.github.io/rustdoc/elastic_macros/)
109+
110+
Provides compiler plugins and macros for the `elastic_types` crate, such as parsing a date format to an array of [Items](https://github.com/lifthrasiir/rust-chrono/blob/master/src/format/mod.rs#L161) at compile-time for efficient runtime date parsing.
111+
94112
### elastic_types
95113

96114
[Docs](http://kodraus.github.io/rustdoc/elastic_types/)
@@ -99,11 +117,3 @@ Provides a [hyper](https://github.com/hyperium/hyper) implementation of the Elas
99117
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.
100118

101119
The `elastic_types` crate tries not to reinvent the wheel wherever possible and relies on some common dependencies for types, such as [chrono](https://github.com/lifthrasiir/rust-chrono) for dates and [rust-geo](https://github.com/georust/rust-geo) for geometry.
102-
103-
### elastic_macros
104-
105-
[![Latest Version](https://img.shields.io/crates/v/elastic_macros.svg)](https://crates.io/crates/elastic_macros)
106-
107-
[Docs](http://kodraus.github.io/rustdoc/elastic_macros/)
108-
109-
Provides compiler plugins and macros for the `elastic_types` crate, such as parsing a date format to an array of [Items](https://github.com/lifthrasiir/rust-chrono/blob/master/src/format/mod.rs#L161) at compile-time for efficient runtime date parsing.

codegen/src/api/ast.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ impl HttpVerb {
6060
/// let verb = HttpVerb::parse("GET");
6161
/// assert!(verb == HttpVerb::Get);
6262
/// ```
63-
pub fn parse(_method: &str) -> HttpVerb {
64-
match _method {
63+
pub fn parse(method: &str) -> HttpVerb {
64+
match method {
6565
"HEAD" => HttpVerb::Head,
6666
"POST" => HttpVerb::Post,
6767
"PUT" => HttpVerb::Put,
@@ -438,7 +438,7 @@ impl Param {
438438
}
439439

440440
/// Get the `Type` for the `Param`.
441-
pub fn get_type<'a>(&'a self) -> Type<'a> {
441+
pub fn get_type(&self) -> Type {
442442
match self._type {
443443
Some(ref t) => Type::parse(t, &self.options),
444444
None => Type::parse("unknown", &self.options)

codegen/src/api/gen/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub mod parse {
3333
let (remainder, param) = take_while1(part_start, |c| c != b'}');
3434

3535
if param.len() > 0 {
36-
parts.push(param.to_string());
36+
parts.push(param.to_owned());
3737
}
3838

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

5959
if part.len() > 0 {
60-
parts.push(part.to_string());
60+
parts.push(part.to_owned());
6161
}
6262

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

109109
if part.len() > 0 {
110-
parts.push(part.to_string());
110+
parts.push(part.to_owned());
111111
}
112112

113113
if remainder.len() != 0 {

codegen/src/api/gen/rust.rs

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,7 @@ impl error::Error for ApiGenError {
5757
}
5858

5959
fn cause(&self) -> Option<&error::Error> {
60-
match self.kind {
61-
ApiGenErrorKind::Parse(_) => None,
62-
ApiGenErrorKind::Other(_) => None
63-
}
60+
None
6461
}
6562
}
6663

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

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

116-
for param in params.iter() {
113+
for param in &params {
117114
let param = param.to_owned();
118-
match self.url.parts.get(&param) {
119-
Some(part) => {
120-
let _ = fn_parts.insert(param, part);
121-
},
122-
None => ()
123-
};
115+
if let Some(part) = self.url.parts.get(&param) {
116+
fn_parts.insert(param, part);
117+
}
124118
}
125119

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

367361
let mut add_expr = len_add(
368362
len_expr(ident_expr(url_base)),
@@ -375,7 +369,7 @@ pub fn url_push_decl<'a, I, K>(url_base: Ident, url_parts: I, param_parts: K) ->
375369
//Sum the url params
376370
let mut param_part_ids = Vec::new();
377371
for url_param in param_parts {
378-
param_part_ids.push(url_param.clone());
372+
param_part_ids.push(url_param);
379373
add_expr = len_add(add_expr, len_expr(ident_expr(url_param)));
380374
}
381375

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

452446
//Thread through each url part and param, pushing a part, then a param to keep the url in order
453447
let (mut part_iter, mut param_iter) = (
454-
url_part_ids.iter().map(|&ident| ident),
455-
param_part_ids.iter().map(|&ident| ident)
448+
url_part_ids.iter().cloned(),
449+
param_part_ids.iter().cloned()
456450
);
457451
let mut cont = true;
458452
while cont {

codegen/src/api/parse.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ pub fn from_reader<R>(rdr: &mut R) -> ParseResult<Endpoint> where R: Read {
113113
let (name, tree) = try!(
114114
data.iter()
115115
.next()
116-
.ok_or("unexpected format".to_string())
116+
.ok_or("unexpected format".to_owned())
117117
);
118118

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

123123
Ok(endpoint)
124124
},
125-
_ => Err(ParseError::from("unexpected format".to_string()))
125+
_ => Err(ParseError::from("unexpected format".to_owned()))
126126
}
127127
}
128128

codegen/src/emit/default/mod.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,13 @@ impl <'a, E> Emitter<'a> for CtxtFreeEmitter<E> where E: From<EmitError> {
6666
type CtxtBrw = ();
6767
type Error = E;
6868

69-
fn get_cx(&self) {
69+
fn get_cx(&self) { }
7070

71-
}
72-
73-
fn emit<Emittable, EmError, W>(&self, e: &'a Emittable, writer: &'a mut W) -> Result<(), Self::Error> where
74-
Emittable: Emit<Self::CtxtBrw, EmError>,
75-
EmError: Into<EmitError>,
76-
W: Write {
77-
let cx = self.get_cx();
78-
emit!(cx, e, writer)
71+
#[allow(let_unit_value)]
72+
fn emit<Emittable, EmError, W>(&self, e: &'a Emittable, writer: &'a mut W) -> Result<(), Self::Error>
73+
where Emittable: Emit<Self::CtxtBrw, EmError>, EmError: Into<EmitError>, W: Write {
74+
let cx = self.get_cx();
75+
emit!(cx, e, writer)
7976
}
8077

8178
fn emit_str<W>(&self, e: &str, writer: &mut W) -> Result<(), Self::Error> where W: Write {

codegen/src/emit/default/string.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ impl Emit<(), EmitError> for AsRef<String> {
1010

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

codegen/src/emit/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ mod macros {
1818
);
1919

2020
$writer.write_all(&emitted.into_bytes()[..]).map_err(|e| {
21-
let _e: EmitError = e.into();
22-
_e.into()
21+
let err: EmitError = e.into();
22+
err.into()
2323
})
2424
}
2525
}
@@ -30,8 +30,8 @@ mod macros {
3030
($emittable:ident, $writer:ident) => {
3131
{
3232
$writer.write_all($emittable.as_bytes()).map_err(|e| {
33-
let _e: EmitError = e.into();
34-
_e.into()
33+
let err: EmitError = e.into();
34+
err.into()
3535
})
3636
}
3737
}
@@ -124,7 +124,7 @@ impl error::Error for EmitError {
124124
fn description(&self) -> &str {
125125
match self.kind {
126126
EmitErrorKind::Io(ref err) => err.description(),
127-
EmitErrorKind::Other(ref err) => &err[..]
127+
EmitErrorKind::Other(ref err) => &err
128128
}
129129
}
130130

codegen/src/gen/rust/fun.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,8 @@ impl Fn {
8383
self.stmts.extend(body.stmts.to_vec());
8484

8585
//Set the return type if the function takes one
86-
match self.decl.output {
87-
FunctionRetTy::Ty(_) => self.expr = body.expr.to_owned(),
88-
_ => ()
86+
if let FunctionRetTy::Ty(_) = self.decl.output {
87+
self.expr = body.expr.to_owned();
8988
}
9089

9190
self

codegen/src/gen/rust/mod.rs

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,40 +12,40 @@ pub use self::fun::*;
1212
pub use self::ty::*;
1313

1414
pub mod parse {
15-
//! Rust Codegen parsers.
15+
//! Rust Codegen parsers.
1616
17-
use ::parse::*;
17+
use ::parse::*;
1818

19-
/// Parses a Rust path to its segments.
20-
///
21-
/// The path is split by '::' and each segment is added in order.
22-
///
23-
/// # Examples
24-
///
25-
/// Parse a path:
26-
///
27-
/// ```
28-
/// use elastic_codegen::gen::rust::parse::parse_path;
29-
///
30-
/// let parsed = parse_path("crate::mod_a::mod_b::fn");
31-
/// ```
32-
pub fn parse_path(path: &str) -> Vec<String> {
33-
let mut parts = Vec::new();
34-
parse_path_parts(path.as_bytes(), &mut parts);
35-
36-
parts
37-
}
19+
/// Parses a Rust path to its segments.
20+
///
21+
/// The path is split by '::' and each segment is added in order.
22+
///
23+
/// # Examples
24+
///
25+
/// Parse a path:
26+
///
27+
/// ```
28+
/// use elastic_codegen::gen::rust::parse::parse_path;
29+
///
30+
/// let parsed = parse_path("crate::mod_a::mod_b::fn");
31+
/// ```
32+
pub fn parse_path(path: &str) -> Vec<String> {
33+
let mut parts = Vec::new();
34+
parse_path_parts(path.as_bytes(), &mut parts);
35+
36+
parts
37+
}
3838

39-
fn parse_path_parts(path: &[u8], parts: &mut Vec<String>) {
40-
if path.len() == 0 {
41-
return;
42-
}
43-
44-
let trim_colons = shift_while(path, |c| c == b':');
45-
let (remainder, seg) = take_while1(trim_colons, |c| c != b':');
46-
47-
parts.push(seg.to_string());
48-
49-
parse_path_parts(remainder, parts);
50-
}
39+
fn parse_path_parts(path: &[u8], parts: &mut Vec<String>) {
40+
if path.len() == 0 {
41+
return;
42+
}
43+
44+
let trim_colons = shift_while(path, |c| c == b':');
45+
let (remainder, seg) = take_while1(trim_colons, |c| c != b':');
46+
47+
parts.push(seg.to_owned());
48+
49+
parse_path_parts(remainder, parts);
50+
}
5151
}

codegen/src/gen/rust/ty.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,26 +60,24 @@ pub fn ty_ptr<T>(mutbl: Mutability, lifetime: Option<Lifetime>, opts: TyPathOpts
6060

6161
/// Get the full-path name of a type.
6262
pub fn type_of<'a, T>() -> &'a str {
63-
let t =
64-
unsafe {
65-
type_name::<T>()
66-
};
67-
t
63+
unsafe {
64+
type_name::<T>()
65+
}
6866
}
6967

7068
fn _type_of<T>(opts: TyPathOpts) -> String {
7169
match opts {
72-
TyPathOpts::Full => type_of::<T>().to_string(),
70+
TyPathOpts::Full => type_of::<T>().to_owned(),
7371
TyPathOpts::NameOnly => {
7472
let mut parts = parse_path(type_of::<T>());
75-
parts.pop().unwrap_or(String::new())
73+
parts.pop().unwrap_or_default()
7674
}
7775
}
7876
}
7977

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

8583
/// The kind of path to use in the type Ident.

0 commit comments

Comments
 (0)