Skip to content

Commit

Permalink
Restrict alias replacment by default & Make sort IndexMap functions m…
Browse files Browse the repository at this point in the history
…ore generic
  • Loading branch information
ybda committed Dec 31, 2023
1 parent 4bba1cf commit cf06411
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 13 deletions.
8 changes: 0 additions & 8 deletions src/alias_dirs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,3 @@ pub fn validate_alias_name(alias_name: &str) -> Result<()> {
Ok(())
}

pub fn sort_by_key(ad: &mut AliasDirs) {
ad.sort_by(|k1, _, k2, _| k1.cmp(k2));
}

pub fn sort_by_value(ad: &mut AliasDirs) {
ad.sort_by(|_, value1, _, value2| value1.cmp(value2));
}

4 changes: 4 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ pub struct NewOpts {
/// Directory to mark [default: current directory]
#[clap(value_hint = ValueHint::DirPath)]
pub directory: Option<PathBuf>,

/// Rewrite alias if exists.
#[clap(short, long)]
pub force: bool,
}

/// Remove mark. Removes mark of current dir if no args provided.
Expand Down
3 changes: 3 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ pub enum Error {

#[error("Alias of directory '{0}' not found")]
AliasOfDirectoryXNotFound(String),

#[error("Alias '{0}' already exists")]
AliasAlreadyExists(String),
}

impl From<&'static str> for Error {
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ pub fn try_auto_sort(ad: &mut AliasDirs) {
};

if var == "a" {
alias_dirs::sort_by_key(ad)
util::sort_by_key(ad)
} else if var == "d" {
alias_dirs::sort_by_value(ad)
util::sort_by_value(ad)
}
}
10 changes: 7 additions & 3 deletions src/process_subcommand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ use crate::constants::LS_COLOR;
use crate::error::{Error, Result};
use crate::{alias_dirs, normalize, util};
use std::borrow::Cow;
use crate::alias_dirs::{AliasDirs, sort_by_key, sort_by_value};
use crate::alias_dirs::{AliasDirs};

pub fn new(opts: &NewOpts, ad: &mut AliasDirs) -> Result<()> {
alias_dirs::validate_alias_name(&opts.alias)?;

if !opts.force && ad.contains_key(&opts.alias) {
return Err(Error::AliasAlreadyExists(opts.alias.to_string()));
}

let dir = if let Some(dir) = &opts.directory {
Cow::Borrowed(dir)
} else {
Expand Down Expand Up @@ -65,9 +69,9 @@ pub fn sort(opts: &SortOpts, ad: &mut AliasDirs) {
}

if opts.directory {
sort_by_value(ad);
util::sort_by_value(ad);
} else {
sort_by_key(ad);
util::sort_by_key(ad);
}
}

Expand Down
16 changes: 16 additions & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use crate::error::Result;
use std::fs::File;
use std::hash::Hash;
use std::io::Read;
use std::path::{Path, PathBuf};
use indexmap::IndexMap;

pub fn retrieve_env_current_dir() -> Result<PathBuf> {
Ok(std::env::current_dir()
Expand All @@ -19,3 +21,17 @@ pub fn read_file_contents<P: AsRef<Path>>(filepath: P) -> Result<String> {
Ok(buf)
}

pub fn sort_by_key<K, V>(m: &mut IndexMap<K, V>)
where
K: Ord + Hash,
{
m.sort_by(|k1, _, k2, _| k1.cmp(k2));
}

pub fn sort_by_value<K, V>(m: &mut IndexMap<K, V>)
where
K: Ord + Hash,
V: Ord
{
m.sort_by(|_, v1, _, v2| v1.cmp(v2));
}

0 comments on commit cf06411

Please sign in to comment.