Skip to content

Commit

Permalink
Refactor and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
casey committed Oct 12, 2023
1 parent 808b1cd commit 5c0d364
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 30 deletions.
1 change: 1 addition & 0 deletions src/load_dotenv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub(crate) fn load_dotenv(
.dotenv_filename
.as_ref()
.or(settings.dotenv_filename.as_ref());

let dotenv_path = config
.dotenv_path
.as_ref()
Expand Down
52 changes: 24 additions & 28 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -780,22 +780,23 @@ impl<'tokens, 'src> Parser<'tokens, 'src> {
self.presume_keyword(Keyword::Set)?;
let name = Name::from_identifier(self.presume(Identifier)?);
let lexeme = name.lexeme();
let keyword = Keyword::from_lexeme(lexeme);
let Some(keyword) = Keyword::from_lexeme(lexeme) else {
return Err(name.error(CompileErrorKind::UnknownSetting {
setting: name.lexeme(),
}));
};

let set_bool: Option<Setting> = match keyword {
Some(kw) => match kw {
Keyword::AllowDuplicateRecipes => {
Some(Setting::AllowDuplicateRecipes(self.parse_set_bool()?))
}
Keyword::DotenvLoad => Some(Setting::DotenvLoad(self.parse_set_bool()?)),
Keyword::Export => Some(Setting::Export(self.parse_set_bool()?)),
Keyword::Fallback => Some(Setting::Fallback(self.parse_set_bool()?)),
Keyword::IgnoreComments => Some(Setting::IgnoreComments(self.parse_set_bool()?)),
Keyword::PositionalArguments => Some(Setting::PositionalArguments(self.parse_set_bool()?)),
Keyword::WindowsPowershell => Some(Setting::WindowsPowerShell(self.parse_set_bool()?)),
_ => None,
},
None => None,
let set_bool = match keyword {
Keyword::AllowDuplicateRecipes => {
Some(Setting::AllowDuplicateRecipes(self.parse_set_bool()?))
}
Keyword::DotenvLoad => Some(Setting::DotenvLoad(self.parse_set_bool()?)),
Keyword::Export => Some(Setting::Export(self.parse_set_bool()?)),
Keyword::Fallback => Some(Setting::Fallback(self.parse_set_bool()?)),
Keyword::IgnoreComments => Some(Setting::IgnoreComments(self.parse_set_bool()?)),
Keyword::PositionalArguments => Some(Setting::PositionalArguments(self.parse_set_bool()?)),
Keyword::WindowsPowershell => Some(Setting::WindowsPowerShell(self.parse_set_bool()?)),
_ => None,
};

if let Some(value) = set_bool {
Expand All @@ -804,22 +805,17 @@ impl<'tokens, 'src> Parser<'tokens, 'src> {

self.expect(ColonEquals)?;

let set_value: Option<Setting> = match keyword {
Some(kw) => match kw {
Keyword::DotenvFilename => {
Some(Setting::DotenvFilename(self.parse_string_literal()?.cooked))
}
Keyword::DotenvPath => Some(Setting::DotenvPath(self.parse_string_literal()?.cooked)),
Keyword::Shell => Some(Setting::Shell(self.parse_shell()?)),
Keyword::Tempdir => Some(Setting::Tempdir(self.parse_string_literal()?.cooked)),
Keyword::WindowsShell => Some(Setting::WindowsShell(self.parse_shell()?)),
_ => None,
},
None => None,
let set_value = match keyword {
Keyword::DotenvFilename => Some(Setting::DotenvFilename(self.parse_string_literal()?.cooked)),
Keyword::DotenvPath => Some(Setting::DotenvPath(self.parse_string_literal()?.cooked)),
Keyword::Shell => Some(Setting::Shell(self.parse_shell()?)),
Keyword::Tempdir => Some(Setting::Tempdir(self.parse_string_literal()?.cooked)),
Keyword::WindowsShell => Some(Setting::WindowsShell(self.parse_shell()?)),
_ => None,
};

if let Some(value) = set_value {
Ok(Set { name, value })
return Ok(Set { name, value });
} else {
Err(name.error(CompileErrorKind::UnknownSetting {
setting: name.lexeme(),
Expand Down
18 changes: 16 additions & 2 deletions tests/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,24 @@ test! {
set foo
",
stderr: "
error: Expected ':=', but found end of line
error: Unknown setting `foo`
|
1 | set foo
| ^
| ^^^
",
status: EXIT_FAILURE,
}

test! {
name: bad_setting_with_keyword_name,
justfile: "
set if := 'foo'
",
stderr: "
error: Unknown setting `if`
|
1 | set if := 'foo'
| ^^
",
status: EXIT_FAILURE,
}
Expand Down

0 comments on commit 5c0d364

Please sign in to comment.