Skip to content

Commit

Permalink
refactor: get_commands_from_history
Browse files Browse the repository at this point in the history
  • Loading branch information
kyu08 committed Nov 26, 2024
1 parent 6733d58 commit 93b1414
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
11 changes: 10 additions & 1 deletion src/model/runner_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ use super::runner;
use serde::{Deserialize, Serialize};
use std::fmt;

#[derive(PartialEq, Debug, Clone, Serialize, Deserialize)]
#[derive(Hash, PartialEq, Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum RunnerType {
Make,
Pnpm,
}

impl std::cmp::Eq for RunnerType {}

impl RunnerType {
pub fn to_runner(&self, runners: &Vec<runner::Runner>) -> Option<runner::Runner> {
match self {
Expand All @@ -23,6 +25,13 @@ impl RunnerType {
RunnerType::Pnpm => todo!("implement and write test"),
}
}

pub fn from(runner: &runner::Runner) -> Self {
match runner {
runner::Runner::MakeCommand(_) => RunnerType::Make,
runner::Runner::PnpmCommand(_) => RunnerType::Pnpm,
}
}
}

impl fmt::Display for RunnerType {
Expand Down
35 changes: 19 additions & 16 deletions src/usecase/tui/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,25 +106,28 @@ impl Model<'_> {
history_commands: Vec<histories::HistoryCommand>,
runners: &Vec<runner::Runner>,
) -> Vec<command::Command> {
// TODO: Make this more readable and more performant.
// make a hashmap in order to search commands by O(1).
let command_hash_map: HashMap<runner_type::RunnerType, HashMap<String, command::Command>> = {
let mut map: HashMap<runner_type::RunnerType, HashMap<String, command::Command>> =
HashMap::new();
for runner in runners {
let mut inner_map = HashMap::new();
for c in runner.list_commands() {
inner_map.insert(c.name.clone(), c);
}
map.insert(runner_type::RunnerType::from(runner), inner_map);
}

map
};

let mut commands: Vec<command::Command> = Vec::new();
for history_command in history_commands {
match history_command.runner_type {
runner_type::RunnerType::Make => {
for runner in runners {
if let runner::Runner::MakeCommand(make) = runner {
// PERF: This method is called every time. Memoize should be considered.
for c in make.to_commands() {
if c.name == history_command.name {
commands.push(c);
break;
}
}
}
}
if let Some(inner_map) = command_hash_map.get(&history_command.runner_type) {
if let Some(c) = inner_map.get(&history_command.name) {
commands.push(c.clone());
}
runner_type::RunnerType::Pnpm => todo!(),
};
}
}
commands
}
Expand Down

0 comments on commit 93b1414

Please sign in to comment.