Skip to main content

multiple_modules/
multiple_modules.rs

1/// Evaluate multiple modules with the same evaluator.
2///
3/// Shows how to reuse a single evaluator for multiple evaluations,
4/// which is more efficient than creating a new one each time.
5use pklrust::{EvaluatorManager, EvaluatorOptions, ModuleSource};
6use serde::Deserialize;
7
8#[derive(Debug, Deserialize)]
9struct DbConfig {
10    host: String,
11    port: u16,
12    name: String,
13}
14
15#[derive(Debug, Deserialize)]
16struct CacheConfig {
17    host: String,
18    port: u16,
19    ttl: i64,
20}
21
22fn main() -> Result<(), Box<dyn std::error::Error>> {
23    let mut manager = EvaluatorManager::new()?;
24    let evaluator = manager.new_evaluator(EvaluatorOptions::preconfigured())?;
25
26    // First module: database config
27    let db: DbConfig = manager.evaluate_module_typed(
28        &evaluator,
29        ModuleSource::text(
30            r#"
31            host = "db.internal"
32            port = 5432
33            name = "myapp_production"
34            "#,
35        ),
36    )?;
37    println!("Database: {db:?}");
38
39    // Second module: cache config
40    let cache: CacheConfig = manager.evaluate_module_typed(
41        &evaluator,
42        ModuleSource::text(
43            r#"
44            host = "redis.internal"
45            port = 6379
46            ttl = 3600
47            "#,
48        ),
49    )?;
50    println!("Cache: {cache:?}");
51
52    manager.close_evaluator(&evaluator)?;
53    Ok(())
54}