Module boolean

Source
Expand description

Implementation of the boolean schema.

By default BooleanSchema encode the Boolean values true and false to one byte. However, internally it only targets a single bit. This allows BDS to recognize BooleanSchema as bitfields.

§Parameters

KeyTypeDefaultComment
"bitoffset"uint0Number of bits the bitfield is shifted
"length"uint1Number of bytes the field encodes

Note: If the BooleanSchema is part of a merged bitfield its length must match the other bitfields.

§Validation

Like for every other bitfield the "bitoffset" must not exceed the field’s length, i.e.:

"bitoffset" + 1 <= "length" * 8

Note: The width ("bits") of a BooleanSchema is always 1.

§Example

All parameters of BooleanSchema has default values. Accordingly, an empty schema is valid.

let schema = json!({ "type": "boolean" });

let mut scope = json_schema::Scope::new();
let j_schema = scope.compile_and_return(schema.clone(), false)?;
let schema = from_value::<DataSchema>(schema)?;

let value = json!(true);
assert!(j_schema.validate(&value).is_valid());
let mut encoded = Vec::new();
schema.encode(&mut encoded, &value)?;

let mut encoded = std::io::Cursor::new(encoded);
let back = schema.decode(&mut encoded)?;
assert!(j_schema.validate(&back).is_valid());
assert_eq!(back, value);

Structs§

BooleanSchema
The Boolean schema describes a Boolean value (further information on the module’s documentation).

Enums§

DecodingError
Errors decoding a string with a BooleanSchema.
EncodingError
Errors encoding a string with a BooleanSchema.
ValidationError
Errors validating a BooleanSchema.

Constants§

DEFAULT_LENGTH
Default length of boolean schemata.