#[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).#[version_migrate(..., save = true|false)]: Controls whether to enable save functionality (default: false) Whensave = false(default), usesinto()for read-only access. Whensave = true, usesinto_with_save()to enable domain entity saving.
§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.
When save = true, the FromDomain trait is required for the save functionality.
§Examples
Basic usage (read-only, default):
ⓘ
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 (read-only, default)
#[derive(Serialize, Deserialize, VersionMigrate)]
#[version_migrate(entity = "task", latest = TaskV1_1_0)]
struct TaskEntity {
id: String,
title: String,
description: Option<String>,
}With save support:
ⓘ
// Domain entity with save support
#[derive(Serialize, Deserialize, VersionMigrate)]
#[version_migrate(entity = "task", latest = TaskV1_1_0, save = true)]
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