Skip to content

Commit

Permalink
refactor: use function in place of trait for autocomplete filenames
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelmauro committed Oct 21, 2024
1 parent b090806 commit 0fee9d5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 16 deletions.
21 changes: 8 additions & 13 deletions zkstack_cli/crates/zkstack/src/commands/autocomplete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ use crate::{
};

pub fn run(args: AutocompleteArgs) -> anyhow::Result<()> {
let path = args.out.join(args.generator.autocomplete_file_name());
let filename = autocomplete_file_name(&args.generator);
let path = args.out.join(filename);

logger::info(msg_generate_autocomplete_file(
path.to_str()
Expand All @@ -41,17 +42,11 @@ pub fn generate_completions<G: Generator>(gen: G, buf: &mut dyn Write) -> anyhow
Ok(())
}

pub trait ShellAutocomplete {
fn autocomplete_file_name(&self) -> &'static str;
}

impl ShellAutocomplete for clap_complete::Shell {
fn autocomplete_file_name(&self) -> &'static str {
match self {
clap_complete::Shell::Bash => "zkstack.sh",
clap_complete::Shell::Fish => "zkstack.fish",
clap_complete::Shell::Zsh => "_zkstack.zsh",
_ => todo!(),
}
pub fn autocomplete_file_name(shell: &clap_complete::Shell) -> &'static str {
match shell {
clap_complete::Shell::Bash => "zkstack.sh",
clap_complete::Shell::Fish => "zkstack.fish",
clap_complete::Shell::Zsh => "_zkstack.zsh",
_ => todo!(),
}
}
6 changes: 3 additions & 3 deletions zkstack_cli/crates/zkstack/src/commands/dev/commands/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use config::EcosystemConfig;
use xshell::{cmd, Shell};

use crate::commands::{
autocomplete::{generate_completions, ShellAutocomplete},
autocomplete::{autocomplete_file_name, generate_completions},
dev::{
commands::lint_utils::{get_unignored_files, Target},
messages::{
Expand Down Expand Up @@ -171,9 +171,9 @@ fn lint_autocompletion_files(_shell: &Shell, check: bool) -> anyhow::Result<()>

let new = String::from_utf8(writer)?;

let path = completion_folder.join(shell.autocomplete_file_name());
let path = completion_folder.join(autocomplete_file_name(&shell));
let mut autocomplete_file = File::open(path.clone())
.context(format!("failed to open {}", shell.autocomplete_file_name()))?;
.context(format!("failed to open {}", autocomplete_file_name(&shell)))?;

let mut old = String::new();
autocomplete_file.read_to_string(&mut old)?;
Expand Down

0 comments on commit 0fee9d5

Please sign in to comment.