Skip to content

Commit

Permalink
Merge pull request #15 from DanCardin/dc/allocation_cleanup
Browse files Browse the repository at this point in the history
Remove a bunch of unecessary allocations.
  • Loading branch information
DanCardin authored Feb 6, 2021
2 parents 86c4ed8 + 03de4b0 commit 6b7b223
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 51 deletions.
2 changes: 1 addition & 1 deletion src/saucefile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl Saucefile {
};

let paths = context.cascade_paths();
let mut paths = paths.iter().peekable();
let mut paths = paths.peekable();
while let Some(path) = paths.next() {
if !path.is_file() {
continue;
Expand Down
47 changes: 12 additions & 35 deletions src/shell/actions.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::{
colors::{BLUE, RED, YELLOW},
option::Options,
saucefile::Saucefile,
shell::{utilities::get_binary, Shell},
target::Target,
Expand Down Expand Up @@ -46,13 +45,13 @@ pub fn execute_shell_command(context: &mut Context, shell: &dyn Shell, command:
pub fn clear(context: &mut Context, shell: &dyn Shell, mut saucefile: Saucefile) {
let options = &context.options;
let output = &mut context.output;
output.output(render_vars(&mut saucefile, options, |k, _| {
output.output(render_items(saucefile.vars(options), |k, _| {
shell.unset_var(k)
}));
output.output(render_aliases(&mut saucefile, options, |k, _| {
output.output(render_items(saucefile.aliases(options), |k, _| {
shell.unset_alias(k)
}));
output.output(render_functions(&mut saucefile, options, |k, _| {
output.output(render_items(saucefile.functions(options), |k, _| {
shell.unset_function(k)
}));
output.notify(&[BLUE.bold().paint("Cleared your sauce")]);
Expand Down Expand Up @@ -106,13 +105,13 @@ pub fn execute(
let options = &context.options;
let output = &mut context.output;

output.output(render_vars(&mut saucefile, options, |k, v| {
output.output(render_items(saucefile.vars(options), |k, v| {
shell.set_var(k, v)
}));
output.output(render_aliases(&mut saucefile, options, |k, v| {
output.output(render_items(saucefile.aliases(options), |k, v| {
shell.set_alias(k, v)
}));
output.output(render_functions(&mut saucefile, options, |k, v| {
output.output(render_items(saucefile.functions(options), |k, v| {
shell.set_function(k, v)
}));

Expand All @@ -122,39 +121,17 @@ pub fn execute(
]);
}

fn render_vars<F>(saucefile: &mut Saucefile, options: &Options, mut format_row: F) -> String
fn render_items<F>(items: Vec<(&str, String)>, mut format_row: F) -> String
where
F: FnMut(&str, &str) -> String,
{
saucefile
.vars(options)
items
.iter()
.map(|(k, v)| format_row(k, v))
.map(|v| format!("{};\n", v))
.collect()
}

fn render_aliases<F>(saucefile: &mut Saucefile, options: &Options, mut format_row: F) -> String
where
F: FnMut(&str, &str) -> String,
{
saucefile
.aliases(options)
.iter()
.map(|(k, v)| format_row(k, v))
.map(|v| format!("{};\n", v))
.collect()
}

fn render_functions<F>(saucefile: &mut Saucefile, options: &Options, mut format_row: F) -> String
where
F: FnMut(&str, &str) -> String,
{
saucefile
.functions(options)
.iter()
.map(|(k, v)| format_row(k, v))
.map(|v| format!("{};\n", v))
.map(|mut v| {
v += ";\n";
v
})
.collect()
}

Expand Down
70 changes: 55 additions & 15 deletions src/shell/context.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use anyhow::Result;
use etcetera::home_dir;
use std::env;
use std::path::PathBuf;
use std::{env, ops::Deref};

use crate::{
colors::{BLUE, RED, YELLOW},
Expand Down Expand Up @@ -143,28 +143,28 @@ impl<'a> Context<'a> {
actions::execute(self, shell_kind, saucefile, autoload);
}

pub fn cascade_paths(&self) -> Vec<PathBuf> {
if self.sauce_path == self.data_dir.with_extension("toml") {
return vec![self.sauce_path.clone()];
}

pub fn cascade_paths(&self) -> impl Iterator<Item = PathBuf> {
self.sauce_path
.ancestors()
.filter(|p| p.strip_prefix(&self.data_dir).is_ok())
.filter(|p| {
if self.data_dir.with_extension("toml") == *p {
true
} else {
p.strip_prefix(&self.data_dir).is_ok()
}
})
.map(|p| p.with_extension("toml"))
.collect::<Vec<PathBuf>>()
.into_iter()
.rev()
.collect()
}

pub fn set_var<T: AsRef<str>>(&mut self, values: &[T]) {
let mut saucefile = self.saucefile();
for values in values.iter() {
let parts: Vec<&str> = values.as_ref().splitn(2, '=').collect();
let var = parts[0];

let value = parts.get(1).map(Deref::deref).unwrap_or("");
let mut parts = values.as_ref().splitn(2, '=');
let var = parts.next().unwrap_or("");
let value = parts.next().unwrap_or("");

saucefile.set_var(var, value);
self.output.notify(&[
Expand All @@ -180,9 +180,9 @@ impl<'a> Context<'a> {
pub fn set_alias<T: AsRef<str>>(&mut self, values: &[T]) {
let mut saucefile = self.saucefile();
for values in values.iter() {
let parts: Vec<&str> = values.as_ref().splitn(2, '=').collect();
let var = parts[0];
let value = if parts.len() > 1 { parts[1] } else { "" };
let mut parts = values.as_ref().splitn(2, '=');
let var = parts.next().unwrap_or("");
let value = parts.next().unwrap_or("");
saucefile.set_alias(var, value);
self.output.notify(&[
BLUE.paint("Set "),
Expand Down Expand Up @@ -233,3 +233,43 @@ impl<'a> Default for Context<'a> {
}
}
}

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

mod cascade_paths {
use super::super::*;
use super::*;
use pretty_assertions::assert_eq;

#[test]
fn test_home() {
let mut context = Context::default();
context.data_dir = "~/.local/share/sauce".into();
context.sauce_path = "~/.local/share/sauce".into();

let paths: Vec<_> = context.cascade_paths().collect();

let expected: Vec<PathBuf> = vec!["~/.local/share/sauce.toml".into()];
assert_eq!(paths, expected);
}

#[test]
fn test_nested_subdir() {
let mut context = Context::default();
context.data_dir = "~/.local/share/sauce".into();
context.sauce_path = "~/.local/share/sauce/meow/meow/kitty.toml".into();

let paths: Vec<_> = context.cascade_paths().collect();

let expected: Vec<PathBuf> = vec![
"~/.local/share/sauce.toml".into(),
"~/.local/share/sauce/meow.toml".into(),
"~/.local/share/sauce/meow/meow.toml".into(),
"~/.local/share/sauce/meow/meow/kitty.toml".into(),
];
assert_eq!(paths, expected);
}
}
}

0 comments on commit 6b7b223

Please sign in to comment.