From 72993539773ae8e6fc4ca76da10775eccb547c8d Mon Sep 17 00:00:00 2001 From: Chris Hamons Date: Wed, 2 Mar 2022 18:48:28 -0600 Subject: [PATCH] Add error() function (#1118) --- README.md | 26 ++++++++++++++++++++++++++ src/function.rs | 5 +++++ tests/functions.rs | 10 ++++++++++ 3 files changed, 41 insertions(+) diff --git a/README.md b/README.md index 167ab1b..5b82c27 100644 --- a/README.md +++ b/README.md @@ -1012,6 +1012,10 @@ These functions can fail, for example if a path does not have an extension, whic - `path_exists(path)` - Returns `true` if the path points at an existing entity and `false` otherwise. Traverses symbolic links, and returns `false` if the path is inaccessible or points to a broken symlink. +##### Error Reporting + +- `error(message)` - Abort execution and report error `message` to user. + ### Command Evaluation Using Backticks Backticks can be used to store the result of commands: @@ -1118,6 +1122,28 @@ $ just bar abc ``` +### Stopping execution with error + +Execution can be halted with the `error` function. For example: + +``` +foo := if "hello" == "goodbye" { + "xyz" +} else if "a" == "b" { + "abc" +} else { + error("123") +} +``` + +Which produce the following error when run: + +``` +error: Call to function `error` failed: 123 + | +16 | error("123") +``` + ### Setting Variables from the Command Line Variables can be overridden from the command line. diff --git a/src/function.rs b/src/function.rs index cc7116e..96dc6ed 100644 --- a/src/function.rs +++ b/src/function.rs @@ -19,6 +19,7 @@ lazy_static! { ("clean", Unary(clean)), ("env_var", Unary(env_var)), ("env_var_or_default", Binary(env_var_or_default)), + ("error", Unary(error)), ("extension", Unary(extension)), ("file_name", Unary(file_name)), ("file_stem", Unary(file_stem)), @@ -117,6 +118,10 @@ fn env_var_or_default( } } +fn error(_context: &FunctionContext, message: &str) -> Result { + Err(message.to_owned()) +} + fn extension(_context: &FunctionContext, path: &str) -> Result { Utf8Path::new(path) .extension() diff --git a/tests/functions.rs b/tests/functions.rs index 5eb865e..713f1dd 100644 --- a/tests/functions.rs +++ b/tests/functions.rs @@ -404,6 +404,16 @@ fn test_path_exists_filepath_doesnt_exist() { .run(); } +#[test] +fn error_errors_with_message() { + Test::new() + .justfile("x := error ('Thing Not Supported')") + .args(&["--evaluate"]) + .status(1) + .stderr("error: Call to function `error` failed: Thing Not Supported\n |\n1 | x := error ('Thing Not Supported')\n | ^^^^^\n") + .run(); +} + #[test] fn test_absolute_path_resolves() { let test_object = Test::new()