public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Wolfgang Bumiller <w.bumiller@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [PATCH backup] turn UPID into an API type
Date: Tue, 18 Aug 2020 09:04:21 +0200	[thread overview]
Message-ID: <20200818070421.19083-1-w.bumiller@proxmox.com> (raw)

It's a string-type.
Implement Serialize via Display, Deserialize via FromStr and
add an API_SCHEMA so that it can be used as a type within
the #[api] macro.

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
---
 src/server/upid.rs | 36 +++++++++++++++++++++++-------------
 1 file changed, 23 insertions(+), 13 deletions(-)

diff --git a/src/server/upid.rs b/src/server/upid.rs
index e0d7d8af..9fc5085b 100644
--- a/src/server/upid.rs
+++ b/src/server/upid.rs
@@ -2,9 +2,9 @@ use std::sync::atomic::{AtomicUsize, Ordering};
 
 use anyhow::{bail, Error};
 use chrono::Local;
-use lazy_static::lazy_static;
-use regex::Regex;
 
+use proxmox::api::schema::{ApiStringFormat, Schema, StringSchema};
+use proxmox::const_regex;
 use proxmox::sys::linux::procfs;
 
 use crate::api2::types::Userid;
@@ -20,6 +20,7 @@ use crate::api2::types::Userid;
 /// ```
 /// Please note that we use tokio, so a single thread can run multiple
 /// tasks.
+// #[api] - manually implemented API type
 #[derive(Debug, Clone)]
 pub struct UPID {
     /// The Unix PID
@@ -40,7 +41,26 @@ pub struct UPID {
     pub node: String,
 }
 
+proxmox::forward_serialize_to_display!(UPID);
+proxmox::forward_deserialize_to_from_str!(UPID);
+
+const_regex! {
+    pub PROXMOX_UPID_REGEX = concat!(
+        r"^UPID:(?P<node>[a-zA-Z0-9]([a-zA-Z0-9\-]*[a-zA-Z0-9])?):(?P<pid>[0-9A-Fa-f]{8}):",
+        r"(?P<pstart>[0-9A-Fa-f]{8,9}):(?P<task_id>[0-9A-Fa-f]{8,16}):(?P<starttime>[0-9A-Fa-f]{8}):",
+        r"(?P<wtype>[^:\s]+):(?P<wid>[^:\s]*):(?P<userid>[^:\s]+):$"
+    );
+}
+
+pub const PROXMOX_UPID_FORMAT: ApiStringFormat =
+    ApiStringFormat::Pattern(&PROXMOX_UPID_REGEX);
+
 impl UPID {
+    pub const API_SCHEMA: Schema = StringSchema::new("Unique Process/Task Identifier")
+        .min_length("UPID:N:12345678:12345678:12345678:::".len())
+        .max_length(128) // arbitrary
+        .format(&PROXMOX_UPID_FORMAT)
+        .schema();
 
     /// Create a new UPID
     pub fn new(
@@ -92,17 +112,7 @@ impl std::str::FromStr for UPID {
     type Err = Error;
 
     fn from_str(s: &str) -> Result<Self, Self::Err> {
-
-        lazy_static! {
-            static ref REGEX: Regex = Regex::new(concat!(
-                r"^UPID:(?P<node>[a-zA-Z0-9]([a-zA-Z0-9\-]*[a-zA-Z0-9])?):(?P<pid>[0-9A-Fa-f]{8}):",
-                r"(?P<pstart>[0-9A-Fa-f]{8,9}):(?P<task_id>[0-9A-Fa-f]{8,16}):(?P<starttime>[0-9A-Fa-f]{8}):",
-                r"(?P<wtype>[^:\s]+):(?P<wid>[^:\s]*):(?P<userid>[^:\s]+):$"
-            )).unwrap();
-        }
-
-        if let Some(cap) = REGEX.captures(s) {
-
+        if let Some(cap) = PROXMOX_UPID_REGEX.captures(s) {
             Ok(UPID {
                 pid: i32::from_str_radix(&cap["pid"], 16).unwrap(),
                 pstart: u64::from_str_radix(&cap["pstart"], 16).unwrap(),
-- 
2.20.1





             reply	other threads:[~2020-08-18  7:04 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-08-18  7:04 Wolfgang Bumiller [this message]
2020-08-18  9:54 ` [pbs-devel] applied: " Dietmar Maurer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200818070421.19083-1-w.bumiller@proxmox.com \
    --to=w.bumiller@proxmox.com \
    --cc=pbs-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal