Commit 9ba01822 authored by AdminXVII's avatar AdminXVII
Browse files

Document expansion & fmt

parent 9931344b
......@@ -36,19 +36,11 @@ impl Range {
}
}
pub fn exclusive(start: Index, end: Index) -> Range {
Range { start, end, inclusive: false }
}
pub fn exclusive(start: Index, end: Index) -> Range { Range { start, end, inclusive: false } }
pub fn inclusive(start: Index, end: Index) -> Range {
Range { start, end, inclusive: true }
}
pub fn inclusive(start: Index, end: Index) -> Range { Range { start, end, inclusive: true } }
pub fn from(start: Index) -> Range {
Range { start, end: Index::new(-1), inclusive: true }
}
pub fn from(start: Index) -> Range { Range { start, end: Index::new(-1), inclusive: true } }
pub fn to(end: Index) -> Range {
Range { start: Index::new(0), end, inclusive: false }
}
pub fn to(end: Index) -> Range { Range { start: Index::new(0), end, inclusive: false } }
}
......@@ -14,8 +14,8 @@ pub enum Namespace {
#[derive(Clone, Debug)]
pub struct Scopes<K: Hash + Eq, V> {
flags: u8,
scopes: Vec<Scope<K, V>>,
flags: u8,
scopes: Vec<Scope<K, V>>,
current: usize,
}
......@@ -30,22 +30,18 @@ pub struct Scope<K: Hash + Eq, V> {
impl<K: Hash + Eq, V> Deref for Scope<K, V> {
type Target = HashMap<K, V>;
fn deref(&self) -> &Self::Target {
&self.vars
}
fn deref(&self) -> &Self::Target { &self.vars }
}
impl<K: Hash + Eq, V> DerefMut for Scope<K, V> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.vars
}
fn deref_mut(&mut self) -> &mut Self::Target { &mut self.vars }
}
impl<K: Hash + Eq, V: Clone> Scopes<K, V> {
pub fn with_capacity(cap: usize) -> Self {
Self {
flags: 0,
scopes: vec![Scope { vars: HashMap::with_capacity(cap), namespace: false }],
flags: 0,
scopes: vec![Scope { vars: HashMap::with_capacity(cap), namespace: false }],
current: 0,
}
}
......
......@@ -24,17 +24,13 @@ impl<T: Eq> Eq for Value<T> {}
// this one’s only special because of the lifetime parameter
impl<'a, T> From<&'a str> for Value<T> {
fn from(string: &'a str) -> Self {
Value::Str(string.into())
}
fn from(string: &'a str) -> Self { Value::Str(string.into()) }
}
macro_rules! value_from_type {
($arg:ident: $from:ty => $variant:ident($inner:expr)) => {
impl<T> From<$from> for Value<T> {
fn from($arg: $from) -> Self {
Value::$variant($inner)
}
fn from($arg: $from) -> Self { Value::$variant($inner) }
}
};
}
......
......@@ -52,9 +52,7 @@ macro_rules! math {
impl<'a, T> $trait<Value<T>> for &'a Value<T> {
type Output = Result<Value<T>, OpError>;
fn $fn(self, rhs: Value<T>) -> Self::Output {
self.$fn(&rhs)
}
fn $fn(self, rhs: Value<T>) -> Self::Output { self.$fn(&rhs) }
}
impl<'a, T> $trait<i128> for &'a Value<T> {
......
......@@ -16,29 +16,21 @@ pub type Str = small::String;
pub struct Alias(pub Str);
impl Alias {
pub fn empty() -> Self {
Alias(Str::with_capacity(1))
}
pub fn empty() -> Self { Alias(Str::with_capacity(1)) }
}
impl Deref for Alias {
type Target = Str;
fn deref(&self) -> &Self::Target {
&self.0
}
fn deref(&self) -> &Self::Target { &self.0 }
}
impl DerefMut for Alias {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 }
}
impl Into<Str> for Alias {
fn into(self) -> Str {
self.0
}
fn into(self) -> Str { self.0 }
}
impl<T> FromIterator<Value<T>> for Value<T> {
......
......@@ -8,7 +8,7 @@ use liner::{BasicCompleter, Completer, CursorPosition, Event, EventKind};
use std::{env, iter, path::PathBuf, str};
pub struct IonCompleter<'a, 'b> {
shell: &'b Shell<'a>,
shell: &'b Shell<'a>,
history_completer: Option<BasicCompleter>,
}
......@@ -30,9 +30,7 @@ fn escape(input: &str) -> String {
}
impl<'a, 'b> IonCompleter<'a, 'b> {
pub fn new(shell: &'b Shell<'a>) -> Self {
IonCompleter { shell, history_completer: None }
}
pub fn new(shell: &'b Shell<'a>) -> Self { IonCompleter { shell, history_completer: None } }
}
impl<'a, 'b> Completer for IonCompleter<'a, 'b> {
......@@ -230,8 +228,8 @@ fn filename_completion<'a>(start: &'a str, path: &'a PathBuf) -> impl Iterator<I
let globs = glob_with(
&string,
MatchOptions {
case_sensitive: true,
require_literal_separator: true,
case_sensitive: true,
require_literal_separator: true,
require_literal_leading_dot: false,
},
)
......@@ -263,9 +261,7 @@ fn filename_completion<'a>(start: &'a str, path: &'a PathBuf) -> impl Iterator<I
pub struct MultiCompleter<A>(Vec<A>);
impl<A> MultiCompleter<A> {
pub fn new(completions: Vec<A>) -> Self {
MultiCompleter(completions)
}
pub fn new(completions: Vec<A>) -> Self { MultiCompleter(completions) }
}
impl<A> Completer for MultiCompleter<A>
......
......@@ -27,9 +27,7 @@ pub fn expand_designators<'a>(context: &Context, cmd: &'a str) -> Cow<'a, str> {
}
}
fn command(text: &str) -> &str {
ArgumentSplitter::new(text).next().unwrap_or(text)
}
fn command(text: &str) -> &str { ArgumentSplitter::new(text).next().unwrap_or(text) }
fn args(text: &str) -> &str {
let bytes = text.as_bytes();
......@@ -50,10 +48,6 @@ fn args(text: &str) -> &str {
.unwrap_or(text)
}
fn first_arg(text: &str) -> &str {
ArgumentSplitter::new(text).nth(1).unwrap_or(text)
}
fn first_arg(text: &str) -> &str { ArgumentSplitter::new(text).nth(1).unwrap_or(text) }
fn last_arg(text: &str) -> &str {
ArgumentSplitter::new(text).last().unwrap_or(text)
}
fn last_arg(text: &str) -> &str { ArgumentSplitter::new(text).last().unwrap_or(text) }
......@@ -15,7 +15,7 @@ pub enum DesignatorToken<'a> {
#[derive(Debug)]
pub struct DesignatorLexer<'a> {
data: &'a [u8],
data: &'a [u8],
quotes: Quotes,
design: bool,
}
......
......@@ -53,7 +53,7 @@ DESCRIPTION
pub struct InteractiveBinary<'a> {
context: Rc<RefCell<Context>>,
shell: RefCell<Shell<'a>>,
shell: RefCell<Shell<'a>>,
}
impl<'a> InteractiveBinary<'a> {
......@@ -240,8 +240,8 @@ struct WordDivide<I>
where
I: Iterator<Item = (usize, char)>,
{
iter: I,
count: usize,
iter: I,
count: usize,
word_start: Option<usize>,
}
impl<I> WordDivide<I>
......
......@@ -36,11 +36,11 @@ pub enum AssignmentError<'a> {
/// string, and will contain the key/value pair to assign.
#[derive(Debug)]
pub struct AssignmentActions<'a> {
keys: KeyIterator<'a>,
keys: KeyIterator<'a>,
operator: Operator,
values: ArgumentSplitter<'a>,
values: ArgumentSplitter<'a>,
prevkeys: Vec<&'a str>,
prevval: &'a str,
prevval: &'a str,
}
impl<'a> AssignmentActions<'a> {
......
......@@ -166,9 +166,7 @@ fn file_has_execute_permission(filepath: &str) -> bool {
}
/// Returns true if the string is not empty
fn string_is_nonzero(string: &str) -> bool {
!string.is_empty()
}
fn string_is_nonzero(string: &str) -> bool { !string.is_empty() }
/// Returns true if the variable is an array and the array is not empty
fn array_var_is_not_empty(arrayvar: &str, shell: &Shell<'_>) -> bool {
......
......@@ -35,7 +35,7 @@ impl Status {
pub fn is_failure(self) -> bool { self.0 != 0 }
pub fn as_os_code(self) -> i32 { self.0 as i32 }
pub fn as_os_code(self) -> i32 { self.0 }
pub fn toggle(&mut self) { self.0 = if self.is_success() { 1 } else { 0 }; }
}
......
......@@ -63,9 +63,7 @@ fn get_var_string(name: &str, shell: &mut Shell<'_>) -> types::Str {
#[test]
fn test_is() {
fn vec_string(args: &[&str]) -> Vec<types::Str> {
args.iter().map(|&s| s.into()).collect()
}
fn vec_string(args: &[&str]) -> Vec<types::Str> { args.iter().map(|&s| s.into()).collect() }
let mut shell = Shell::default();
shell.variables_mut().set("x", "value");
shell.variables_mut().set("y", "0");
......
......@@ -338,9 +338,7 @@ fn file_is_character_device(filepath: &str) -> bool {
}
/// Exits SUCCESS if the file exists
fn file_exists(filepath: &str) -> bool {
Path::new(filepath).exists()
}
fn file_exists(filepath: &str) -> bool { Path::new(filepath).exists() }
/// Exits SUCCESS if the file is a regular file
fn file_is_regular(filepath: &str) -> bool {
......@@ -358,14 +356,10 @@ fn file_is_symlink(filepath: &str) -> bool {
}
/// Exits SUCCESS if the string is not empty
fn string_is_nonzero(string: &str) -> bool {
!string.is_empty()
}
fn string_is_nonzero(string: &str) -> bool { !string.is_empty() }
/// Exits SUCCESS if the string is empty
fn string_is_zero(string: &str) -> bool {
string.is_empty()
}
fn string_is_zero(string: &str) -> bool { string.is_empty() }
#[test]
fn test_strings() {
......
......@@ -42,8 +42,8 @@ fn escape_string(output: &mut SmallVec<[u8; 64]>, input: &str) {
pub struct MultipleBraceExpand<'a> {
permutator: Permutator<'a, str>,
tokens: &'a [BraceToken],
buffer: Vec<&'a str>,
tokens: &'a [BraceToken],
buffer: Vec<&'a str>,
}
impl<'a> MultipleBraceExpand<'a> {
......@@ -87,8 +87,8 @@ pub struct SingleBraceExpand<'a, 'b, I>
where
I: Iterator<Item = &'a str>,
{
elements: I,
tokens: &'b [BraceToken],
elements: I,
tokens: &'b [BraceToken],
loop_count: usize,
}
......
......@@ -14,9 +14,9 @@ use unicode_segmentation::UnicodeSegmentation;
#[derive(Debug, PartialEq, Clone)]
pub struct ArrayMethod<'a> {
method: &'a str,
variable: &'a str,
pattern: Pattern<'a>,
method: &'a str,
variable: &'a str,
pattern: Pattern<'a>,
selection: Select<types::Str>,
}
......
......@@ -357,9 +357,9 @@ mod test {
fn test_ends_with_succeeding() {
let mut output = types::Str::new();
let method = StringMethod {
method: "ends_with",
variable: "$FOO",
pattern: "\"BAR\"",
method: "ends_with",
variable: "$FOO",
pattern: "\"BAR\"",
selection: Select::All,
};
method.handle(&mut output, &DummyExpander).unwrap();
......@@ -370,9 +370,9 @@ mod test {
fn test_ends_with_failing() {
let mut output = types::Str::new();
let method = StringMethod {
method: "ends_with",
variable: "$FOO",
pattern: "\"BA\"",
method: "ends_with",
variable: "$FOO",
pattern: "\"BA\"",
selection: Select::All,
};
method.handle(&mut output, &DummyExpander).unwrap();
......@@ -383,9 +383,9 @@ mod test {
fn test_contains_succeeding() {
let mut output = types::Str::new();
let method = StringMethod {
method: "contains",
variable: "$FOO",
pattern: "\"OBA\"",
method: "contains",
variable: "$FOO",
pattern: "\"OBA\"",
selection: Select::All,
};
method.handle(&mut output, &DummyExpander).unwrap();
......@@ -396,9 +396,9 @@ mod test {
fn test_contains_failing() {
let mut output = types::Str::new();
let method = StringMethod {
method: "contains",
variable: "$FOO",
pattern: "\"OBI\"",
method: "contains",
variable: "$FOO",
pattern: "\"OBI\"",
selection: Select::All,
};
method.handle(&mut output, &DummyExpander).unwrap();
......@@ -409,9 +409,9 @@ mod test {
fn test_starts_with_succeeding() {
let mut output = types::Str::new();
let method = StringMethod {
method: "starts_with",
variable: "$FOO",
pattern: "\"FOO\"",
method: "starts_with",
variable: "$FOO",
pattern: "\"FOO\"",
selection: Select::All,
};
method.handle(&mut output, &DummyExpander).unwrap();
......@@ -422,9 +422,9 @@ mod test {
fn test_starts_with_failing() {
let mut output = types::Str::new();
let method = StringMethod {
method: "starts_with",
variable: "$FOO",
pattern: "\"OO\"",
method: "starts_with",
variable: "$FOO",
pattern: "\"OO\"",
selection: Select::All,
};
method.handle(&mut output, &DummyExpander).unwrap();
......@@ -435,9 +435,9 @@ mod test {
fn test_basename() {
let mut output = types::Str::new();
let method = StringMethod {
method: "basename",
variable: "\"/home/redox/file.txt\"",
pattern: "",
method: "basename",
variable: "\"/home/redox/file.txt\"",
pattern: "",
selection: Select::All,
};
method.handle(&mut output, &DummyExpander).unwrap();
......@@ -448,9 +448,9 @@ mod test {
fn test_extension() {
let mut output = types::Str::new();
let method = StringMethod {
method: "extension",
variable: "\"/home/redox/file.txt\"",
pattern: "",
method: "extension",
variable: "\"/home/redox/file.txt\"",
pattern: "",
selection: Select::All,
};
method.handle(&mut output, &DummyExpander).unwrap();
......@@ -461,9 +461,9 @@ mod test {
fn test_filename() {
let mut output = types::Str::new();
let method = StringMethod {
method: "filename",
variable: "\"/home/redox/file.txt\"",
pattern: "",
method: "filename",
variable: "\"/home/redox/file.txt\"",
pattern: "",
selection: Select::All,
};
method.handle(&mut output, &DummyExpander).unwrap();
......@@ -474,9 +474,9 @@ mod test {
fn test_parent() {
let mut output = types::Str::new();
let method = StringMethod {
method: "parent",
variable: "\"/home/redox/file.txt\"",
pattern: "",
method: "parent",
variable: "\"/home/redox/file.txt\"",
pattern: "",
selection: Select::All,
};
method.handle(&mut output, &DummyExpander).unwrap();
......@@ -487,9 +487,9 @@ mod test {
fn test_to_lowercase() {
let mut output = types::Str::new();
let method = StringMethod {
method: "to_lowercase",
variable: "\"Ford Prefect\"",
pattern: "",
method: "to_lowercase",
variable: "\"Ford Prefect\"",
pattern: "",
selection: Select::All,
};
method.handle(&mut output, &DummyExpander).unwrap();
......@@ -500,9 +500,9 @@ mod test {
fn test_to_uppercase() {
let mut output = types::Str::new();
let method = StringMethod {
method: "to_uppercase",
variable: "\"Ford Prefect\"",
pattern: "",
method: "to_uppercase",
variable: "\"Ford Prefect\"",
pattern: "",
selection: Select::All,
};
method.handle(&mut output, &DummyExpander).unwrap();
......@@ -513,9 +513,9 @@ mod test {
fn test_trim_with_string() {
let mut output = types::Str::new();
let method = StringMethod {
method: "trim",
variable: "\" Foo Bar \"",
pattern: "",
method: "trim",
variable: "\" Foo Bar \"",
pattern: "",
selection: Select::All,
};
method.handle(&mut output, &DummyExpander).unwrap();
......@@ -525,8 +525,12 @@ mod test {
#[test]
fn test_trim_with_variable() {
let mut output = types::Str::new();
let method =
StringMethod { method: "trim", variable: "$BAZ", pattern: "", selection: Select::All };
let method = StringMethod {
method: "trim",
variable: "$BAZ",
pattern: "",
selection: Select::All,
};
method.handle(&mut output, &DummyExpander).unwrap();
assert_eq!(&*output, "BARBAZ");
}
......@@ -535,9 +539,9 @@ mod test {
fn test_trim_right_with_string() {
let mut output = types::Str::new();
let method = StringMethod {
method: "trim_right",
variable: "\" Foo Bar \"",
pattern: "",
method: "trim_right",
variable: "\" Foo Bar \"",
pattern: "",
selection: Select::All,
};
method.handle(&mut output, &DummyExpander).unwrap();
......@@ -548,9 +552,9 @@ mod test {
fn test_trim_right_with_variable() {
let mut output = types::Str::new();
let method = StringMethod {
method: "trim_right",
variable: "$BAZ",
pattern: "",
method: "trim_right",
variable: "$BAZ",
pattern: "",
selection: Select::All,
};
method.handle(&mut output, &DummyExpander).unwrap();
......@@ -561,9 +565,9 @@ mod test {
fn test_trim_left_with_string() {
let mut output = types::Str::new();
let method = StringMethod {
method: "trim_left",
variable: "\" Foo Bar \"",
pattern: "",
method: "trim_left",
variable: "\" Foo Bar \"",
pattern: "",
selection: Select::All,
};
method.handle(&mut output, &DummyExpander).unwrap();
......@@ -574,9 +578,9 @@ mod test {
fn test_trim_left_with_variable() {
let mut output = types::Str::new();
let method = StringMethod {
method: "trim_left",
variable: "$BAZ",
pattern: "",
method: "trim_left",
variable: "$BAZ",
pattern: "",
selection: Select::All,
};
method.handle(&mut output, &DummyExpander).unwrap();
......@@ -587,9 +591,9 @@ mod test {
fn test_repeat_succeeding() {
let mut output = types::Str::new();
let method = StringMethod {
method: "repeat",
variable: "$FOO",
pattern: "2",
method: "repeat",
variable: "$FOO",
pattern: "2",
selection: Select::All,
};
method.handle(&mut output, &DummyExpander).unwrap();
......@@ -601,9 +605,9 @@ mod test {
fn test_repeat_failing() {
let mut output = types::Str::new();
let method = StringMethod {
method: "repeat",
variable: "$FOO",
pattern: "-2",
method: "repeat",
variable: "$FOO",
pattern: "-2",
selection: Select::All,
};
method.handle(&mut output, &DummyExpander).unwrap();
......@@ -613,9 +617,9 @@ mod test {
fn test_replace_succeeding() {
let mut output = types::Str::new();
let method = StringMethod {
method: "replace",
variable: "$FOO",
pattern: "[\"FOO\" \"BAR\"]",
method: "replace",
variable: "$FOO",