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 <w.bumiller@proxmox.com>
This commit is contained in:
Wolfgang Bumiller 2025-01-15 11:56:32 +01:00
parent c040054827
commit 2e2edcf833

View File

@ -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.