]> git.proxmox.com Git - proxmox.git/commitdiff
schema: convenience accessors to schema subtypes
authorWolfgang Bumiller <w.bumiller@proxmox.com>
Mon, 27 Feb 2023 13:05:36 +0000 (14:05 +0100)
committerThomas Lamprecht <t.lamprecht@proxmox.com>
Thu, 13 Jul 2023 14:19:05 +0000 (16:19 +0200)
Adds `const fn <type>(&self) -> Option<&<Type>Schema>` methods to
`Schema`.

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
proxmox-schema/src/schema.rs

index 2ae58c0f42c06e6dfbd5de491836891a7cf033b5..8bbadcf81e244df030caa6e48b5f30629e22dc4a 100644 (file)
@@ -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.