[pbs-devel] [PATCH proxmox 2/2] api-macro: relax Fieldname rules

Dominik Csapak d.csapak at proxmox.com
Wed Sep 16 14:09:45 CEST 2020


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 at proxmox.com>
---
 proxmox-api-macro/src/util.rs | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/proxmox-api-macro/src/util.rs b/proxmox-api-macro/src/util.rs
index 6ac39c2..9748131 100644
--- a/proxmox-api-macro/src/util.rs
+++ b/proxmox-api-macro/src/util.rs
@@ -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),
-- 
2.20.1






More information about the pbs-devel mailing list