in api_generator/src/generator/code_gen/url/enum_builder.rs [320:442]
fn generate_parts_enum_from_endpoint() {
let endpoint = (
"search".to_string(),
ApiEndpoint {
full_name: Some("search".to_string()),
documentation: Documentation {
description: None,
url: None,
},
stability: Stability::Stable,
deprecated: None,
url: Url {
paths: vec![
Path {
path: PathString("/_search".to_string()),
methods: vec![HttpMethod::Get, HttpMethod::Post],
parts: BTreeMap::new(),
deprecated: None,
},
Path {
path: PathString("/{index}/_search".to_string()),
methods: vec![HttpMethod::Get, HttpMethod::Post],
parts: {
let mut map = BTreeMap::new();
map.insert("index".to_string(), Type {
ty: TypeKind::List,
description: Some("A comma-separated list of document types to search".to_string()),
options: vec![],
default: None,
});
map
},
deprecated: None,
},
Path {
path: PathString("/{index}/{type}/_search".to_string()),
methods: vec![HttpMethod::Get, HttpMethod::Post],
parts: {
let mut map = BTreeMap::new();
map.insert("index".to_string(), Type {
ty: TypeKind::List,
description: Some("A comma-separated list of index names to search".to_string()),
options: vec![],
default: None,
});
map.insert("type".to_string(), Type {
ty: TypeKind::List,
description: Some("A comma-separated list of document types to search".to_string()),
options: vec![],
default: None,
});
map
},
deprecated: Some(Deprecated {
version: "7.0.0".to_string(),
description: "types are going away".to_string()
}),
},
],
},
params: BTreeMap::new(),
body: Some(Body {
description: Some("The search request".to_string()),
required: Some(false),
serialize: None
}),
},
);
let (enum_ty, enum_decl, enum_impl) = EnumBuilder::from(&endpoint).build();
assert_eq!(ty_b("SearchParts"), enum_ty);
let expected_decl = quote!(
#[derive(Debug, Clone, PartialEq)]
#[doc = "API parts for the Search API"]
pub enum SearchParts<'b> {
#[doc = "No parts"]
None,
#[doc = "Index"]
Index(&'b [&'b str]),
#[doc = "Index and Type"]
IndexType(&'b [&'b str], &'b [&'b str]),
}
);
ast_eq(expected_decl, enum_decl);
let expected_impl = quote!(
impl<'b> SearchParts<'b> {
#[doc = "Builds a relative URL path to the Search API"]
pub fn url(self) -> Cow<'static, str> {
match self {
SearchParts::None => "/_search".into(),
SearchParts::Index(ref index) => {
let index_str = index.join(",");
let encoded_index: Cow<str> = percent_encode(index_str.as_bytes(), PARTS_ENCODED).into();
let mut p = String::with_capacity(9usize + encoded_index.len());
p.push_str("/");
p.push_str(encoded_index.as_ref());
p.push_str("/_search");
p.into()
}
SearchParts::IndexType(ref index, ref ty) => {
let index_str = index.join(",");
let ty_str = ty.join(",");
let encoded_index: Cow<str> = percent_encode(index_str.as_bytes(), PARTS_ENCODED).into();
let encoded_ty: Cow<str> = percent_encode(ty_str.as_bytes(), PARTS_ENCODED).into();
let mut p = String::with_capacity(10usize + encoded_index.len() + encoded_ty.len());
p.push_str("/");
p.push_str(encoded_index.as_ref());
p.push_str("/");
p.push_str(encoded_ty.as_ref());
p.push_str("/_search");
p.into()
}
}
}
}
);
ast_eq(expected_impl, enum_impl);
}