Expand description
Deserialization of a Value<T>
type which tracks where it was deserialized from.
§Rationale for Value<T>
Often Cargo wants to report semantic error information or other sorts of
error information about configuration keys but it also may wish to indicate
as an error context where the key was defined as well (to help user
debugging). The Value<T>
type here can be used to deserialize a T
value
from configuration, but also record where it was deserialized from when it
was read.
Deserializing Value<T>
is pretty special, and serde doesn’t have built-in
support for this operation. To implement this we extend serde’s “data model”
a bit. We configure deserialization of Value<T>
to basically only work with
our one deserializer using configuration.
§How Value<T>
deserialization works
Value<T>
uses a custom protocol to inject source location information
into serde’s deserialization process:
Magic identifiers: Value<T>::deserialize
requests a struct with special
name and field names that use invalid Rust syntax to avoid
conflicts. This signals to Cargo’s deserializer that location tracking is needed.
Custom deserializer response: When Cargo’s deserializer sees these magic
identifiers, it switches to ValueDeserializer
(from the de
module)
instead of normal struct deserialization.
Two-field protocol: ValueDeserializer
presents exactly two fields
through map visiting:
- The actual value (deserialized normally)
- The definition context (encoded as a
(u32, String)
tuple acting as a tagged union ofDefinition
variants)
This allows Value<T>
to capture both the deserialized data and where it
came from.
Note: When modifying Definition
variants, be sure to update both
the Definition::deserialize
implementation here and the
MapAccess::next_value_seed
implementation in ValueDeserializer
.
Structs§
- Definition
Key 🔒 - Field
Visitor 🔒 - Value
- A type which can be deserialized as a configuration value which records where it was deserialized from.
- Value
Key 🔒
Enums§
- Definition
- Location where a config value is defined.
Constants§
- DEFINITION_
FIELD 🔒 - NAME 🔒
- VALUE_
FIELD 🔒
Statics§
- FIELDS 🔒