Here's a Rust program that uses the serde and toml crates to parse the provided TOML file into a Config struct. The resulting struct is stored in a pub static Lazy cell using the once_cell crate.
First, add the necessary dependencies to your Cargo.toml file:
[dependencies]
serde = { version = "1.0", features = ["derive"] }
toml = "0.7"
once_cell = "1.17"
Now, create your main Rust file (e.g., main.rs) with the following code:
use serde::Deserialize;
use std::collections::HashMap;
use std::fs;
use once_cell::sync::Lazy;
#[derive(Debug, Deserialize)]
pub struct Config {
pub output_file: String,
pub input_dirs: Vec<String>,
pub display_text: Vec<String>,
pub duration_s: u32,
pub width: u32,
pub height: u32,
pub framerate: u32,
pub quantizer: u32,
pub speed: u32,
pub bit_depth: u32,
pub effects: HashMap<String, Effect>,
}
#[derive(Debug, Deserialize)]
pub struct Effect {
pub weight: f64,
}
pub static CONFIG: Lazy<Config> = Lazy::new(|| {
// Read the TOML configuration file
let config_str = fs::read_to_string("config.toml")
.expect("Failed to read config.toml");
// Parse the TOML string into the Config struct
toml::from_str(&config_str)
.expect("Failed to parse config.toml")
});
fn main() {
// Use the CONFIG static variable
println!("{:#?}", *CONFIG);
}
Ensure that your config.toml file has the correct content, with backslashes properly escaped:
output_file = "output2.ivf"
input_dirs = [
"G:\\example\\path",
"G:\\example\\path2"
]
display_text = [
"Example text 1",
"Example text 2"
]
duration_s = 60
width = 1920
height = 1080
framerate = 60
quantizer = 100
speed = 6
bit_depth = 8
[effects]
invert_colors = { weight = 0.3 }
color_flash = { weight = 0.2 }
sparkle = { weight = 0.2 }
spiral = { weight = 0.2 }
transparency_strobe = { weight = 0.1 }
overlay_text = { weight = 0.2 }
zoom_in = { weight = 0.1 }
kaleidoscope = { weight = 0.02 }
bit_focus = { weight = 0.4 }
glow = { weight = 0.2 }
grow_shrink = { weight = 0.3 }
fade_out = { weight = 0.2 }
split_apart = { weight = 0.2 }
Note: The glow effect appears twice in the TOML file. In TOML, keys must be unique within their table. The second occurrence will overwrite the first. Ensure that all keys are unique or consolidate the entries as needed.
This program defines a Config struct that mirrors the structure of your TOML file. It uses serde's Deserialize derive macro to automatically generate code to parse the TOML into the Config struct. The CONFIG static variable is a Lazy cell that reads and parses the TOML file upon first use.
When you run this program, it will print out the contents of the CONFIG struct, showing that it has been successfully populated from the TOML file.
Example Output:
Config {
output_file: "output2.ivf",
input_dirs: [
"G:\\example\\path",
"G:\\example\\path2",
],
display_text: [
"Example text 1",
"Example text 2",
],
duration_s: 60,
width: 1920,
height: 1080,
framerate: 60,
quantizer: 100,
speed: 6,
bit_depth: 8,
effects: {
"invert_colors": Effect {
weight: 0.3,
},
"color_flash": Effect {
weight: 0.2,
},
"sparkle": Effect {
weight: 0.2,
},
"spiral": Effect {
weight: 0.2,
},
"transparency_strobe": Effect {
weight: 0.1,
},
"overlay_text": Effect {
weight: 0.2,
},
"zoom_in": Effect {
weight: 0.1,
},
"kaleidoscope": Effect {
weight: 0.02,
},
"bit_focus": Effect {
weight: 0.4,
},
"glow": Effect {
weight: 0.2,
},
"grow_shrink": Effect {
weight: 0.3,
},
"fade_out": Effect {
weight: 0.2,
},
"split_apart": Effect {
weight: 0.2,
},
},
}
This demonstrates that the TOML file has been parsed into a Config struct, and the struct is stored in a pub static Lazy cell as requested.