From 413d631fa60f6287e5542b9f09f12793d7418845 Mon Sep 17 00:00:00 2001 From: Wolfgang Bumiller Date: Mon, 27 Feb 2023 14:05:36 +0100 Subject: [PATCH] schema: convenience accessors to schema subtypes Adds `const fn (&self) -> Option<&Schema>` methods to `Schema`. Signed-off-by: Wolfgang Bumiller --- proxmox-schema/src/schema.rs | 64 ++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/proxmox-schema/src/schema.rs b/proxmox-schema/src/schema.rs index 2ae58c0f..8bbadcf8 100644 --- a/proxmox-schema/src/schema.rs +++ b/proxmox-schema/src/schema.rs @@ -1067,6 +1067,70 @@ impl Schema { _ => panic!("unwrap_all_of_schema on different schema"), } } + + /// Gets the underlying [`BooleanSchema`]. + pub const fn boolean(&self) -> Option<&BooleanSchema> { + match self { + Schema::Boolean(s) => Some(s), + _ => None, + } + } + + /// Gets the underlying [`IntegerSchema`]. + pub const fn integer(&self) -> Option<&IntegerSchema> { + match self { + Schema::Integer(s) => Some(s), + _ => None, + } + } + + /// Gets the underlying [`NumberSchema`]. + pub const fn number(&self) -> Option<&NumberSchema> { + match self { + Schema::Number(s) => Some(s), + _ => None, + } + } + + /// Gets the underlying [`StringSchema`]. + pub const fn string(&self) -> Option<&StringSchema> { + match self { + Schema::String(s) => Some(s), + _ => None, + } + } + + /// Gets the underlying [`ObjectSchema`]. + pub const fn object(&self) -> Option<&ObjectSchema> { + match self { + Schema::Object(s) => Some(s), + _ => None, + } + } + + /// Gets the underlying [`ArraySchema`]. + pub const fn array(&self) -> Option<&ArraySchema> { + match self { + Schema::Array(s) => Some(s), + _ => None, + } + } + + /// Gets the underlying [`AllOfSchema`]. + pub const fn all_of(&self) -> Option<&AllOfSchema> { + match self { + Schema::AllOf(s) => Some(s), + _ => None, + } + } + + pub fn any_object(&self) -> Option<&dyn ObjectSchemaType> { + match self { + Schema::Object(s) => Some(s), + Schema::AllOf(s) => Some(s), + _ => None, + } + } } /// A string enum entry. An enum entry must have a value and a description.