From ad4a9aea25e721d8c0c18faab1f2e6fa90177edb Mon Sep 17 00:00:00 2001 From: Wolfgang Bumiller Date: Wed, 7 Jul 2021 09:49:35 +0200 Subject: [PATCH] move id and single line comment format to pbs-api-types Signed-off-by: Wolfgang Bumiller --- pbs-api-types/src/lib.rs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/pbs-api-types/src/lib.rs b/pbs-api-types/src/lib.rs index 108c2ee2..125f67c8 100644 --- a/pbs-api-types/src/lib.rs +++ b/pbs-api-types/src/lib.rs @@ -3,8 +3,26 @@ use proxmox::api::schema::{ApiStringFormat, Schema, StringSchema}; use proxmox::const_regex; +#[macro_export] +macro_rules! PROXMOX_SAFE_ID_REGEX_STR { + () => { + r"(?:[A-Za-z0-9_][A-Za-z0-9._\-]*)" + }; +} + const_regex! { pub FINGERPRINT_SHA256_REGEX = r"^(?:[0-9a-fA-F][0-9a-fA-F])(?::[0-9a-fA-F][0-9a-fA-F]){31}$"; + + /// Regex for safe identifiers. + /// + /// This + /// [article](https://dwheeler.com/essays/fixing-unix-linux-filenames.html) + /// 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 PROXMOX_SAFE_ID_REGEX = concat!(r"^", PROXMOX_SAFE_ID_REGEX_STR!(), r"$"); + + pub SINGLE_LINE_COMMENT_REGEX = r"^[[:^cntrl:]]*$"; } pub const FINGERPRINT_SHA256_FORMAT: ApiStringFormat = @@ -14,3 +32,13 @@ pub const CERT_FINGERPRINT_SHA256_SCHEMA: Schema = StringSchema::new("X509 certificate fingerprint (sha256).") .format(&FINGERPRINT_SHA256_FORMAT) .schema(); + +pub const PROXMOX_SAFE_ID_FORMAT: ApiStringFormat = + ApiStringFormat::Pattern(&PROXMOX_SAFE_ID_REGEX); + +pub const SINGLE_LINE_COMMENT_FORMAT: ApiStringFormat = + ApiStringFormat::Pattern(&SINGLE_LINE_COMMENT_REGEX); + +pub const SINGLE_LINE_COMMENT_SCHEMA: Schema = StringSchema::new("Comment (single line).") + .format(&SINGLE_LINE_COMMENT_FORMAT) + .schema();