Trait SpecializableDataSchema

Source
pub trait SpecializableDataSchema<DS, AS, OS>: BuildableDataSchema<DS, AS, OS, Extended> {
    type Stateless: BuildableDataSchema<DS, AS, OS, Extended>;
    type Array: BuildableDataSchema<DS, AS, OS, Extended>;
    type Number: BuildableDataSchema<DS, AS, OS, Extended>;
    type Integer: BuildableDataSchema<DS, AS, OS, Extended>;
    type Object: BuildableDataSchema<DS, AS, OS, Extended>;
    type String: BuildableDataSchema<DS, AS, OS, Extended>;
    type Constant: BuildableDataSchema<DS, AS, OS, Extended>;

    // Required methods
    fn array(self) -> Self::Array
       where AS: Default;
    fn array_ext<F>(self, f: F) -> Self::Array
       where F: FnOnce(<AS as Extendable>::Empty) -> AS,
             AS: Extendable;
    fn bool(self) -> Self::Stateless;
    fn number(self) -> Self::Number;
    fn integer(self) -> Self::Integer;
    fn object(self) -> Self::Object
       where OS: Default;
    fn object_ext<F>(self, f: F) -> Self::Object
       where F: FnOnce(<OS as Extendable>::Empty) -> OS,
             OS: Extendable;
    fn string(self) -> Self::String;
    fn null(self) -> Self::Stateless;
    fn constant(self, value: impl Into<Value>) -> Self::Constant;
}
Expand description

An interface for a specializable version of a DataSchema.

A meaningful DataSchema should always contain a valid subtype field, unless enumeration or one_of fields are used. This trait allows to safely transform an unspecialized DataSchema into a specialized one.

§Notes

  • This trait should not be implemented directly, even if it is not sealed.
  • This is going to break in future releases in order to have less, more expressive code.

Required Associated Types§

Source

type Stateless: BuildableDataSchema<DS, AS, OS, Extended>

A generic stateless specialized data schema builder.

Source

type Array: BuildableDataSchema<DS, AS, OS, Extended>

The array specialization of the data schema builder.

Source

type Number: BuildableDataSchema<DS, AS, OS, Extended>

The number specialization of the data schema builder.

Source

type Integer: BuildableDataSchema<DS, AS, OS, Extended>

The integer specialization of the data schema builder.

Source

type Object: BuildableDataSchema<DS, AS, OS, Extended>

The object specialization of the data schema builder.

Source

type String: BuildableDataSchema<DS, AS, OS, Extended>

The string specialization of the data schema builder.

Source

type Constant: BuildableDataSchema<DS, AS, OS, Extended>

The constant specialization of the data schema builder.

Required Methods§

Source

fn array(self) -> Self::Array
where AS: Default,

Specialize the builder into an array data schema builder, initializing the array extensions with default values.

Note that this function can only be called if AS implements Default, use array_ext otherwise.

§Examples
#[derive(Debug, Eq, PartialEq, Serialize, Deserialize)]
struct ThingExtension {}

#[derive(Debug, Default, PartialEq, Serialize, Deserialize)]
struct ArraySchemaExtension {
    array_field: u32,
}

impl ExtendableThing for ThingExtension {
    type ArraySchema = ArraySchemaExtension;
    /* Other types set to `()` */
}

let thing = Thing::builder("Thing name")
    .ext(ThingExtension {})
    .finish_extend()
    .schema_definition("test", |b| b.ext(()).finish_extend().array())
    .build()
    .unwrap();

assert_eq!(
    serde_json::to_value(thing).unwrap(),
    json!({
        "@context": "https://www.w3.org/2022/wot/td/v1.1",
        "title": "Thing name",
        "schemaDefinitions": {
            "test": {
                "type": "array",
                "array_field": 0,
                "readOnly": false,
                "writeOnly": false,
            }
        },
        "security": [],
        "securityDefinitions": {},
    })
);

The following does not work instead:

#[derive(Debug, PartialEq, Serialize, Deserialize)]
struct NotDefaultableU32(u32);

#[derive(Debug, PartialEq, Serialize, Deserialize)]
struct ArraySchemaExtension {
    array_field: NotDefaultableU32,
}

