Skip to content

Commit

Permalink
feat: dump project
Browse files Browse the repository at this point in the history
  • Loading branch information
orhnk committed Jan 9, 2024
1 parent 09d5892 commit b646255
Show file tree
Hide file tree
Showing 8 changed files with 258 additions and 313 deletions.
25 changes: 25 additions & 0 deletions dump.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Permutation

struct Human {
actions: Vec<Action>
}

enum Action {
Eat(Food),
Sleep,
Code,
}

/* VS */

// Combination

struct Human {
actions: HumanActions
}

struct HumanActions {
eat: Food,
sleep: bool,
code: bool,
}
55 changes: 27 additions & 28 deletions gene-config/src/backends.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
macro_rules! generate_backends {
($($name:ident),*) => {
struct Backends {
$(
pub $name: bool,
)*
}
};
($($name:ident),*,) => {
generate_backends!($($name),*);
};
() => {
compile_error!("This macro requires at least one argument")
};
}

/// SPM backends
generate_backends!(
brew,
choco,
dpkg,
flatpak,
pacman,
rpm,
scoop,
snap,
zypper,
);
// macro_rules! generate_backends {
// ($($name:ident),*) => {
// struct Backends {
// $(
// pub $name: bool,
// )*
// }
// };
// ($($name:ident),*,) => {
// generate_backends!($($name),*);
// };
// () => {
// compile_error!("This macro requires at least one argument")
// };
// }

// /// SPM backends
// generate_backends!(
// brew,
// choco,
// dpkg,
// flatpak,
// pacman,
// rpm,
// scoop,
// snap,
// zypper,
// );
20 changes: 3 additions & 17 deletions gene-pmm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,5 @@
pub mod pm;
pub mod spm;
pub mod error;
pub mod mir;
pub mod pm;
pub mod raw_args;

pub fn add(left: usize, right: usize) -> usize {
left + right
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
}
}
pub mod spm;
42 changes: 42 additions & 0 deletions gene-pmm/src/mir.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use crate::pm::PackageManager;

/// List of actions that GENE can infer
#[derive(Debug, Clone, Copy)]
pub enum GeneMIRActions {
Install,
Remove,
Query,
Update,
}

/// GENE Middle Intermediate Representation
///
/// Abstraction to make it easier to transpile between different package managers.
/// Just like how LLVM IR works but dead simple.
pub struct GeneMIR<'a> {
/// Package Manager backend to use
package_manager: PackageManager,
/// Options that get transpiled
actions: Vec<GeneMIRActions>,
/// Non-transpiled Options
raw_actions: Vec<&'a str>,
}

impl<'a> GeneMIR<'a> {
pub fn new(package_manager: PackageManager) -> Self {
GeneMIR {
package_manager,
actions: Default::default(),
raw_actions: Default::default(),
}
}
pub fn actions(mut self, actions: Vec<GeneMIRActions>) -> Self {
self.actions.extend(actions);
self
}

pub fn raw_actions(mut self, raw_actions: Vec<&'a str>) -> Self {
self.raw_actions.extend(raw_actions);
self
}
}
51 changes: 46 additions & 5 deletions gene-pmm/src/pm.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,47 @@
use gene_utils::args::GeneArgs;
use std::mem::size_of_val;

pub trait PackageManager<'a>: TryFrom<&'a GeneArgs> {
/// Convert Middle Representation (Self) to String
fn compile(&'a self) -> String;
}
pub struct PackageManagerArgs<'a> {
package_manager: &'a str,
args: &'a [&'a str],
}

/// List of supported package manager backends
pub enum PackageManager {
APT,
Pacman,
}

impl ToString for PackageManager {
fn to_string(&self) -> String {
use PackageManager::*;

let pm_name = match self {
APT => "apt",
Pacman => "pacman",
};

pm_name.to_string()
}
}

impl<'a> PackageManagerArgs<'a> {
pub fn new(package_manager: &'a str, args: &'a [&'a str]) -> Self {
Self {
package_manager,
args,
}
}
}

impl ToString for PackageManagerArgs<'_> {
fn to_string(&self) -> String {
let mut cmd: Vec<&str> = Vec::with_capacity(
size_of_val(self.args) + size_of_val(self.package_manager) + self.args.len(), // allocate enough memory to fit package manager name, arguments and spaces between both of them.
);

cmd.push(self.package_manager);
cmd.extend_from_slice(self.args);

cmd.join(" ")
}
}
18 changes: 0 additions & 18 deletions gene-pmm/src/raw_args.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1 @@
/// Read-only wrapper for the raw arguments passed to the program.
#[derive(Debug)]
pub struct RawArgs<'a> {
pub args: &'a Vec<String>,
}

impl<'a> From<&'a Vec<String>> for RawArgs<'a> {
#[inline(always)]
fn from(args: &'a Vec<String>) -> Self {
Self { args }
}
}

impl ToString for RawArgs<'_> {
#[inline(always)]
fn to_string(&self) -> String {
self.args.join(" ")
}
}
Loading

0 comments on commit b646255

Please sign in to comment.