/* * Licensed to Elasticsearch B.V. under one or more contributor * license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright * ownership. Elasticsearch B.V. licenses this file to you under * the Apache License, Version 2.0 (the "License"); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ use crate::generator::code_gen::stability_doc; use crate::generator::*; use inflector::Inflector; use proc_macro2::Span; use regex::Regex; pub fn generate(api: &Api) -> anyhow::Result { let mut tokens = quote!( use serde::{Serialize, Deserialize}; ); for e in &api.enums { generate_param(&mut tokens, e); } let generated = tokens.to_string(); Ok(generated) } fn generate_param(tokens: &mut TokenStream, e: &ApiEnum) { let name = syn::Ident::new(&e.name.to_pascal_case(), Span::call_site()); let (renames, variants): (Vec, Vec) = e .values .iter() .map(|v| { if v.is_empty() { ( v.to_owned(), syn::Ident::new("Unspecified", Span::call_site()), ) } else if !v.contains('(') { ( v.to_owned(), syn::Ident::new(&v.to_pascal_case(), Span::call_site()), ) } else { lazy_static! { static ref PARENS_REGEX: Regex = Regex::new(r"^(.*?)\s*\(.*?\)\s*$").unwrap(); } if let Some(c) = PARENS_REGEX.captures(v) { ( c.get(1).unwrap().as_str().to_owned(), syn::Ident::new( &c.get(1).unwrap().as_str().to_pascal_case(), Span::call_site(), ), ) } else { ( v.to_owned(), syn::Ident::new(&v.to_pascal_case(), Span::call_site()), ) } } }) .unzip(); let doc = e.description.as_ref().map(code_gen::doc_escaped); let cfg_attr = e.stability.outer_cfg_attr(); let cfg_doc = stability_doc(e.stability); let generated_enum_tokens = quote!( #doc #cfg_doc #cfg_attr #[derive(Debug, PartialEq, Eq, Deserialize, Serialize, Clone, Copy)] pub enum #name { #(#[serde(rename = #renames)] #variants),* } ); tokens.append_all(generated_enum_tokens); }