VersionMigrate

Derive Macro VersionMigrate 

Source
#[derive(VersionMigrate)]
{
    // Attributes available to this derive:
    #[version_migrate]
}
Expand description

Derives the LatestVersioned trait for a domain entity.

This macro associates a domain entity with its latest versioned representation, enabling automatic conversion and saving using the latest version.

§Attributes

  • #[version_migrate(entity = "name", latest = Type)]: Specifies the entity name and the latest versioned type (both required).

§Requirements

You must manually implement FromDomain<YourEntity> on the latest versioned type to define how to convert from the domain entity to the versioned format.

§Examples

Basic usage:

use version_migrate::{VersionMigrate, FromDomain, Versioned};
use serde::{Serialize, Deserialize};

// Latest versioned type
#[derive(Serialize, Deserialize, Versioned)]
#[versioned(version = "1.1.0")]
struct TaskV1_1_0 {
    id: String,
    title: String,
    description: Option<String>,
}

// Domain entity
#[derive(Serialize, Deserialize, VersionMigrate)]
#[version_migrate(entity = "task", latest = TaskV1_1_0)]
struct TaskEntity {
    id: String,
    title: String,
    description: Option<String>,
}

// Implement FromDomain to define the conversion
impl FromDomain<TaskEntity> for TaskV1_1_0 {
    fn from_domain(entity: TaskEntity) -> Self {
        TaskV1_1_0 {
            id: entity.id,
            title: entity.title,
            description: entity.description,
        }
    }
}

// Now you can save entities directly
let entity = TaskEntity {
    id: "1".into(),
    title: "My Task".into(),
    description: Some("Description".into()),
};
let json = migrator.save_entity(entity)?; // Automatically uses TaskV1_1_0