From c59ecd893379a616daf9a93822128b235cb98bb2 Mon Sep 17 00:00:00 2001 From: Revar Desmera Date: Mon, 25 Mar 2019 01:40:53 -0700 Subject: [PATCH] Added function equivalents of assertion, deprecate, etc. --- compat.scad | 55 ++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 50 insertions(+), 5 deletions(-) diff --git a/compat.scad b/compat.scad index 27feb43..847043c 100644 --- a/compat.scad +++ b/compat.scad @@ -138,6 +138,11 @@ function scalar_vec3(v, dflt=undef) = +// Function: f_echo() +// Description: If possible, echo a message from a function. +function f_echo(msg) = (version_num() >= 20190303)? echo(msg) : 0; + + // Section: Modules @@ -164,6 +169,17 @@ module assert_in_list(argname, val, l, idx=undef) { } } +function assert_in_list(argname, val, l, idx=undef) = + let(succ = search([val], l, num_returns_per_match=1, index_col_num=idx) != [[]]) + succ? 0 : let( + msg = str( + "In argument '", argname, "', ", + (is_str(val)? str("\"", val, "\"") : val), + " must be one of ", + (is_def(idx)? [for (v=l) v[idx]] : l) + ) + ) assertion(succ, msg); + // Module: assertion() // Usage: @@ -183,6 +199,11 @@ module assertion(succ, msg) { } } +function assertion(succ, msg) = + (version_num() >= 20190303)? + let(FAILED=succ) assert(FAILED, msg) : + (!succ)? echo_error(msg) : 0; + // Module: echo_error() // Usage: @@ -195,6 +216,9 @@ module echo_error(msg, pfx="ERROR") { echo(str("

", pfx, ": ", msg, "

")); } +function echo_error(msg, pfx="ERROR") = + f_echo(str("

", pfx, ": ", msg, "

")); + // Module: echo_warning() // Usage: @@ -207,6 +231,9 @@ module echo_warning(msg, pfx="WARNING") { echo(str("

", pfx, ": ", msg, "

")); } +function echo_warning(msg, pfx="WARNING") = + f_echo(str("

", pfx, ": ", msg, "

")); + // Module: deprecate() // Usage: @@ -226,6 +253,16 @@ module deprecate(name, suggest=undef) { ); } +function deprecate(name, suggest=undef) = + echo_warning(pfx="DEPRECATED", + str( + "`", name, "` is deprecated and should not be used.", + !is_def(suggest)? "" : str( + " You should use `", suggest, "` instead." + ) + ) + ); + // Module: deprecate_argument() // Usage: @@ -236,18 +273,26 @@ module deprecate(name, suggest=undef) { // arg = The name of the deprecated argument. // suggest = If given, the argument to recommend using instead. module deprecate_argument(name, arg, suggest=undef) { - echo(str( - "

", - "DEPRECATED ARGUMENT: In `", name, "`, ", + echo_warning(pfx="DEPRECATED ARG", str( + "In `", name, "`, ", "the argument `", arg, "` ", "is deprecated and should not be used.", !is_def(suggest)? "" : str( " You should use `", suggest, "` instead." - ), - "

" + ) )); } +function deprecate_argument(name, arg, suggest=undef) = + echo_warning(pfx="DEPRECATED ARG", str( + "In `", name, "`, ", + "the argument `", arg, "` ", + "is deprecated and should not be used.", + !is_def(suggest)? "" : str( + " You should use `", suggest, "` instead." + ) + )); + // vim: noexpandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap