Skip to content

Commit

Permalink
Cleanup (#2026)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamnemecek authored May 15, 2024
1 parent c796a25 commit b855400
Show file tree
Hide file tree
Showing 23 changed files with 171 additions and 181 deletions.
2 changes: 1 addition & 1 deletion src/analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ impl<'src> Analyzer<'src> {
asts: &HashMap<PathBuf, Ast<'src>>,
root: &Path,
) -> CompileResult<'src, Justfile<'src>> {
Analyzer::default().justfile(loaded, paths, asts, root)
Self::default().justfile(loaded, paths, asts, root)
}

fn justfile(
Expand Down
6 changes: 3 additions & 3 deletions src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,21 @@ impl Color {
pub(crate) fn auto() -> Self {
Self {
use_color: UseColor::Auto,
..Color::default()
..Self::default()
}
}

pub(crate) fn always() -> Self {
Self {
use_color: UseColor::Always,
..Color::default()
..Self::default()
}
}

pub(crate) fn never() -> Self {
Self {
use_color: UseColor::Never,
..Color::default()
..Self::default()
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/compile_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ impl<'src> CompileError<'src> {
pub(crate) fn new(token: Token<'src>, kind: CompileErrorKind<'src>) -> CompileError<'src> {
Self {
token,
kind: Box::new(kind),
kind: kind.into(),
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ impl Compiler {
loader: &'src Loader,
root: &Path,
) -> RunResult<'src, Compilation<'src>> {
let mut asts: HashMap<PathBuf, Ast> = HashMap::new();
let mut paths: HashMap<PathBuf, PathBuf> = HashMap::new();
let mut srcs: HashMap<PathBuf, &str> = HashMap::new();
let mut asts = HashMap::<PathBuf, Ast>::new();
let mut paths = HashMap::<PathBuf, PathBuf>::new();
let mut srcs = HashMap::<PathBuf, &str>::new();
let mut loaded = Vec::new();

let mut stack = Vec::new();
Expand Down
6 changes: 3 additions & 3 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1290,7 +1290,7 @@ mod tests {
test! {
name: shell_args_clear,
args: ["--clear-shell-args"],
shell_args: Some(vec![]),
shell_args: Some(Vec::new()),

}

Expand All @@ -1304,14 +1304,14 @@ mod tests {
test! {
name: shell_args_set_and_clear,
args: ["--shell-arg", "bar", "--clear-shell-args"],
shell_args: Some(vec![]),
shell_args: Some(Vec::new()),

}

test! {
name: shell_args_set_multiple_and_clear,
args: ["--shell-arg", "bar", "--shell-arg", "baz", "--clear-shell-args"],
shell_args: Some(vec![]),
shell_args: Some(Vec::new()),

}

Expand Down
20 changes: 10 additions & 10 deletions src/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,23 +53,23 @@ impl<'src> Expression<'src> {
impl<'src> Display for Expression<'src> {
fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
match self {
Expression::Assert { condition, error } => write!(f, "assert({condition}, {error})"),
Expression::Backtick { token, .. } => write!(f, "{}", token.lexeme()),
Expression::Join { lhs: None, rhs } => write!(f, "/ {rhs}"),
Expression::Join {
Self::Assert { condition, error } => write!(f, "assert({condition}, {error})"),
Self::Backtick { token, .. } => write!(f, "{}", token.lexeme()),
Self::Join { lhs: None, rhs } => write!(f, "/ {rhs}"),
Self::Join {
lhs: Some(lhs),
rhs,
} => write!(f, "{lhs} / {rhs}"),
Expression::Concatenation { lhs, rhs } => write!(f, "{lhs} + {rhs}"),
Expression::Conditional {
Self::Concatenation { lhs, rhs } => write!(f, "{lhs} + {rhs}"),
Self::Conditional {
condition,
then,
otherwise,
} => write!(f, "if {condition} {{ {then} }} else {{ {otherwise} }}"),
Expression::StringLiteral { string_literal } => write!(f, "{string_literal}"),
Expression::Variable { name } => write!(f, "{}", name.lexeme()),
Expression::Call { thunk } => write!(f, "{thunk}"),
Expression::Group { contents } => write!(f, "({contents})"),
Self::StringLiteral { string_literal } => write!(f, "{string_literal}"),
Self::Variable { name } => write!(f, "{}", name.lexeme()),
Self::Call { thunk } => write!(f, "{thunk}"),
Self::Group { contents } => write!(f, "({contents})"),
}
}
}
Expand Down
14 changes: 7 additions & 7 deletions src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ pub(crate) enum Item<'src> {
impl<'src> Display for Item<'src> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self {
Item::Alias(alias) => write!(f, "{alias}"),
Item::Assignment(assignment) => write!(f, "{assignment}"),
Item::Comment(comment) => write!(f, "{comment}"),
Item::Import {
Self::Alias(alias) => write!(f, "{alias}"),
Self::Assignment(assignment) => write!(f, "{assignment}"),
Self::Comment(comment) => write!(f, "{comment}"),
Self::Import {
relative, optional, ..
} => {
write!(f, "import")?;
Expand All @@ -39,7 +39,7 @@ impl<'src> Display for Item<'src> {

write!(f, " {relative}")
}
Item::Module {
Self::Module {
name,
relative,
optional,
Expand All @@ -59,8 +59,8 @@ impl<'src> Display for Item<'src> {

Ok(())
}
Item::Recipe(recipe) => write!(f, "{}", recipe.color_display(Color::never())),
Item::Set(set) => write!(f, "{set}"),
Self::Recipe(recipe) => write!(f, "{}", recipe.color_display(Color::never())),
Self::Set(set) => write!(f, "{set}"),
}
}
}
16 changes: 6 additions & 10 deletions src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,16 @@ pub(crate) struct Lexer<'src> {
impl<'src> Lexer<'src> {
/// Lex `src`
pub(crate) fn lex(path: &'src Path, src: &'src str) -> CompileResult<'src, Vec<Token<'src>>> {
Lexer::new(path, src).tokenize()
Self::new(path, src).tokenize()
}

#[cfg(test)]
pub(crate) fn test_lex(src: &'src str) -> CompileResult<'src, Vec<Token<'src>>> {
Lexer::new("justfile".as_ref(), src).tokenize()
Self::new("justfile".as_ref(), src).tokenize()
}

/// Create a new Lexer to lex `src`
fn new(path: &'src Path, src: &'src str) -> Lexer<'src> {
fn new(path: &'src Path, src: &'src str) -> Self {
let mut chars = src.chars();
let next = chars.next();

Expand All @@ -57,7 +57,7 @@ impl<'src> Lexer<'src> {
line: 0,
};

Lexer {
Self {
indentation: vec![""],
tokens: Vec::new(),
token_start: start,
Expand Down Expand Up @@ -282,11 +282,7 @@ impl<'src> Lexer<'src> {

/// True if `c` can be a continuation character of an identifier
fn is_identifier_continue(c: char) -> bool {
if Self::is_identifier_start(c) {
return true;
}

matches!(c, '0'..='9' | '-')
Self::is_identifier_start(c) || matches!(c, '0'..='9' | '-')
}

/// Consume the text and produce a series of tokens
Expand Down Expand Up @@ -1028,7 +1024,7 @@ mod tests {
length,
path: "justfile".as_ref(),
},
kind: Box::new(kind),
kind: kind.into(),
};
assert_eq!(have, want);
}
Expand Down
8 changes: 4 additions & 4 deletions src/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ pub struct List<T: Display, I: Iterator<Item = T> + Clone> {
}

impl<T: Display, I: Iterator<Item = T> + Clone> List<T, I> {
pub fn or<II: IntoIterator<Item = T, IntoIter = I>>(values: II) -> List<T, I> {
List {
pub fn or<II: IntoIterator<Item = T, IntoIter = I>>(values: II) -> Self {
Self {
conjunction: "or",
values: values.into_iter(),
}
}

pub fn and<II: IntoIterator<Item = T, IntoIter = I>>(values: II) -> List<T, I> {
List {
pub fn and<II: IntoIterator<Item = T, IntoIter = I>>(values: II) -> Self {
Self {
conjunction: "and",
values: values.into_iter(),
}
Expand Down
2 changes: 1 addition & 1 deletion src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub(crate) struct Loader {

impl Loader {
pub(crate) fn new() -> Self {
Loader {
Self {
srcs: Arena::new(),
paths: Arena::new(),
}
Expand Down
38 changes: 19 additions & 19 deletions src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ impl<'src> Node<'src> for Ast<'src> {
impl<'src> Node<'src> for Item<'src> {
fn tree(&self) -> Tree<'src> {
match self {
Item::Alias(alias) => alias.tree(),
Item::Assignment(assignment) => assignment.tree(),
Item::Comment(comment) => comment.tree(),
Item::Import {
Self::Alias(alias) => alias.tree(),
Self::Assignment(assignment) => assignment.tree(),
Self::Comment(comment) => comment.tree(),
Self::Import {
relative, optional, ..
} => {
let mut tree = Tree::atom("import");
Expand All @@ -32,7 +32,7 @@ impl<'src> Node<'src> for Item<'src> {

tree.push(format!("{relative}"))
}
Item::Module {
Self::Module {
name,
optional,
relative,
Expand All @@ -52,8 +52,8 @@ impl<'src> Node<'src> for Item<'src> {

tree
}
Item::Recipe(recipe) => recipe.tree(),
Item::Set(set) => set.tree(),
Self::Recipe(recipe) => recipe.tree(),
Self::Set(set) => set.tree(),
}
}
}
Expand Down Expand Up @@ -83,16 +83,16 @@ impl<'src> Node<'src> for Assignment<'src> {
impl<'src> Node<'src> for Expression<'src> {
fn tree(&self) -> Tree<'src> {
match self {
Expression::Assert {
Self::Assert {
condition: Condition { lhs, rhs, operator },
error,
} => Tree::atom(Keyword::Assert.lexeme())
.push(lhs.tree())
.push(operator.to_string())
.push(rhs.tree())
.push(error.tree()),
Expression::Concatenation { lhs, rhs } => Tree::atom("+").push(lhs.tree()).push(rhs.tree()),
Expression::Conditional {
Self::Concatenation { lhs, rhs } => Tree::atom("+").push(lhs.tree()).push(rhs.tree()),
Self::Conditional {
condition: Condition { lhs, rhs, operator },
then,
otherwise,
Expand All @@ -105,7 +105,7 @@ impl<'src> Node<'src> for Expression<'src> {
tree.push_mut(otherwise.tree());
tree
}
Expression::Call { thunk } => {
Self::Call { thunk } => {
use Thunk::*;

let mut tree = Tree::atom("call");
Expand Down Expand Up @@ -158,14 +158,14 @@ impl<'src> Node<'src> for Expression<'src> {

tree
}
Expression::Variable { name } => Tree::atom(name.lexeme()),
Expression::StringLiteral {
Self::Variable { name } => Tree::atom(name.lexeme()),
Self::StringLiteral {
string_literal: StringLiteral { cooked, .. },
} => Tree::string(cooked),
Expression::Backtick { contents, .. } => Tree::atom("backtick").push(Tree::string(contents)),
Expression::Group { contents } => Tree::List(vec![contents.tree()]),
Expression::Join { lhs: None, rhs } => Tree::atom("/").push(rhs.tree()),
Expression::Join {
Self::Backtick { contents, .. } => Tree::atom("backtick").push(Tree::string(contents)),
Self::Group { contents } => Tree::List(vec![contents.tree()]),
Self::Join { lhs: None, rhs } => Tree::atom("/").push(rhs.tree()),
Self::Join {
lhs: Some(lhs),
rhs,
} => Tree::atom("/").push(lhs.tree()).push(rhs.tree()),
Expand Down Expand Up @@ -258,8 +258,8 @@ impl<'src> Node<'src> for Line<'src> {
impl<'src> Node<'src> for Fragment<'src> {
fn tree(&self) -> Tree<'src> {
match self {
Fragment::Text { token } => Tree::string(token.lexeme()),
Fragment::Interpolation { expression } => Tree::List(vec![expression.tree()]),
Self::Text { token } => Tree::string(token.lexeme()),
Self::Interpolation { expression } => Tree::List(vec![expression.tree()]),
}
}
}
Expand Down
20 changes: 10 additions & 10 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -479,18 +479,18 @@ impl<'run, 'src> Parser<'run, 'src> {
self.parse_conditional()?
} else if self.accepted(Slash)? {
let lhs = None;
let rhs = Box::new(self.parse_expression()?);
let rhs = self.parse_expression()?.into();
Expression::Join { lhs, rhs }
} else {
let value = self.parse_value()?;

if self.accepted(Slash)? {
let lhs = Some(Box::new(value));
let rhs = Box::new(self.parse_expression()?);
let rhs = self.parse_expression()?.into();
Expression::Join { lhs, rhs }
} else if self.accepted(Plus)? {
let lhs = Box::new(value);
let rhs = Box::new(self.parse_expression()?);
let lhs = value.into();
let rhs = self.parse_expression()?.into();
Expression::Concatenation { lhs, rhs }
} else {
value
Expand Down Expand Up @@ -525,8 +525,8 @@ impl<'run, 'src> Parser<'run, 'src> {

Ok(Expression::Conditional {
condition,
then: Box::new(then),
otherwise: Box::new(otherwise),
then: then.into(),
otherwise: otherwise.into(),
})
}

Expand All @@ -542,8 +542,8 @@ impl<'run, 'src> Parser<'run, 'src> {
};
let rhs = self.parse_expression()?;
Ok(Condition {
lhs: Box::new(lhs),
rhs: Box::new(rhs),
lhs: lhs.into(),
rhs: rhs.into(),
operator,
})
}
Expand Down Expand Up @@ -592,7 +592,7 @@ impl<'run, 'src> Parser<'run, 'src> {
}
} else if self.next_is(ParenL) {
self.presume(ParenL)?;
let contents = Box::new(self.parse_expression()?);
let contents = self.parse_expression()?.into();
self.expect(ParenR)?;
Ok(Expression::Group { contents })
} else {
Expand Down Expand Up @@ -1055,7 +1055,7 @@ mod tests {
length,
path: "justfile".as_ref(),
},
kind: Box::new(kind),
kind: kind.into(),
};
assert_eq!(have, want);
}
Expand Down
Loading

0 comments on commit b855400

Please sign in to comment.