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
The metadata about the file format, currently the version information.
The top-level element containing a “format” structure.
The representation of a major.minor version string.
Enums
An error that occurred during the processing of the format data.
Functions
Parse the format section from a string.
Parse the format section from an arbitrary value.
The from_value_fn
parameter can be a function like e.g.
serde_json::from_value()
or serde_yaml::from_value()
.
Parse format.version from a string.
Parse format.version from an arbitrary value.
The from_value_fn
parameter can be a function like e.g.
serde_json::from_value()
or serde_yaml::from_value()
.
Type Definitions
A trivial representation of a major.minor version string.