Crates.io | serde_flow_derive |
lib.rs | serde_flow_derive |
version | 1.1.1 |
source | src |
created_at | 2024-03-07 21:09:53.41298 |
updated_at | 2024-03-14 21:34:22.89229 |
description | Simplifying migration for serde and zero-copy entities |
homepage | https://github.com/versolid/serde-flow |
repository | https://github.com/versolid/serde-flow |
max_upload_size | |
id | 1166288 |
size | 33,703 |
The library assists you in smoothly deserializing the earlier variants of your serialized information.
The Rust library that simplifies managing changes to serialized data formats during software development, enabling seamless file migration and maintaining version compatibility by supporting backward compatibility through versioning of serialized data.
Serde Flow primarily consists of three major components:
#[derive(Flow)]
: To utilize Serde Flow, you must annotate your class with serde_flow::Flow
. This annotation serves as a signal to the library that the class is eligible for data migration.#[flow(variant = N)]
: Utilize this annotation to specify the version of the entity. Simply replace N with a u16
number that represents the version. This helps in managing different versions of your data structures efficiently.
variant = N
- defines version of the struct with number(u16) Nfile(option1, option2)
or file
- implements loading from file
blocking
- (default) - normal blocking IO loading and deserializationnonblocking
- async IO loading and deserialization (it's possible to use blockin and nonblocking at the same time)verify_write
- verifies writted data by calculating checksumzerocopy
- Uses rkyv to perfome zerocopy deserialization.bytes
- Uses in memory migration without persising on the disk (just call encode()->Vec<u8>
or decode(Vec<u8>)->T
)#[variants(StructA, StructB, ...)]
(Optional): This annotation is optional but highly recommended for comprehensive data migration management. Here, you list the structs that are essential for migrating into the struct highlighted with this annotation. To ensure, you need to implement From<VariantStruct>
for all structs listed in #[variants(..)]
.[dependencies]
serde_flow = "1.1.0"
Imagine you have a User
struct that has evolved over time through versions UserV1
-> UserV2
-> User
(current), while the previous versions UserV1
and UserV2
still exist elsewhere. To manage this scenario effectively, follow these steps:
#[flow(variant = 1)]
.#[flow(variant = 2)]
for the next version.#[variants(UserV1, UserV2)]
annotation to the main User struct. It's essential to include all previous variants that you intend to migrate from.By adhering to these guidelines, you can effectively manage the evolution of your data structures while ensuring seamless migration across versions.
use serde_flow::{Flow};
use serde::{Deserialize, Serialize};
// The last (variant 3) of the User
#[derive(Serialize, Deserialize, Flow)]
#[flow(variant = 3, file)]
#[variants(UserV1, UserV2)]
pub struct User {
pub first_name: String,
pub middle_name: String,
pub last_name: String,
}
// previous variant
#[derive(Serialize, Deserialize, Flow)]
#[flow(variant = 2)]
pub struct UserV2 {
pub first_name: String,
pub last_name: String,
}
// the first variant of the User entity
#[derive(Serialize, Deserialize, Flow)]
#[flow(variant = 1)]
pub struct UserV1 {
pub name: String,
}
// Migration from UserV1 and UserV2 for User
impl From<UserV1> for User { /* migration */ }
impl From<UserV2> for User { /* migration */ }
use serde_flow::{encoder::bincode, flow::File, flow::FileMigrate, Flow};
// create an old struct UserV2
let user_v2 = UserV2 {
name: "John Adam Doe".to_string(),
};
// serialize to the disk
user_v2
.save_to_path::<bincode::Encoder>(&Path::new("/path/to/user"))
.unwrap();
// deserialize as User
let user = User::load_from_path::<bincode::Encoder>(path.as_path()).unwrap();
// Migrate and load (loads, migrates, saves, and returns new entity)
let user User::load_and_migrate::<bincode::Encoder>(path.as_path()).unwrap();
// Just migrate (loads, migrates, and saves new entity)
User::migrate::<bincode::Encoder>(path.as_path()).unwrap();
Serde-flow is open-source software, freely available under the MIT License.