Migrator

Struct Migrator 

Source
pub struct Migrator { /* private fields */ }
Expand description

The migration manager that orchestrates all migrations.

Implementations§

Source§

impl Migrator

Source

pub fn new() -> Self

Creates a new, empty migrator.

Source

pub fn define(entity: &str) -> MigrationPathBuilder<Start>

Starts defining a migration path for an entity.

Source

pub fn register<D>( &mut self, path: MigrationPath<D>, ) -> Result<(), MigrationError>

Registers a migration path with validation.

This method validates the migration path before registering it:

  • Checks for circular migration paths
  • Validates version ordering follows semver rules
§Errors

Returns an error if validation fails.

Source

pub fn load_from<D, T>( &self, entity: &str, data: T, ) -> Result<D, MigrationError>

Loads and migrates data from any serde-compatible format.

This is the generic version that accepts any type implementing Serialize. For JSON strings, use the convenience method load instead.

§Arguments
  • entity - The entity name used when registering the migration path
  • data - Versioned data in any serde-compatible format (e.g., toml::Value, serde_json::Value)
§Returns

The migrated data as the domain model type

§Errors

Returns an error if:

  • The data cannot be converted to the internal format
  • The entity is not registered
  • A migration step fails
§Example
// Load from TOML
let toml_data: toml::Value = toml::from_str(toml_str)?;
let domain: TaskEntity = migrator.load_from("task", toml_data)?;

// Load from JSON Value
let json_data: serde_json::Value = serde_json::from_str(json_str)?;
let domain: TaskEntity = migrator.load_from("task", json_data)?;
Source

pub fn load<D: DeserializeOwned>( &self, entity: &str, json: &str, ) -> Result<D, MigrationError>

Loads and migrates data from a JSON string.

This is a convenience method for the common case of loading from JSON. For other formats, use load_from instead.

§Arguments
  • entity - The entity name used when registering the migration path
  • json - A JSON string containing versioned data
§Returns

The migrated data as the domain model type

§Errors

Returns an error if:

  • The JSON cannot be parsed
  • The entity is not registered
  • A migration step fails
§Example
let json = r#"{"version":"1.0.0","data":{"id":"task-1","title":"My Task"}}"#;
let domain: TaskEntity = migrator.load("task", json)?;
Source

pub fn save<T: Versioned + Serialize>( &self, data: T, ) -> Result<String, MigrationError>

Saves versioned data to a JSON string.

This method wraps the provided data with its version information and serializes it to JSON format. The resulting JSON can later be loaded and migrated using the load method.

§Arguments
  • data - The versioned data to save
§Returns

A JSON string with the format: {"version":"x.y.z","data":{...}}

§Errors

Returns SerializationError if the data cannot be serialized to JSON.

§Example
let task = TaskV1_0_0 {
    id: "task-1".to_string(),
    title: "My Task".to_string(),
};

let migrator = Migrator::new();
let json = migrator.save(task)?;
// json: {"version":"1.0.0","data":{"id":"task-1","title":"My Task"}}

Trait Implementations§

Source§

impl Default for Migrator

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.