From 2e2edcf833bcc4ca9ec801184b50f88964875973 Mon Sep 17 00:00:00 2001 From: Wolfgang Bumiller Date: Wed, 15 Jan 2025 11:56:32 +0100 Subject: [PATCH] schema: add Schema::unwrap_*_schema_cloned This way we can copy and modify a schema. Eg. via the following ways: const FOO_SCHEMA: Schema = SOME_SCHEMA .unwrap_integer_schema_cloned() .description("Foo") .schema(); Note that for example there is currently no builder to set a `default_key` for an `ObjectSchema` back to None, so one could do: const FOO_SCHEMA: Schema = const { let mut schema = SOME_SCHEMA.unwrap_object_schema_cloned(); schema.default_key = None; schema.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 a8598a6c..3448112b 100644 --- a/proxmox-schema/src/schema.rs +++ b/proxmox-schema/src/schema.rs @@ -1456,6 +1456,70 @@ impl Schema { _ => None, } } + + /// Gets a copy of the underlying [`BooleanSchema`], panics on different schemas. + pub const fn unwrap_boolean_schema_cloned(&self) -> BooleanSchema { + match self { + Schema::Boolean(s) => BooleanSchema { ..*s }, + _ => panic!("unwrap_boolean_schema_cloned on different schema"), + } + } + + /// Gets a copy of the underlying [`IntegerSchema`], panics on different schemas. + pub const fn unwrap_integer_schema_cloned(&self) -> IntegerSchema { + match self { + Schema::Integer(s) => IntegerSchema { ..*s }, + _ => panic!("unwrap_integer_schema_cloned on different schema"), + } + } + + /// Gets a copy of the underlying [`NumberSchema`], panics on different schemas. + pub const fn unwrap_number_schema_cloned(&self) -> NumberSchema { + match self { + Schema::Number(s) => NumberSchema { ..*s }, + _ => panic!("unwrap_number_schema_cloned on different schema"), + } + } + + /// Gets the underlying [`StringSchema`], panics on different schemas. + pub const fn unwrap_string_schema_cloned(&self) -> StringSchema { + match self { + Schema::String(s) => StringSchema { ..*s }, + _ => panic!("unwrap_string_schema_cloned on different schema"), + } + } + + /// Gets the underlying [`ObjectSchema`], panics on different schemas. + pub const fn unwrap_object_schema_cloned(&self) -> ObjectSchema { + match self { + Schema::Object(s) => ObjectSchema { ..*s }, + _ => panic!("unwrap_object_schema_cloned on different schema"), + } + } + + /// Gets the underlying [`ArraySchema`], panics on different schemas. + pub const fn unwrap_array_schema_cloned(&self) -> ArraySchema { + match self { + Schema::Array(s) => ArraySchema { ..*s }, + _ => panic!("unwrap_array_schema_cloned on different schema"), + } + } + + /// Gets the underlying [`AllOfSchema`], panics on different schemas. + pub const fn unwrap_all_of_schema_cloned(&self) -> AllOfSchema { + match self { + Schema::AllOf(s) => AllOfSchema { ..*s }, + _ => panic!("unwrap_all_of_schema_cloned on different schema"), + } + } + + /// Gets the underlying [`OneOfSchema`], panics on different schemas. + pub const fn unwrap_one_of_schema_cloned(&self) -> OneOfSchema { + match self { + Schema::OneOf(s) => OneOfSchema { ..*s }, + _ => panic!("unwrap_one_of_schema_cloned on different schema"), + } + } } /// A string enum entry. An enum entry must have a value and a description.