api-macro: relax Fieldname rules

by replacing more characters ('.','+') by '_' and prefix them when
it starts with a number

we sometimes need to parse such fields, e.g in serde attributes like
 #[serde(rename = "802.3ad")]

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
This commit is contained in:
Dominik Csapak 2020-09-16 14:09:45 +02:00 committed by Dietmar Maurer
parent 29763449f0
commit 958e72aa69

View File

@ -29,7 +29,11 @@ pub struct FieldName {
impl FieldName {
pub fn new(name: String, span: Span) -> Self {
let ident_str = name.replace("-", "_");
let mut ident_str = name.replace(['-', '.', '+'].as_ref(), "_");
if ident_str.chars().next().unwrap().is_numeric() {
ident_str.insert(0, '_');
}
Self {
ident: Ident::new(&ident_str, span),