let thing = Thing::builder("Thing name")
    .ext(ThingExtension {})
    .finish_extend()
    .schema_definition("test", |b| b.ext(()).finish_extend().array())
    .build()
    .unwrap();

In this case, the following is necessary:

let thing = Thing::builder("Thing name")
    .ext(ThingExtension {})
    .finish_extend()
    .schema_definition("test", |b| {
        b.ext(()).finish_extend().array_ext(|b| {
            b.ext(ArraySchemaExtension {
                array_field: NotDefaultableU32(42),
            })
        })
    })
    .build()
    .unwrap();
Source

fn array_ext<F>(self, f: F) -> Self::Array
where F: FnOnce(<AS as Extendable>::Empty) -> AS, AS: Extendable,

Specialize the builder into an array data schema builder, passing a function to create the array extensions.

§Example
#[derive(Debug, Eq, PartialEq, Serialize, Deserialize)]
struct ThingExtension {}

#[derive(Debug, PartialEq, Serialize, Deserialize)]
struct NotDefaultableU32(u32);

#[derive(Debug, PartialEq, Serialize, Deserialize)]
struct ArraySchemaExtension {
    array_field: NotDefaultableU32,
}

impl ExtendableThing for ThingExtension {
    type ArraySchema = ArraySchemaExtension;
}

let thing = Thing::builder("Thing name")
    .ext(ThingExtension {})
    .finish_extend()
    .schema_definition("test", |b| {
        b.ext(()).finish_extend().array_ext(|b| {
            b.ext(ArraySchemaExtension {
                array_field: NotDefaultableU32(42),
            })
        })
    })
    .build()
    .unwrap();

assert_eq!(
    serde_json::to_value(thing).unwrap(),
    json!({
        "@context": "https://www.w3.org/2022/wot/td/v1.1",
        "title": "Thing name",
        "schemaDefinitions": {
            "test": {
                "type": "array",
                "array_field": 42,
                "readOnly": false,
                "writeOnly": false,
            }
        },
        "security": [],
        "securityDefinitions": {},
    })
);
Source

fn bool(self) -> Self::Stateless

Specialize the builder into a boolean data schema builder.

Source

fn number(self) -> Self::Number

Specialize the builder into a number data schema builder.

Source

fn integer(self) -> Self::Integer

Specialize the builder into an integer data schema builder.

Source

fn object(self) -> Self::Object
where OS: Default,

Specialize the builder into an object data schema builder, initializing the object extensions with default values.

Note that this function can only be called if OS implements Default, use object_ext otherwise.

§Examples
#[derive(Debug, Eq, PartialEq, Serialize, Deserialize)]
struct ThingExtension {}

#[derive(Debug, Default, PartialEq, Serialize, Deserialize)]
struct ObjectSchemaExtension {
    object_field: u32,
}

impl ExtendableThing for ThingExtension {
    type ObjectSchema = ObjectSchemaExtension;
    /* Other types set to `()` */
}

let thing = Thing::builder("Thing name")
    .ext(ThingExtension {})
    .finish_extend()
    .schema_definition("test", |b| b.ext(()).finish_extend().object())
    .build()
    .unwrap();

assert_eq!(
    serde_json::to_value(thing).unwrap(),
    json!({
        "@context": "https://www.w3.org/2022/wot/td/v1.1",
        "title": "Thing name",
        "schemaDefinitions": {
            "test": {
                "type": "object",
                "object_field": 0,
                "readOnly": false,
                "writeOnly": false,
            }
        },
        "security": [],
        "securityDefinitions": {},
    })
);

The following does not work instead:

#[derive(Debug, PartialEq, Serialize, Deserialize)]
struct NotDefaultableU32(u32);

#[derive(Debug, PartialEq, Serialize, Deserialize)]
struct ObjectSchemaExtension {
    object_field: NotDefaultableU32,
}

let thing = Thing::builder("Thing name")
    .ext(ThingExtension {})
    .finish_extend()
    .schema_definition("test", |b| b.ext(()).finish_extend().object())
    .build()
    .unwrap();

In this case, the following is necessary:

