Expand description
Typed, lossy-aware interface translation between API versions.
This crate models the migration of data structures between versioned API
schemas. Each translation carries a Diff and is parameterised by a
Lossiness marker that distinguishes whether the conversion is
information-preserving (Lossless) or destructive (Lossy).
§Quick start
use interface::{Diff, Lossless, Lossy, Translation, Upgrade, Downgrade};
#[derive(Debug, PartialEq, Eq)]
struct UserV1 { name: String }
#[derive(Debug, PartialEq, Eq)]
struct UserV2 { name: String, email: String }
impl Upgrade<UserV2> for UserV1 {
type Lossiness = Lossless;
fn upgrade(self) -> Translation<Self, UserV2, Lossless> {
let diff = Diff::new().add("email", "default@example.com");
Translation::new(self, Box::new(|s| UserV2 {
name: s.name,
email: "default@example.com".into(),
}), diff)
}
}
impl Downgrade<UserV1> for UserV2 {
type Lossiness = Lossy;
fn downgrade(self) -> Translation<Self, UserV1, Lossy> {
let diff = Diff::new().sub("email", &self.email);
Translation::new(self, Box::new(|s| UserV1 { name: s.name }), diff)
}
}
let t = UserV1 { name: "Alice".into() }.upgrade();
assert!(!t.is_lossy());
let v2 = t.translate();
assert_eq!(v2.email, "default@example.com");
let t = v2.downgrade();
assert!(t.is_lossy());
let v1 = t.translate_lossy();
assert_eq!(v1.name, "Alice");Structs§
- Diff
- A human-readable record of fields added or removed during a translation.
- Lossless
- Marker: the translation preserves all information from the source.
- Lossy
- Marker: the translation drops information present in the source.
- Translation
- A pending translation from
SourcetoTarget.