From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 3AAFC79595 for ; Tue, 4 May 2021 13:18:30 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 30974887D for ; Tue, 4 May 2021 13:18:00 +0200 (CEST) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [94.136.29.106]) (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 firstgate.proxmox.com (Proxmox) with ESMTPS id 8D05E8870 for ; Tue, 4 May 2021 13:17:59 +0200 (CEST) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 6679C42A26 for ; Tue, 4 May 2021 13:17:59 +0200 (CEST) To: Proxmox Backup Server development discussion , Wolfgang Bumiller References: <20210504101930.3590-1-w.bumiller@proxmox.com> From: Dietmar Maurer Message-ID: <9e12ba36-9321-9c9a-6a71-f49e65217841@proxmox.com> Date: Tue, 4 May 2021 13:17:56 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.10.0 MIME-Version: 1.0 In-Reply-To: <20210504101930.3590-1-w.bumiller@proxmox.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Content-Language: en-US X-SPAM-LEVEL: Spam detection results: 0 AWL 0.275 Adjusted score from AWL reputation of From: address KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment NICE_REPLY_A -0.001 Looks like a legit reply (A) SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [tools.rs] Subject: Re: [pbs-devel] [RFC backup 1/2] implement an api_string_type helper macro X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 04 May 2021 11:18:30 -0000 two suggestions: 1.) Move the code to the proxmox crate 2.) Do not implement Debug (can be derived) On 5/4/21 12:19 PM, Wolfgang Bumiller wrote: > Signed-off-by: Wolfgang Bumiller > --- > src/tools.rs | 3 + > src/tools/api_string_type.rs | 105 +++++++++++++++++++++++++++++++++++ > 2 files changed, 108 insertions(+) > create mode 100644 src/tools/api_string_type.rs > > diff --git a/src/tools.rs b/src/tools.rs > index c58569a6..98597c29 100644 > --- a/src/tools.rs > +++ b/src/tools.rs > @@ -18,6 +18,9 @@ use percent_encoding::{utf8_percent_encode, AsciiSet}; > pub use proxmox::tools::fd::Fd; > use proxmox::tools::fs::{create_path, CreateOptions}; > > +#[macro_use] > +pub mod api_string_type; > + > pub mod acl; > pub mod apt; > pub mod async_io; > diff --git a/src/tools/api_string_type.rs b/src/tools/api_string_type.rs > new file mode 100644 > index 00000000..122fcbed > --- /dev/null > +++ b/src/tools/api_string_type.rs > @@ -0,0 +1,105 @@ > +/// Helper macro to generate a simple string type wrapper. > +/// > +/// This is meant to be used with an API-type tuple struct containing a single `String` like this: > +/// > +/// ```ignore > +/// api_string_type! { > +/// #[api(format: &PROXMOX_SAFE_ID_FORMAT)] > +/// /// ACME account name. > +/// #[derive(Clone, Eq, PartialEq, Hash, Deserialize, Serialize)] > +/// #[serde(transparent)] > +/// pub struct AccountName(String); > +/// } > +/// ``` > +/// > +/// This will automatically implements: > +/// * `Display` as a pass-through to `String`'s `Display` > +/// * `Debug` as a pass-through to `String`'s `Debug` > +/// * `Deref` > +/// * `DerefMut` > +/// * `AsRef` > +/// * `fn into_string(self) -> String` > +/// * `fn as_str(&self) -> &str` > +/// * `fn from_string(inner: String) -> Result` using > +/// `StringSchema::check_constraints`. > +/// * `unsafe fn from_string_unchecked(inner: String) -> Self` > +macro_rules! api_string_type { > + ( > + $(#[$doc:meta])* > + $vis:vis struct $name:ident(String); > + ) => ( > + $(#[$doc])* > + $vis struct $name(String); > + > + impl ::std::ops::Deref for $name { > + type Target = str; > + > + #[inline] > + fn deref(&self) -> &str { > + &self.0 > + } > + } > + > + impl ::std::ops::DerefMut for $name { > + #[inline] > + fn deref_mut(&mut self) -> &mut str { > + &mut self.0 > + } > + } > + > + impl AsRef for $name { > + #[inline] > + fn as_ref(&self) -> &str { > + self.0.as_ref() > + } > + } > + > + impl $name { > + /// Get the contained string. > + pub fn into_string(self) -> String { > + self.0 > + } > + > + /// Get the string as slice. > + pub fn as_str(&self) -> &str { > + self.0.as_str() > + } > + > + /// Create an instance directly from a `String`. > + /// > + /// # Safety > + /// > + /// It is the caller's job to have validated the contents. > + /// While there are no memory safety issues, a wrong string can cause API calls to > + /// fail parameter validation. > + pub unsafe fn from_string_unchecked(name: String) -> Self { > + Self(name) > + } > + > + /// Create an instance directly from a `String`, validating it using the API schema's > + /// [`check_constraints`](::proxmox::api::schema::StringSchema::check_constraints()) > + /// method. > + pub fn from_string(inner: String) -> Result { > + match &Self::API_SCHEMA { > + ::proxmox::api::schema::Schema::String(s) => s.check_constraints(&inner)?, > + _ => unreachable!(), > + } > + Ok(Self(inner)) > + } > + } > + > + impl ::std::fmt::Debug for $name { > + #[inline] > + fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { > + ::std::fmt::Debug::fmt(&self.0, f) > + } > + } > + > + impl ::std::fmt::Display for $name { > + #[inline] > + fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { > + ::std::fmt::Display::fmt(&self.0, f) > + } > + } > + ); > +}