use super::bridge;
use agent_client_protocol::{v1::schema as v1, v2::schema as v2};
pub(super) fn v1_options(options: &[v2::SessionConfigOption]) -> Vec<v1::SessionConfigOption> {
options.iter().filter_map(v1_option).collect()
}
fn v1_option(option: &v2::SessionConfigOption) -> Option<v1::SessionConfigOption> {
let kind = match &option.kind {
v2::SessionConfigKind::Select(select) => v1::SessionConfigKind::Select(
v1::SessionConfigSelect::new(select.current_value.to_string(), select_options(select)?),
),
v2::SessionConfigKind::Boolean(boolean) => {
v1::SessionConfigKind::Boolean(v1::SessionConfigBoolean::new(boolean.current_value))
}
_ => return None,
};
let category = match &option.category {
Some(category) => Some(bridge(category).ok()?),
None => None,
};
Some(
v1::SessionConfigOption::new(option.config_id.to_string(), option.name.clone(), kind)
.description(option.description.clone())
.category(category)
.meta(option.meta.clone()),
)
}
fn select_options(select: &v2::SessionConfigSelect) -> Option<v1::SessionConfigSelectOptions> {
let values = |options: &[v2::SessionConfigSelectOption]| -> Vec<_> {
options
.iter()
.map(|option| {
v1::SessionConfigSelectOption::new(option.value.to_string(), option.name.clone())
.description(option.description.clone())
.meta(option.meta.clone())
})
.collect()
};
match &select.options {
v2::SessionConfigSelectOptions::Ungrouped(options) => Some(values(options).into()),
v2::SessionConfigSelectOptions::Grouped(groups) => Some(
groups
.iter()
.map(|group| {
v1::SessionConfigSelectGroup::new(
group.group_id.to_string(),
group.name.clone(),
values(&group.options),
)
.meta(group.meta.clone())
})
.collect::<Vec<_>>()
.into(),
),
_ => None,
}
}