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
impl ConfigMigrator
Sourcepub fn from(json: &str, migrator: Migrator) -> Result<Self, MigrationError>
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.
Sourcepub fn query<T>(&self, key: &str) -> Result<Vec<T>, MigrationError>where
T: Queryable + for<'de> Deserialize<'de>,
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 implementQueryableto provide the entity name, andDeserializefor deserialization.
§Errors
- Returns
MigrationError::DeserializationErrorif 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")?;Sourcepub fn update<T>(
&mut self,
key: &str,
data: Vec<T>,
) -> Result<(), MigrationError>
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 (automatically
determined from the Queryable trait) and updates the JSON object in place.
§Type Parameters
T: Must implementSerializeandQueryable.
§Errors
- Returns
MigrationError::EntityNotFoundif the entity is not registered. - Returns serialization errors if the data cannot be serialized.
§Example
// Version is automatically determined from the entity's migration path
config.update("tasks", updated_tasks)?;Sourcepub fn to_string(&self) -> Result<String, MigrationError>
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.
Sourcepub fn to_string_compact(&self) -> Result<String, MigrationError>
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.