https://github.com/nonnorm/toml-migrate
Automatically migrate versioned config files to the latest version
https://github.com/nonnorm/toml-migrate
Last synced: 10 days ago
JSON representation
Automatically migrate versioned config files to the latest version
- Host: GitHub
- URL: https://github.com/nonnorm/toml-migrate
- Owner: nonnorm
- License: apache-2.0
- Created: 2024-10-21T02:42:59.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-05-10T14:56:01.000Z (about 1 year ago)
- Last Synced: 2025-12-13T14:44:09.101Z (6 months ago)
- Language: Rust
- Homepage:
- Size: 12.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE-APACHE
Awesome Lists containing this project
README
# toml_migrate
A crate that lets you read versioned config files and easily migrate them to the latest version.
Inspired by the [magic_migrate](https://crates.io/crates/magic_migrate) library.
## Why?
Many applications need their configuration files to evolve over time as features get added and changed. This aims to simplify the process of taking a old config file and transforming it to the latest version.
## How does it work?
For every version of your library, define a configuration struct that can be deserialized. This struct should not include the version field:
```rust
#[derive(Deserialize)]
struct ConfigV1 {
name: String,
timeout: u32,
}
#[derive(Deserialize)]
struct ConfigV2 {
name: String,
timeout: u32,
// new!
retries: u8,
}
```
Then, implement `From` for all of your config types:
```rust
impl From for ConfigV2 {
fn from(prev: ConfigV1) -> Self {
Self {
name: prev.name,
timeout: prev.timeout,
retries: 4,
}
}
}
```
Finally, use the [`build_migration_chain!`] macro to automatically implement the `Migrate` trait for all of your structs:
```rust
build_migration_chain!(ConfigV1 = 1, ConfigV2 = 2);
```
From there, you can use the [`ConfigMigrator`] to easily migrate your config file from a string:
```rust
fn read_config() -> ConfigV2 {
let config_str = r#"
version = 1
name = "MyApp"
timeout = 60
"#;
let migrator = ConfigMigrator::new("version");
let (config, _) = migrator.migrate_config(config_str).unwrap();
config
}
```