use onlyne_config::Drive;
use onlyne_proto::{RoleInfo, Welcome};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RoleSlice {
pub drive: Drive,
pub command: Vec<String>,
pub max_sessions: u32,
pub required_targets: Vec<String>,
}
impl RoleSlice {
pub fn from_welcome(welcome: &Welcome) -> Self {
let runtime = welcome.runtime.clone().unwrap_or_default();
Self {
drive: drive_of(runtime.drive),
command: runtime.command,
max_sessions: welcome.max_sessions,
required_targets: welcome.allowed_targets.clone(),
}
}
pub fn from_role_info(info: &RoleInfo, _current: &RoleSlice) -> Self {
Self {
drive: drive_of(info.runtime.drive),
command: info.runtime.command.clone(),
max_sessions: info.max_sessions,
required_targets: info.edges.clone(),
}
}
}
pub fn drive_of(wire: onlyne_proto::Drive) -> Drive {
match wire {
onlyne_proto::Drive::Plugin => Drive::Plugin,
onlyne_proto::Drive::Acp => Drive::Acp,
onlyne_proto::Drive::Exec => Drive::Exec,
}
}
pub fn slice_diff(current: &RoleSlice, next: &RoleSlice) -> Vec<&'static str> {
let mut fields = Vec::new();
if current.drive != next.drive || current.command != next.command {
fields.push("runtime");
}
if current.max_sessions != next.max_sessions {
fields.push("max_sessions");
}
if current.required_targets != next.required_targets {
fields.push("allowed_targets");
}
fields
}
pub fn apply_if_changed(
current: &RoleSlice,
next: RoleSlice,
) -> Option<(RoleSlice, Vec<&'static str>)> {
let fields = slice_diff(current, &next);
if fields.is_empty() {
None
} else {
Some((next, fields))
}
}