pub struct FomCodec { /* private fields */ }Expand description
Encodes and decodes values of the data types of one FDD.
The codec is a snapshot: it owns a resolved copy of the data type, attribute and parameter tables
and does not follow later changes of the Fdd. Build a new one after Fdd::merge; handles stay
valid because the FDD is append-only. Lookups by handle are table accesses, lookups by data type
name are one ordered map access.
See the module documentation for the mapping between data types and Values.
Implementations§
Source§impl FomCodec
impl FomCodec
Sourcepub fn new(fdd: &Fdd) -> Result<Self, CodecError>
pub fn new(fdd: &Fdd) -> Result<Self, CodecError>
Resolves the data types of fdd.
Fails with CodecError::RecursiveDataType when a data type contains itself, directly or
through other data types, and with CodecError::NestingTooDeep when data types nest deeper
than 64 levels. Data types that cannot be resolved for another reason (user-defined encodings,
missing definitions) are not errors: their values are Value::Raw, and
FomCodec::raw_reason tells why.
Sourcepub fn data_type_names(&self) -> impl Iterator<Item = &str>
pub fn data_type_names(&self) -> impl Iterator<Item = &str>
Names of all data types, in alphabetical order.
Sourcepub fn has_data_type(&self, data_type: &str) -> bool
pub fn has_data_type(&self, data_type: &str) -> bool
True if the FDD defines a data type of this name.
Sourcepub fn octet_boundary(&self, data_type: &str) -> Option<usize>
pub fn octet_boundary(&self, data_type: &str) -> Option<usize>
Octet boundary of a data type: its encoding starts at a multiple of it.
Sourcepub fn raw_reason(&self, data_type: &str) -> Option<&str>
pub fn raw_reason(&self, data_type: &str) -> Option<&str>
Why the values of a data type are Value::Raw: a user-defined encoding, an unsupported or
missing definition. None for a data type that is resolved, and for an unknown name.
Sourcepub fn attribute_data_type(&self, attribute: AttributeId) -> Option<&str>
pub fn attribute_data_type(&self, attribute: AttributeId) -> Option<&str>
Name of the data type of an attribute, as the FDD gives it.
Sourcepub fn parameter_data_type(&self, parameter: ParameterId) -> Option<&str>
pub fn parameter_data_type(&self, parameter: ParameterId) -> Option<&str>
Name of the data type of a parameter, as the FDD gives it.
Sourcepub fn encode(
&self,
data_type: &str,
value: &Value,
) -> Result<Vec<u8>, CodecError>
pub fn encode( &self, data_type: &str, value: &Value, ) -> Result<Vec<u8>, CodecError>
Encodes value as the data type called data_type.
Sourcepub fn decode(&self, data_type: &str, bytes: &[u8]) -> Result<Value, CodecError>
pub fn decode(&self, data_type: &str, bytes: &[u8]) -> Result<Value, CodecError>
Decodes bytes, which must hold exactly one value of the data type called data_type: octets
after the value are an error.
Sourcepub fn decode_prefix(
&self,
data_type: &str,
bytes: &[u8],
) -> Result<(Value, usize), CodecError>
pub fn decode_prefix( &self, data_type: &str, bytes: &[u8], ) -> Result<(Value, usize), CodecError>
Decodes one value from the start of bytes and returns it with the number of octets it used;
octets after the value are ignored.
Sourcepub fn encode_attribute(
&self,
attribute: AttributeId,
value: &Value,
) -> Result<Vec<u8>, CodecError>
pub fn encode_attribute( &self, attribute: AttributeId, value: &Value, ) -> Result<Vec<u8>, CodecError>
Encodes the value of an attribute as the data type the FDD gives it. An attribute without a
usable data type takes Value::Raw (or bytes) as it is.
Sourcepub fn decode_attribute(
&self,
attribute: AttributeId,
bytes: &[u8],
) -> Result<Value, CodecError>
pub fn decode_attribute( &self, attribute: AttributeId, bytes: &[u8], ) -> Result<Value, CodecError>
Decodes the value of an attribute; strict like FomCodec::decode. An attribute without a
usable data type gives Value::Raw.
Sourcepub fn encode_parameter(
&self,
parameter: ParameterId,
value: &Value,
) -> Result<Vec<u8>, CodecError>
pub fn encode_parameter( &self, parameter: ParameterId, value: &Value, ) -> Result<Vec<u8>, CodecError>
Encodes the value of a parameter as the data type the FDD gives it.
Sourcepub fn decode_parameter(
&self,
parameter: ParameterId,
bytes: &[u8],
) -> Result<Value, CodecError>
pub fn decode_parameter( &self, parameter: ParameterId, bytes: &[u8], ) -> Result<Value, CodecError>
Decodes the value of a parameter; strict like FomCodec::decode.
Sourcepub fn encode_attributes<I, N, V>(
&self,
class: ObjectClassId,
values: I,
) -> Result<BTreeMap<AttributeId, Vec<u8>>, CodecError>
pub fn encode_attributes<I, N, V>( &self, class: ObjectClassId, values: I, ) -> Result<BTreeMap<AttributeId, Vec<u8>>, CodecError>
Encodes attribute values given by attribute name, for example a &BTreeMap<String, Value>.
Every name must be an available attribute of class (declared or inherited).
Sourcepub fn decode_attributes<I, K, B>(
&self,
class: ObjectClassId,
values: I,
) -> Result<BTreeMap<String, Value>, CodecError>
pub fn decode_attributes<I, K, B>( &self, class: ObjectClassId, values: I, ) -> Result<BTreeMap<String, Value>, CodecError>
Decodes attribute values given by handle, for example a &BTreeMap<AttributeId, Vec<u8>>, into
a map by attribute name. Every handle must be an available attribute of class.
Sourcepub fn encode_parameters<I, N, V>(
&self,
class: InteractionClassId,
values: I,
) -> Result<BTreeMap<ParameterId, Vec<u8>>, CodecError>
pub fn encode_parameters<I, N, V>( &self, class: InteractionClassId, values: I, ) -> Result<BTreeMap<ParameterId, Vec<u8>>, CodecError>
Encodes parameter values given by parameter name, for example a &BTreeMap<String, Value>.
Every name must be an available parameter of class (declared or inherited).
Sourcepub fn decode_parameters<I, K, B>(
&self,
class: InteractionClassId,
values: I,
) -> Result<BTreeMap<String, Value>, CodecError>
pub fn decode_parameters<I, K, B>( &self, class: InteractionClassId, values: I, ) -> Result<BTreeMap<String, Value>, CodecError>
Decodes parameter values given by handle, for example a &BTreeMap<ParameterId, Vec<u8>>, into
a map by parameter name. Every handle must be an available parameter of class.