2021-02-22 14:32:45 +01:00
|
|
|
{ depot, ... }:
|
|
|
|
|
|
|
|
let
|
|
|
|
|
|
|
|
inherit (depot.nix)
|
|
|
|
yants
|
|
|
|
;
|
|
|
|
|
2021-03-04 22:20:09 +01:00
|
|
|
inherit (depot.users.sterni.nix)
|
|
|
|
fun
|
|
|
|
;
|
|
|
|
|
2021-02-22 14:32:45 +01:00
|
|
|
# we must avoid evaluating any of the sublists
|
|
|
|
# as they may contain conditions that throw
|
|
|
|
condition = yants.restrict "condition"
|
|
|
|
(ls: builtins.length ls == 2)
|
|
|
|
(yants.list yants.any);
|
|
|
|
|
2021-03-04 22:20:09 +01:00
|
|
|
/* Like the common lisp macro: takes a list
|
|
|
|
of two elemented lists whose first element
|
|
|
|
is a boolean. The second element of the
|
|
|
|
first list that has true as its first
|
|
|
|
element is returned.
|
|
|
|
|
|
|
|
Type: [ [ bool a ] ] -> a
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
cond [
|
|
|
|
[ (builtins.isString true) 12 ]
|
|
|
|
[ (3 == 2) 13 ]
|
|
|
|
[ true 42 ]
|
|
|
|
]
|
|
|
|
|
|
|
|
=> 42
|
2021-02-22 14:32:45 +01:00
|
|
|
*/
|
2021-03-04 22:20:09 +01:00
|
|
|
cond = conds: switch true conds;
|
|
|
|
|
|
|
|
/* Generic pattern match-ish construct for nix.
|
|
|
|
Takes a bunch of lists which are of length
|
|
|
|
two and checks the first element for either
|
|
|
|
a predicate or a value. The second value of
|
|
|
|
the first list which either has a value equal
|
|
|
|
to or a function that evaluates to true for
|
|
|
|
the given value.
|
|
|
|
|
|
|
|
Type: a -> [ [ (function | a) b ] ] -> b
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
switch "foo" [
|
|
|
|
[ "smol" "SMOL!!!" ]
|
|
|
|
[ (x: builtins.stringLength x <= 3) "smol-ish" ]
|
|
|
|
[ (fun.const true) "not smol" ]
|
|
|
|
]
|
|
|
|
|
|
|
|
=> "smol-ish"
|
|
|
|
*/
|
|
|
|
switch = x: conds:
|
2021-02-22 14:32:45 +01:00
|
|
|
if builtins.length conds == 0
|
2021-03-04 22:20:09 +01:00
|
|
|
then builtins.throw "exhausted all conditions"
|
2021-02-22 14:32:45 +01:00
|
|
|
else
|
|
|
|
let
|
|
|
|
c = condition (builtins.head conds);
|
2021-03-04 22:20:09 +01:00
|
|
|
s = builtins.head c;
|
|
|
|
b =
|
|
|
|
if builtins.isFunction s
|
|
|
|
then s x
|
|
|
|
else x == s;
|
2021-02-22 14:32:45 +01:00
|
|
|
in
|
2021-03-04 22:20:09 +01:00
|
|
|
if b
|
2021-02-22 14:32:45 +01:00
|
|
|
then builtins.elemAt c 1
|
2021-03-04 22:20:09 +01:00
|
|
|
else switch x (builtins.tail conds);
|
2021-02-22 14:32:45 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
in {
|
|
|
|
inherit
|
|
|
|
cond
|
2021-03-04 22:20:09 +01:00
|
|
|
switch
|
2021-02-22 14:32:45 +01:00
|
|
|
;
|
|
|
|
}
|