let thing = Thing::builder("Thing name")
    .ext(ThingExtension {})
    .finish_extend()
    .schema_definition("test", |b| {
        b.ext(()).finish_extend().object_ext(|b| {
            b.ext(ObjectSchemaExtension {
                object_field: NotDefaultableU32(42),
            })
        })
    })
    .build()
    .unwrap();
Source

fn object_ext<F>(self, f: F) -> Self::Object
where F: FnOnce(<OS as Extendable>::Empty) -> OS, OS: Extendable,

Specialize the builder into an object data schema builder, passing a function to create the object extensions.

§Example
#[derive(Debug, Eq, PartialEq, Serialize, Deserialize)]
struct ThingExtension {}

#[derive(Debug, PartialEq, Serialize, Deserialize)]
struct NotDefaultableU32(u32);

#[derive(Debug, PartialEq, Serialize, Deserialize)]
struct ObjectSchemaExtension {
    object_field: NotDefaultableU32,
}

impl ExtendableThing for ThingExtension {
    type ObjectSchema = ObjectSchemaExtension;
}

let thing = Thing::builder("Thing name")
    .ext(ThingExtension {})
    .finish_extend()
    .schema_definition("test", |b| {
        b.ext(()).finish_extend().object_ext(|b| {
            b.ext(ObjectSchemaExtension {
                object_field: NotDefaultableU32(42),
            })
        })
    })
    .build()
    .unwrap();

assert_eq!(
    serde_json::to_value(thing).unwrap(),
    json!({
        "@context": "https://www.w3.org/2022/wot/td/v1.1",
        "title": "Thing name",
        "schemaDefinitions": {
            "test": {
                "type": "object",
                "object_field": 42,
                "readOnly": false,
                "writeOnly": false,
            }
        },
        "security": [],
        "securityDefinitions": {},
    })
);
Source

fn string(self) -> Self::String

Specialize the builder into a string data schema builder.

Source

fn null(self) -> Self::Stateless

Specialize the builder into a null data schema builder.

Source

fn constant(self, value: impl Into<Value>) -> Self::Constant

Specialize the builder into a constant data schema builder.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<DS, AS, OS> SpecializableDataSchema<DS, AS, OS> for PartialDataSchemaBuilder<DS, AS, OS, Extended>

Source§

impl<DS, AS, OS> SpecializableDataSchema<DS, AS, OS> for DataSchemaBuilder<DS, AS, OS, Extended>

Source§

impl<Other, DataSchema, DS, AS, OS, OtherInteractionAffordance, OtherPropertyAffordance> SpecializableDataSchema<DS, AS, OS> for PropertyAffordanceBuilder<Other, DataSchema, OtherInteractionAffordance, OtherPropertyAffordance>
where Other: ExtendableThing<DataSchema = DS, ArraySchema = AS, ObjectSchema = OS>, DataSchema: SpecializableDataSchema<DS, AS, OS>,

Source§

type Stateless = PropertyAffordanceBuilder<Other, <DataSchema as SpecializableDataSchema<DS, AS, OS>>::Stateless, OtherInteractionAffordance, OtherPropertyAffordance>

Source§

type Array = PropertyAffordanceBuilder<Other, <DataSchema as SpecializableDataSchema<DS, AS, OS>>::Array, OtherInteractionAffordance, OtherPropertyAffordance>

Source§

type Number = PropertyAffordanceBuilder<Other, <DataSchema as SpecializableDataSchema<DS, AS, OS>>::Number, OtherInteractionAffordance, OtherPropertyAffordance>

Source§

type Integer = PropertyAffordanceBuilder<Other, <DataSchema as SpecializableDataSchema<DS, AS, OS>>::Integer, OtherInteractionAffordance, OtherPropertyAffordance>

Source§

type Object = PropertyAffordanceBuilder<Other, <DataSchema as SpecializableDataSchema<DS, AS, OS>>::Object, OtherInteractionAffordance, OtherPropertyAffordance>

Source§

type String = PropertyAffordanceBuilder<Other, <DataSchema as SpecializableDataSchema<DS, AS, OS>>::String, OtherInteractionAffordance, OtherPropertyAffordance>

Source§

type Constant = PropertyAffordanceBuilder<Other, <DataSchema as SpecializableDataSchema<DS, AS, OS>>::Constant, OtherInteractionAffordance, OtherPropertyAffordance>