use super::types::*;
use crate::types::CallState;
pub struct StateTableBuilder {
table: MasterStateTable,
}
impl StateTableBuilder {
pub fn new() -> Self {
Self {
table: MasterStateTable::new(),
}
}
pub fn from_yaml() -> crate::errors::Result<Self> {
let table = super::yaml_loader::YamlTableLoader::load_default()?;
Ok(Self { table })
}
pub fn from_yaml_file(path: &str) -> crate::errors::Result<Self> {
let table = super::yaml_loader::YamlTableLoader::load_from_file(path)?;
Ok(Self { table })
}
pub fn add_transition(
&mut self,
role: Role,
state: CallState,
event: EventType,
transition: Transition,
) -> &mut Self {
let key = StateKey { role, state, event };
self.table.insert(key, transition);
self
}
pub fn add_raw_transition(&mut self, key: StateKey, transition: Transition) {
self.table.insert(key, transition);
}
pub fn add_wildcard_transition(
&mut self,
role: Role,
event: EventType,
transition: Transition,
) {
self.table.insert_wildcard(role, event, transition);
}
pub fn add_state_change(
&mut self,
role: Role,
from_state: CallState,
event: EventType,
to_state: CallState,
) -> &mut Self {
self.add_transition(
role,
from_state,
event,
Transition {
guards: vec![],
actions: vec![],
next_state: Some(to_state),
condition_updates: ConditionUpdates::none(),
publish_events: vec![EventTemplate::StateChanged],
},
)
}
pub fn build(self) -> MasterStateTable {
self.table
}
}
impl StateTableBuilder {
pub fn add_condition_setter(
&mut self,
role: Role,
state: CallState,
event: EventType,
condition: Condition,
value: bool,
) -> &mut Self {
let condition_updates = match condition {
Condition::DialogEstablished => ConditionUpdates::set_dialog_established(value),
Condition::MediaSessionReady => ConditionUpdates::set_media_ready(value),
Condition::SDPNegotiated => ConditionUpdates::set_sdp_negotiated(value),
};
self.add_transition(
role,
state,
event,
Transition {
guards: vec![],
actions: vec![Action::SetCondition(condition, value)],
next_state: None,
condition_updates,
publish_events: vec![],
},
)
}
pub fn add_media_flow_publisher(
&mut self,
role: Role,
state: CallState,
event: EventType,
guards: Vec<Guard>,
) -> &mut Self {
self.add_transition(
role,
state,
event,
Transition {
guards,
actions: vec![],
next_state: None,
condition_updates: ConditionUpdates::none(),
publish_events: vec![EventTemplate::MediaFlowEstablished],
},
)
}
}