Crate typed_format_version

Source
Expand description

typed-format-version: load format.version.{major,minor} from a structured file.

This library tries to parse a format.version “section” in some raw data that may have been loaded from a configuration file, and determines whether that section contains valid “major” and “minor” integer values. The caller can then choose the correct schema to validate the loaded data against, e.g. by using a serde-derived library with the correct top-level dataclass definition.

The most commonly used function will probably be get_version_from_str(): it takes a string and a deserialization function and returns a Version object with a major and minor integer attributes, if the data contained a valid “format” element with a “version” element within it.

// This would usually be read from a file.
let contents = r#"{"format": {"version": {"major": 1, "minor": 3}}, "data": ["hello"]}"#;

let fver = typed_format_version::get_version_from_str(&contents, serde_json::from_str)
   .expect("Could not get the data format version");
if (fver.major(), fver.minor()) != (1, 3) {
    panic!("Unexpected data format version {}.{}", fver.major(), fver.minor());
}
// Load the data as usual using e.g. serde_json::from_str().

Examining the returned version also allows loading data in different formats using e.g. deserialization of different top-level structures and then performing some kind of data migration to the preferred one.

Structs§

Format
The metadata about the file format, currently the version information.
FormatOnlyTop
The top-level element containing a “format” structure.
Version
The representation of a major.minor version string.

Enums§

LoadError
An error that occurred during the processing of the format data.

Functions§

get_format_from_str
Parse the format section from a string.
get_format_from_value
Parse the format section from an arbitrary deserializable value. The value parameter may be a struct similar to serde_json::Value, serde_yaml::Value, or toml::Value.
get_version_from_str
Parse format.version from a string.
get_version_from_value
Parse format.version from an arbitrary value. The value parameter may be a struct similar to serde_json::Value, serde_yaml::Value, or toml::Value.

Type Aliases§

VersionTuple
A trivial representation of a major.minor version string.