ConfigMigrator

Struct ConfigMigrator 

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

A wrapper around JSON data that provides convenient query and update methods for partial updates with automatic migration.

ConfigMigrator holds a JSON object and allows you to query specific keys, automatically migrating versioned data to domain entities, and update them with the latest version.

§Example

// config.json:
// {
//   "app_name": "MyApp",
//   "tasks": [
//     {"version": "1.0.0", "id": "1", "title": "Task 1"},
//     {"version": "2.0.0", "id": "2", "title": "Task 2", "description": "New"}
//   ]
// }

let config_json = fs::read_to_string("config.json")?;
let mut config = ConfigMigrator::from(&config_json, migrator)?;

// Query tasks (automatically migrates all versions to TaskEntity)
let mut tasks: Vec<TaskEntity> = config.query("tasks")?;

// Update tasks
tasks[0].title = "Updated Task".to_string();

// Save back with latest version
config.update("tasks", tasks)?;

// Write to file
fs::write("config.json", config.to_string()?)?;

Implementations§

Source§

impl ConfigMigrator

Source

pub fn from(json: &str, migrator: Migrator) -> Result<Self, MigrationError>

Creates a new ConfigMigrator from a JSON string and a Migrator.

§Errors

Returns MigrationError::DeserializationError if the JSON is invalid.

Source

pub fn query<T>(&self, key: &str) -> Result<Vec<T>, MigrationError>
where T: Queryable + for<'de> Deserialize<'de>,

Queries a specific key from the JSON object and returns the data as domain entities.

This method automatically migrates all versioned data to the latest version and converts them to domain entities.

§Type Parameters
  • T: Must implement Queryable to provide the entity name, and Deserialize for deserialization.
§Errors
  • Returns MigrationError::DeserializationError if the key doesn’t contain a valid array.
  • Returns migration errors if the data cannot be migrated.
§Example
let tasks: Vec<TaskEntity> = config.query("tasks")?;
Source

pub fn update<T>( &mut self, key: &str, data: Vec<T>, ) -> Result<(), MigrationError>

Updates a specific key in the JSON object with new domain entities.

This method serializes the entities with the latest version and updates the JSON object in place.

§Type Parameters
  • T: Must implement Queryable, Versioned, and Serialize.
§Errors
  • Returns serialization errors if the data cannot be serialized.
§Example
config.update("tasks", updated_tasks)?;
Source

pub fn to_string(&self) -> Result<String, MigrationError>

Converts the entire JSON object back to a pretty-printed string.

§Errors

Returns MigrationError::SerializationError if serialization fails.

Source

pub fn to_string_compact(&self) -> Result<String, MigrationError>

Converts the entire JSON object to a compact string.

§Errors

Returns MigrationError::SerializationError if serialization fails.

Source

pub fn as_value(&self) -> &Value

Returns a reference to the underlying JSON value.

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.