The goal is just to replac manual match statements with dynamic registry, that's it.
We can do this via creating a CircuitRegistry struct with HashMap<String, CircuitFactory> and use the new() method to register the circuits. Then, with getters we can take them.
some explanatory code - DO NOT USE IN PROD IS AUTOGEN
use std::collections::HashMap;
pub struct CircuitRegistry {
circuits: HashMap<String, CircuitFactory>,
}
type CircuitFactory = Box<dyn Fn(CircuitConfig) -> Box<dyn Circuit>>;
impl CircuitRegistry {
pub fn new() -> Self {
let mut registry = Self {
circuits: HashMap::new(),
};
// Register all circuits
registry.register("enc-bfv", |config| {
Box::new(enc_bfv::codegen::EncBfvCircuit::new(
config.parameter_type,
config.lambda,
))
});
registry.register("enc-trbfv", |config| {
Box::new(enc_trbfv::codegen::GrecoCircuit::new(
config.parameter_type,
config.sample_type,
config.lambda,
))
});
// ... register other circuits
registry
}
pub fn get(&self, name: &str, config: CircuitConfig) -> Result<Box<dyn Circuit>> {
let factory = self.circuits
.get(name)
.ok_or_else(|| anyhow!("Unknown circuit: {}", name))?;
Ok(factory(config))
}
}
The goal is just to replac manual match statements with dynamic registry, that's it.
We can do this via creating a
CircuitRegistrystruct withHashMap<String, CircuitFactory>and use thenew()method to register the circuits. Then, with getters we can take them.some explanatory code - DO NOT USE IN PROD IS AUTOGEN