From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <dietmar@druiddev.proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by lists.proxmox.com (Postfix) with ESMTPS id 97119B9A54
 for <pbs-devel@lists.proxmox.com>; Fri, 15 Mar 2024 12:28:06 +0100 (CET)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
 by firstgate.proxmox.com (Proxmox) with ESMTP id 781D21B7D3
 for <pbs-devel@lists.proxmox.com>; Fri, 15 Mar 2024 12:27:36 +0100 (CET)
Received: from druiddev.proxmox.com (unknown [94.136.29.99])
 by firstgate.proxmox.com (Proxmox) with ESMTP
 for <pbs-devel@lists.proxmox.com>; Fri, 15 Mar 2024 12:27:34 +0100 (CET)
Received: by druiddev.proxmox.com (Postfix, from userid 1000)
 id AF4548BCC6; Fri, 15 Mar 2024 12:27:34 +0100 (CET)
From: Dietmar Maurer <dietmar@proxmox.com>
To: pbs-devel@lists.proxmox.com
Date: Fri, 15 Mar 2024 12:27:29 +0100
Message-Id: <20240315112732.368831-1-dietmar@proxmox.com>
X-Mailer: git-send-email 2.39.2
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-SPAM-LEVEL: Spam detection results:  0
 AWL -0.546 Adjusted score from AWL reputation of From: address
 BAYES_00                 -1.9 Bayes spam probability is 0 to 1%
 DMARC_MISSING             0.1 Missing DMARC policy
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 KAM_LAZY_DOMAIN_SECURITY 1 Sending domain does not have any anti-forgery
 methods
 RDNS_NONE 0.793 Delivered to internal network by a host with no rDNS
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_NONE                0.001 SPF: sender does not publish an SPF Record
 T_SCC_BODY_TEXT_LINE    -0.01 -
Subject: [pbs-devel] [PATCH proxmox 1/4] proxmox-schema: use const_format to
 define static strings.
X-BeenThere: pbs-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox Backup Server development discussion
 <pbs-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pbs-devel>, 
 <mailto:pbs-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pbs-devel/>
List-Post: <mailto:pbs-devel@lists.proxmox.com>
List-Help: <mailto:pbs-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel>, 
 <mailto:pbs-devel-request@lists.proxmox.com?subject=subscribe>
X-List-Received-Date: Fri, 15 Mar 2024 11:28:06 -0000

Macro rules are not hygienic, and current rust macro visibility rules
are a nightmare. Using const_format::concatcp!() is a much cleaner
solution.

Signed-off-by: Dietmar Maurer <dietmar@proxmox.com>
---
 Cargo.toml                      |  1 +
 proxmox-schema/Cargo.toml       |  3 ++-
 proxmox-schema/src/api_types.rs | 12 +++++++-----
 3 files changed, 10 insertions(+), 6 deletions(-)

diff --git a/Cargo.toml b/Cargo.toml
index 58d5a67..032ef65 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -55,6 +55,7 @@ base32 = "0.4"
 base64 = "0.13"
 bitflags = "1.2.1"
 bytes = "1.0"
+const_format = "0.2"
 crc32fast = "1"
 crossbeam-channel = "0.5"
 endian_trait = "0.6"
diff --git a/proxmox-schema/Cargo.toml b/proxmox-schema/Cargo.toml
index ae06818..1c150b2 100644
--- a/proxmox-schema/Cargo.toml
+++ b/proxmox-schema/Cargo.toml
@@ -11,6 +11,7 @@ exclude.workspace = true
 
 [dependencies]
 anyhow.workspace = true
+const_format = { workspace = true, optional = true }
 lazy_static.workspace = true
 regex.workspace = true
 serde.workspace = true
@@ -33,7 +34,7 @@ default = []
 
 api-macro = ["dep:proxmox-api-macro"]
 upid-api-impl = [ "dep:libc", "dep:nix" ]
-api-types = []
+api-types = [ "dep:const_format" ]
 
 # Testing only
 test-harness = []
diff --git a/proxmox-schema/src/api_types.rs b/proxmox-schema/src/api_types.rs
index 0cec043..4e10ebb 100644
--- a/proxmox-schema/src/api_types.rs
+++ b/proxmox-schema/src/api_types.rs
@@ -1,10 +1,10 @@
 //! The "basic" api types we generally require along with some of their macros.
+use const_format::concatcp;
 
 use crate::{ApiStringFormat, Schema, StringSchema};
 
 #[rustfmt::skip]
-#[macro_export]
-macro_rules! SAFE_ID_REGEX_STR { () => { r"(?:[A-Za-z0-9_][A-Za-z0-9._\-]*)" }; }
+const SAFE_ID_REGEX_STR: &str = r"(?:[A-Za-z0-9_][A-Za-z0-9._\-]*)";
 
 const_regex! {
     /// Regex for safe identifiers.
@@ -14,9 +14,11 @@ const_regex! {
     /// contains further information why it is reasonable to restict
     /// names this way. This is not only useful for filenames, but for
     /// any identifier command line tools work with.
-    pub SAFE_ID_REGEX = concat!(r"^", SAFE_ID_REGEX_STR!(), r"$");
-    pub PASSWORD_REGEX = r"^[[:^cntrl:]]*$"; // everything but control characters
-    pub SINGLE_LINE_COMMENT_REGEX = r"^[[:^cntrl:]]*$"; // everything but control characters
+    pub SAFE_ID_REGEX = concatcp!(r"^", SAFE_ID_REGEX_STR, r"$");
+    /// Password. Allow everything but control characters.
+    pub PASSWORD_REGEX = r"^[[:^cntrl:]]*$";
+    /// Single line comment. Allow everything but control characters.
+    pub SINGLE_LINE_COMMENT_REGEX = r"^[[:^cntrl:]]*$";
 }
 
 pub const SAFE_ID_FORMAT: ApiStringFormat = ApiStringFormat::Pattern(&SAFE_ID_REGEX);
-- 
2.39.2