elastic / elasticsearch-rs

Official Elasticsearch Rust Client
https://www.elastic.co/guide/en/elasticsearch/client/rust-api/current/index.html
Apache License 2.0
705 stars 72 forks source link

[ENHANCEMENT] Builder fields and Parts enums accepting references #22

Closed russcam closed 4 years ago

russcam commented 4 years ago

Builder structs and their associated UrlParts enum accept all arguments as owned types. For example, for CatCount and CatCountUrlParts

#[derive(Debug, Clone, PartialEq)]
#[doc = "Url parts for the Cat Count API"]
pub enum CatCountUrlParts {
    None,
    Index(Vec<String>),
}
impl CatCountUrlParts {
    pub fn build(self) -> Cow<'static, str> {
        match self {
            CatCountUrlParts::None => "/_cat/count".into(),
            CatCountUrlParts::Index(ref index) => {
                let index_str = index.join(",");
                let mut p = String::with_capacity(12usize + index_str.len());
                p.push_str("/_cat/count/");
                p.push_str(index_str.as_ref());
                p.into()
            }
        }
    }
}
#[derive(Clone, Debug)]
#[doc = "Request builder for the Cat Count API"]
pub struct CatCount {
    client: Elasticsearch,
    parts: CatCountUrlParts,
    error_trace: Option<bool>,
    filter_path: Option<Vec<String>>,
    format: Option<String>,
    h: Option<Vec<String>>,
    help: Option<bool>,
    human: Option<bool>,
    pretty: Option<bool>,
    s: Option<Vec<String>>,
    source: Option<String>,
    v: Option<bool>,
}

Arguments that accept

This forces a consumer of the client to create types when wanting to use the API

let response = client
    .cat()
    .count(CatCountUrlParts::Index(vec!["index-1".into()]))
    .filter_path(Some(vec!["some_path".into()]))
    .send()
    .await?;

A more idiomatic way would be to allow a consumer to pass references and slices, something like the following

let response = client
    .cat()
    .count(CatCountUrlParts::Index(&["index-1"]))
    .filter_path(Some(&["some_path"]))
    .send()
    .await?;

Such a change likely requires lifetimes to be specified on the builders and enums. See the example/reference-args branch for an example with the Cat Count API.

russcam commented 4 years ago

Opened #25