Skip to content

Commit

Permalink
refactor(either): enhance throw error info
Browse files Browse the repository at this point in the history
  • Loading branch information
GTrunSec committed Nov 9, 2023
1 parent 77e92c6 commit cde27c2
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,28 @@
};
};
eitherN = tn:
typedef "either<${concatStringsSep ", " (map (x: x.name) tn)}>" (x: any (t: (self.type t).check x) tn);
let
combine = a: b: if a == null then b else "[${a}]: ${b}";
logContexts = map (t: t.logContext or null) tn;
# Function to pair two lists, combining their corresponding elements
pairList = listA: listB: lib.lists.zipListsWith combine listA listB;
in
_typedef' null rec {
name = "either<${concatStringsSep ", " (map (x: x.name) tn)}>";
checkType = x: let
results = map (t: t.checkType x) tn;
allErrors = filter (r: !r.ok) results;
errorMsgs = map (r: r.err) allErrors;
formatError = err: "expected type '${name}', but found:\n" + err;
in
if any (r: r.ok) results
then { ok = true; }
else {
ok = false;
err = concatStringsSep "\n" (pairList logContexts (map formatError errorMsgs));
};
};

either = t1: t2: self.eitherN [t1 t2];
list = t:
typedef' rec {
Expand Down

0 comments on commit cde27c2

Please sign in to comment.