From a8bd8fca15f3c28d0e0bc96411b8be159ac522a0 Mon Sep 17 00:00:00 2001 From: Wolfgang Bumiller Date: Tue, 31 Jan 2023 13:46:37 +0100 Subject: [PATCH] schema: add basic api types feature Signed-off-by: Wolfgang Bumiller --- proxmox-schema/Cargo.toml | 1 + proxmox-schema/src/api_types.rs | 28 ++++++++++++++++++++++++++++ proxmox-schema/src/lib.rs | 4 ++++ 3 files changed, 33 insertions(+) create mode 100644 proxmox-schema/src/api_types.rs diff --git a/proxmox-schema/Cargo.toml b/proxmox-schema/Cargo.toml index e1b18b1d..de9151cf 100644 --- a/proxmox-schema/Cargo.toml +++ b/proxmox-schema/Cargo.toml @@ -33,6 +33,7 @@ default = [] api-macro = ["dep:proxmox-api-macro"] upid-api-impl = [ "dep:libc", "dep:nix" ] +api-types = [] # Testing only test-harness = [] diff --git a/proxmox-schema/src/api_types.rs b/proxmox-schema/src/api_types.rs new file mode 100644 index 00000000..ba6f02df --- /dev/null +++ b/proxmox-schema/src/api_types.rs @@ -0,0 +1,28 @@ +//! The "basic" api types we generally require along with some of their macros. + +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_regex! { + /// 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 SAFE_ID_REGEX = concat!(r"^", SAFE_ID_REGEX_STR!(), r"$"); + pub PASSWORD_REGEX = r"^[[:^cntrl:]]*$"; // everything but control characters +} + +pub const SAFE_ID_FORMAT: ApiStringFormat = ApiStringFormat::Pattern(&SAFE_ID_REGEX); +pub const PASSWORD_FORMAT: ApiStringFormat = ApiStringFormat::Pattern(&PASSWORD_REGEX); + +pub const PASSWORD_SCHEMA: Schema = StringSchema::new("Password.") + .format(&PASSWORD_FORMAT) + .min_length(1) + .max_length(1024) + .schema(); diff --git a/proxmox-schema/src/lib.rs b/proxmox-schema/src/lib.rs index 50741654..cfebed7e 100644 --- a/proxmox-schema/src/lib.rs +++ b/proxmox-schema/src/lib.rs @@ -13,6 +13,7 @@ pub use proxmox_api_macro::api; mod api_type_macros; +#[macro_use] mod const_regex; pub use const_regex::ConstRegexPattern; @@ -33,3 +34,6 @@ pub mod upid; pub mod semver_exempt { pub use lazy_static::lazy_static; } + +#[cfg(feature = "api-types")] +pub mod api_types;