Skip to main content

deq_runtime/
controller.rs

1use crate::coordinator::CoordinatorClient;
2#[cfg(feature = "cli")]
3use crate::misc::util::help_message;
4#[cfg(feature = "cli")]
5use clap::ValueEnum;
6use serde::Serialize;
7use std::sync::Arc;
8#[cfg(feature = "cli")]
9use tonic::transport::server::Router;
10
11#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Debug)]
12#[cfg_attr(feature = "cli", derive(ValueEnum))]
13pub enum ControllerType {
14    /// do not provide controller
15    None,
16    /// a controller that runs the static reference program in the deq-bin and only takes
17    /// measurement outcomes as input during runtime
18    Static,
19    /// a JIT controller that compiles gadgets, check models, and error models on-the-fly
20    Jit,
21}
22
23pub mod static_controller;
24pub use static_controller::StaticController;
25
26pub mod jit_controller;
27pub use jit_controller::JitController;
28
29impl ControllerType {
30    pub fn create(&self, config: serde_json::Value) -> DynController {
31        match self {
32            Self::None => DynController::None,
33            Self::Static => DynController::Static(Arc::new(StaticController::new(config))),
34            Self::Jit => DynController::Jit(JitController::new(config)),
35        }
36    }
37
38    #[cfg(feature = "cli")]
39    pub fn config_help() -> String {
40        help_message::<static_controller::StaticControllerConfig>("StaticControllerConfig:")
41            + &*help_message::<jit_controller::JitControllerConfig>("JitControllerConfig:")
42    }
43
44    #[cfg(not(feature = "cli"))]
45    pub fn config_help() -> String {
46        String::new()
47    }
48}
49
50#[derive(Clone)]
51pub enum DynController {
52    None,
53    Static(Arc<StaticController>),
54    Jit(Arc<JitController>),
55}
56
57impl DynController {
58    #[cfg(feature = "cli")]
59    pub fn add_service(&self, router: Router) -> Router {
60        match self {
61            DynController::None => router,
62            DynController::Static(controller) => StaticController::add_service(controller, router),
63            DynController::Jit(controller) => JitController::add_service(controller, router),
64        }
65    }
66
67    pub async fn start(&self, coordinator: CoordinatorClient) {
68        match self {
69            DynController::None => {}
70            DynController::Static(controller) => {
71                controller.start(coordinator).await;
72            }
73            DynController::Jit(controller) => {
74                controller.start(coordinator).await;
75            }
76        }
77    }
78
79    /// Fire the JIT controller's cancellation token to abort pending
80    /// error-model loads and in-flight decodes. The static controller and
81    /// `None` variant are no-ops. Used by
82    /// [`crate::server::LocalServer::shutdown`].
83    pub async fn cancel_pending(&self) {
84        match self {
85            DynController::None => {}
86            DynController::Static(_) => {}
87            DynController::Jit(controller) => controller.cancel_pending().await,
88        }
89    }
90}
91
92/// Trait for parsing enum variants from their kebab-case string names.
93/// When the `cli` feature is enabled, this delegates to clap's `ValueEnum`.
94/// Otherwise, each enum provides its own manual implementation.
95pub trait ParseByName: Sized {
96    fn from_name(name: &str) -> Option<Self>;
97    fn variant_names() -> Vec<&'static str>;
98}