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 111C6627EC for ; Tue, 22 Feb 2022 09:47:50 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 0BBC121F43 for ; Tue, 22 Feb 2022 09:47:20 +0100 (CET) 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 2FA2221F39 for ; Tue, 22 Feb 2022 09:47:19 +0100 (CET) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 044FD46310 for ; Tue, 22 Feb 2022 09:47:19 +0100 (CET) From: Wolfgang Bumiller To: pbs-devel@lists.proxmox.com Date: Tue, 22 Feb 2022 09:47:16 +0100 Message-Id: <20220222084717.60064-1-w.bumiller@proxmox.com> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.379 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record T_SCC_BODY_TEXT_LINE -0.01 - URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [schema.rs] Subject: [pbs-devel] [PATCH proxmox] schema: add const fn unwrap_*_schema/format methods 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, 22 Feb 2022 08:47:50 -0000 'unwrap_' because they will panic and as `const fn` since panic in const fn is now possible Note that const evaluation will only be triggered when actually used in const context, so to ensure *compile time* checks, use something like this: const FOO_SCHEMA: &AllOfSchema = SomeType::API_SCHEMA.unwrap_all_of_schema(); then_use(FOO_SCHEMA); or to use the list of enum values of an enum string type with compile time checks: const LIST: &'static [EnumEntry] = AnEnumStringType::API_SCHEMA .unwrap_string_schema() .unwrap_format() .unwrap_enum_format(); for values in LIST { ... } Signed-off-by: Wolfgang Bumiller --- While schemas are usually unlikely to change type or lose properties such as enum lists, for `ObjectSchema` and `AllOfSchema` this may actually allow catching future issues at build-time... If we want to do this, I'd prepare a similar patch set for all the `ApiHandler::...` value extractions we do in the CLI in pbs (just look at the output of `egrep -B1 -nr 'unreachable' ./src ./*/src` in pbs ;-) ) proxmox-schema/src/schema.rs | 90 ++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) diff --git a/proxmox-schema/src/schema.rs b/proxmox-schema/src/schema.rs index 58c6a82..0f90682 100644 --- a/proxmox-schema/src/schema.rs +++ b/proxmox-schema/src/schema.rs @@ -459,6 +459,14 @@ impl StringSchema { bail!("Expected string value."); } } + + /// Get the [`format`](ApiStringFormat), panics if there is no format. + pub const fn unwrap_format(&self) -> &'static ApiStringFormat { + match self.format { + Some(v) => v, + None => panic!("unwrap_format on StringSchema without format"), + } + } } /// Data type to describe array of values. @@ -950,6 +958,62 @@ impl Schema { _ => bail!("Got unexpected schema type."), } } + + /// Gets the underlying [`BooleanSchema`], panics on different schemas. + pub const fn unwrap_boolean_schema(&self) -> &BooleanSchema { + match self { + Schema::Boolean(s) => s, + _ => panic!("unwrap_boolean_schema on different schema"), + } + } + + /// Gets the underlying [`IntegerSchema`], panics on different schemas. + pub const fn unwrap_integer_schema(&self) -> &IntegerSchema { + match self { + Schema::Integer(s) => s, + _ => panic!("unwrap_integer_schema on different schema"), + } + } + + /// Gets the underlying [`NumberSchema`], panics on different schemas. + pub const fn unwrap_number_schema(&self) -> &NumberSchema { + match self { + Schema::Number(s) => s, + _ => panic!("unwrap_number_schema on different schema"), + } + } + + /// Gets the underlying [`StringSchema`], panics on different schemas. + pub const fn unwrap_string_schema(&self) -> &StringSchema { + match self { + Schema::String(s) => s, + _ => panic!("unwrap_string_schema on different schema"), + } + } + + /// Gets the underlying [`ObjectSchema`], panics on different schemas. + pub const fn unwrap_object_schema(&self) -> &ObjectSchema { + match self { + Schema::Object(s) => s, + _ => panic!("unwrap_object_schema on different schema"), + } + } + + /// Gets the underlying [`ArraySchema`], panics on different schemas. + pub const fn unwrap_array_schema(&self) -> &ArraySchema { + match self { + Schema::Array(s) => s, + _ => panic!("unwrap_array_schema on different schema"), + } + } + + /// Gets the underlying [`AllOfSchema`], panics on different schemas. + pub const fn unwrap_all_of_schema(&self) -> &AllOfSchema { + match self { + Schema::AllOf(s) => s, + _ => panic!("unwrap_all_of_schema on different schema"), + } + } } /// A string enum entry. An enum entry must have a value and a description. @@ -1047,6 +1111,32 @@ pub enum ApiStringFormat { VerifyFn(fn(&str) -> Result<(), Error>), } +impl ApiStringFormat { + /// Gets the underlying [`&[EnumEntry]`](EnumEntry) list, panics on different formats. + pub const fn unwrap_enum_format(&self) -> &'static [EnumEntry] { + match self { + ApiStringFormat::Enum(v) => v, + _ => panic!("unwrap_enum_format on a different ApiStringFormat"), + } + } + + /// Gets the underlying [`&ConstRegexPattern`](ConstRegexPattern), panics on different formats. + pub const fn unwrap_pattern_format(&self) -> &'static ConstRegexPattern { + match self { + ApiStringFormat::Pattern(v) => v, + _ => panic!("unwrap_pattern_format on a different ApiStringFormat"), + } + } + + /// Gets the underlying property [`&Schema`](Schema), panics on different formats. + pub const fn unwrap_property_string_format(&self) -> &'static Schema { + match self { + ApiStringFormat::PropertyString(v) => v, + _ => panic!("unwrap_property_string_format on a different ApiStringFormat"), + } + } +} + impl std::fmt::Debug for ApiStringFormat { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { -- 2.30.2