#[macro_export]
macro_rules! render_order_enum {
( $name:ident, $( $variant:ident ),+ ) => {
#[derive(::formatted_index_macro::FormattedIndex, Debug, Default, Clone, Copy, PartialOrd, Ord, PartialEq, Eq)]
pub enum $name {
#[default]
$(
$variant,
)*
}
impl From<$name> for usize {
fn from(value: $name) -> Self {
match value {
$(
$name::$variant => ${index()},
)*
}
}
}
impl TryFrom<usize> for $name {
type Error = anyhow::Error;
fn try_from(value: usize) -> Result<Self, Self::Error> {
Ok(match value {
$(
${index()} => $name::$variant,
)*
_ => anyhow::bail!("no matching variant of {} found for the provided index", stringify!($name)),
})
}
}
impl $name {
const NUMBER_OF_FIELDS: usize = ${count($variant)};
pub fn toggle(&mut self, direction: $crate::tui::action::Direction) {
let current_variant_idx = Into::<usize>::into(*self);
let next_variant_idx = match direction {
$crate::tui::action::Direction::Forward => (current_variant_idx + 1) % Self::NUMBER_OF_FIELDS,
$crate::tui::action::Direction::Backward => (current_variant_idx + Self::NUMBER_OF_FIELDS - 1) % Self::NUMBER_OF_FIELDS,
};
if let Ok(next_variant) = $name::try_from(next_variant_idx) {
*self = next_variant;
} else {
tracing::debug!("Failed to toggle the currently active element for {}. Unknown index: {}", stringify!($name), next_variant_idx);
}
}
pub fn select(&mut self, index: usize) -> anyhow::Result<()> {
let new_active_pane = anyhow::Context::<Self, anyhow::Error>::with_context(
TryFrom::<usize>::try_from(index),
|| format!("failed to convert the provided index into an instance of {}", stringify!($name))
)?;
*self = new_active_pane;
Ok(())
}
}
};
}
#[macro_export]
macro_rules! sort_fields_by_render_order {
( $order_enum:ident ) => {
impl $order_enum {
fn sort_fields_by_order(
fields: &mut [$crate::tui::view::pane::util::Field<
'_,
$order_enum,
>],
) {
fields.sort_by(|(a, _), (b, _)| a.cmp(b))
}
}
};
}