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§
Sourcetype Stateless: BuildableDataSchema<DS, AS, OS, Extended>
type Stateless: BuildableDataSchema<DS, AS, OS, Extended>
A generic stateless specialized data schema builder.
Sourcetype Array: BuildableDataSchema<DS, AS, OS, Extended>
type Array: BuildableDataSchema<DS, AS, OS, Extended>
The array specialization of the data schema builder.
Sourcetype Number: BuildableDataSchema<DS, AS, OS, Extended>
type Number: BuildableDataSchema<DS, AS, OS, Extended>
The number specialization of the data schema builder.
Sourcetype Integer: BuildableDataSchema<DS, AS, OS, Extended>
type Integer: BuildableDataSchema<DS, AS, OS, Extended>
The integer specialization of the data schema builder.
Sourcetype Object: BuildableDataSchema<DS, AS, OS, Extended>
type Object: BuildableDataSchema<DS, AS, OS, Extended>
The object specialization of the data schema builder.
Sourcetype String: BuildableDataSchema<DS, AS, OS, Extended>
type String: BuildableDataSchema<DS, AS, OS, Extended>
The string specialization of the data schema builder.
Sourcetype Constant: BuildableDataSchema<DS, AS, OS, Extended>
type Constant: BuildableDataSchema<DS, AS, OS, Extended>
The constant specialization of the data schema builder.
Required Methods§
Sourcefn array(self) -> Self::Arraywhere
AS: Default,
fn array(self) -> Self::Arraywhere
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();Sourcefn array_ext<F>(self, f: F) -> Self::Array
fn array_ext<F>(self, f: F) -> Self::Array
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": {},
})
);Sourcefn object(self) -> Self::Objectwhere
OS: Default,
fn object(self) -> Self::Objectwhere
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();Sourcefn object_ext<F>(self, f: F) -> Self::Object
fn object_ext<F>(self, f: F) -> Self::Object
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": {},
})
);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.