From 49aa2b67e26536a4e75951b54fa4bcfc4c659008 Mon Sep 17 00:00:00 2001 From: steven-omaha <35634100+steven-omaha@users.noreply.github.com> Date: Thu, 12 Jan 2023 13:49:42 +0100 Subject: [PATCH] add backend macro (WIP) --- src/backend/macros.rs | 72 +++++++++++++++++++++++++++++++++++++++++++ src/backend/mod.rs | 44 ++++++++++++-------------- 2 files changed, 91 insertions(+), 25 deletions(-) diff --git a/src/backend/macros.rs b/src/backend/macros.rs index 29bf2ac..19030eb 100644 --- a/src/backend/macros.rs +++ b/src/backend/macros.rs @@ -41,3 +41,75 @@ macro_rules! impl_backend_constants { } }; } + +// #[macro_export] +// macro_rules! register_backends { +// ($first:ident, $($name:ident),*) => { +// #[derive(Debug, Hash, PartialEq, Eq)] +// pub(crate) enum Backends { +// $first, +// $( +// $name, +// )* +// } +// +// impl Backends { +// pub fn iter() -> BackendIter { +// BackendIter { +// next: Some(Backends::$first), +// } +// } +// fn next(&self) -> Option { +// match self { +// $( +// Self::$name => match $name { +// $( +// _ => Some(Self::$name), +// )* +// _ => None +// }, +// )* +// } +// } +// } +// } +// } + +#[macro_export] +macro_rules! register_backends { + ($first:ident, $($name:ident),*) => { + #[derive(Debug, Hash, PartialEq, Eq)] + pub(crate) enum Backends { + $first, + $( + $name, + )* + } + + impl Backends { + pub fn iter() -> BackendIter { + BackendIter { + next: Some(Backends::$first), + } + } + + fn next(&self) -> Option { + println!("Some({:?})", Self::$first); + $( + println!("{:?}", Self::$name); + )* + None + } + + fn get_backend(&self) -> Box { + match self { + Self::$first => Box::new($first::new()), + $( + Self::$name => Box::new($name::new()), + )* + } + } + + } + } +} diff --git a/src/backend/mod.rs b/src/backend/mod.rs index 5ef5ce5..2fd09fb 100644 --- a/src/backend/mod.rs +++ b/src/backend/mod.rs @@ -9,41 +9,35 @@ use std::process::Command; use anyhow::{Context, Result}; +use crate::register_backends; use crate::{Group, Package}; -pub(crate) use pacman::Pacman; -pub(crate) use rust::Rust; pub(crate) use todo_per_backend::ToDoPerBackend; pub(in crate::backend) type Switches = &'static [&'static str]; pub(in crate::backend) type Text = &'static str; -#[derive(Debug, Hash, PartialEq, Eq)] -pub(crate) enum Backends { - Pacman, - Rust, -} +pub(crate) use pacman::Pacman; +pub(crate) use rust::Rust; + +register_backends!(Pacman, Rust); impl Backends { - pub fn iter() -> BackendIter { - BackendIter { - next: Some(Backends::Pacman), - } - } + // fn next(&self) -> Option { + // match self { + // Self::Pacman => Some(Self::Rust), + // Self::Rust => Some(Self::Pip), + // Self::Pip => None, + // } + // } - fn next(&self) -> Option { - match self { - Self::Pacman => Some(Self::Rust), - Self::Rust => None, - } - } - - fn get_backend(&self) -> Box { - match self { - Self::Pacman => Box::new(Pacman::new()), - Self::Rust => Box::new(Rust::new()), - } - } + // fn get_backend(&self) -> Box { + // match self { + // Self::Pacman => Box::new(Pacman::new()), + // Self::Rust => Box::new(Rust::new()), + // _ => todo!(), + // } + // } } #[derive(Debug)]