Skip to main content

Crate serde_versioned

Crate serde_versioned 

Source
Expand description

§serde_versioned

A library for handling versioned serialization and deserialization of Rust structs. This crate provides traits and derive macros to support multiple versions of data structures while maintaining backward compatibility.

§Usage

use serde_versioned::{Versioned, FromVersion};
use serde::{Serialize, Deserialize};

#[derive(Versioned, Serialize, Deserialize, Clone)]
#[versioned(versions = [UserV1, UserV2])]
struct User {
    pub name: String,
    pub age: u32,
}

#[derive(Serialize, Deserialize, Clone)]
pub struct UserV1 {
    pub name: String,
}

#[derive(Serialize, Deserialize, Clone)]
pub struct UserV2 {
    pub name: String,
    pub age: u32,
}

impl FromVersion<User> for UserV1 {
    fn convert(self) -> User {
        User { name: self.name, age: 0 }
    }
}

impl FromVersion<User> for UserV2 {
    fn convert(self) -> User {
        User { name: self.name, age: self.age }
    }
}

Structs§

VersionConversionError
Error type for version conversion operations.

Enums§

FormatError
Error type for format conversion operations.

Traits§

FromVersion
Trait for converting from a versioned struct to the current struct.
Versioned
Trait for handling versioned serialization and deserialization.

Derive Macros§

Versioned
Derives the Versioned trait for a struct.