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
Enums
Functions
value
parameter may be a struct similar to serde_json::Value
,
serde_yaml::Value
, or toml::Value
.value
parameter may be a struct similar to serde_json::Value
,
serde_yaml::Value
, or toml::Value
.