segregate parsing utilities

This commit is contained in:
Emile Clark-Boman 2026-01-25 11:42:16 +10:00
parent 57fee5b63e
commit b6324b652d
5 changed files with 15 additions and 59 deletions

View file

@ -9,16 +9,19 @@
inherit
(this)
enfIsType
is
Wrap
;
inherit
(this.std)
enfIsAttrs
;
in rec {
# form: getAttrAt :: list string -> set -> null | Wrap Any
# given path as a list of strings, return that value of an
# attribute set at that path
getAttrAt = path: xs:
assert enfIsType "set" xs "getAttrAt";
assert enfIsAttrs xs "getAttrAt";
foldl' (left: right:
if left != null && isAttrs left.value && hasAttr right left.value
then Wrap left.value.${right}
@ -30,7 +33,7 @@ in rec {
# given path as a list of strings, return that value of an
# attribute set at that path
hasAttrAt = path: xs:
assert enfIsType "set" xs "hasAttrAt";
assert enfIsAttrs xs "hasAttrAt";
getAttrAt path xs != null; # NOTE: inefficient (im lazy)
# Alternative to mapAttrsRecursiveCond

View file

@ -1,18 +1,16 @@
{this, ...}: let
inherit
(builtins)
isString
split
stringLength
typeOf
;
inherit
(this)
enfIsNT
(this.std)
filterEven
init
last
ntTrapdoorKey
nullOr
stringHead
stringTail
@ -34,6 +32,12 @@ in rec {
isClassSig = sig:
parseSig sig |> nullOr (result: last result |> isClassSig);
enfIsClassSig = sig: msg:
isClassSig sig || throw "${msg}: given value \"${toString sig}\" of primitive nix type \"${typeOf sig}\" is not a valid Typeclass signature";
enfIsTypeSig = sig: msg:
isTypeSig sig || throw "${msg}: given value \"${toString sig}\" of primitive nix type \"${typeOf sig}\" is not a valid Type signature";
parseTypeSig = sig: let
result = parseSig sig;
in
@ -48,17 +52,6 @@ in rec {
then init result ++ (last result |> stringTail)
else null;
# NOTE: unsafe variant, use typeSig if you can't guarantee `isNT T` holds
typeSig' = T: T.${ntTrapdoorKey}.sig;
# NOTE: safe variant, use typeSig' if you can guarantee `isNT T` holds
typeSig = T: assert enfIsNT T "nt.typeSig"; typeSig' T;
toTypeSig = x:
if isString x
then x
else typeSig x;
# NOTE: we're testing how similar `list` is to `toTypeSig type` (non-commutative)
# NOTE: we measure similarity in the reverse order (ie end of signature is most important)
# sigSimilarity = type: list: let

View file

@ -1 +0,0 @@

View file

@ -1,14 +1,7 @@
{mix, ...} @ inputs:
mix.newMixture inputs (mixture: {
includes.public = [
./enforce.nix
./nt.nix
./null.nix
./maybe.nix
./parse.nix
./sig.nix
./trapdoor.nix
./util.nix
./wrap.nix
];
})

View file

@ -1,32 +0,0 @@
{this, ...}: let
inherit
(builtins)
typeOf
;
inherit
(this)
impls
isClassSig
isNT
isTypeSig
toTypeSig
;
in {
enfIsType = type: value: msg: let
got = typeOf value;
in
got == type || throw "${msg}: expected primitive nix type \"${type}\" but got \"${got}\"";
enfIsClassSig = sig: msg:
isClassSig sig || throw "${msg}: given value \"${toString sig}\" of primitive nix type \"${typeOf sig}\" is not a valid Typeclass signature";
enfIsTypeSig = sig: msg:
isTypeSig sig || throw "${msg}: given value \"${toString sig}\" of primitive nix type \"${typeOf sig}\" is not a valid Type signature";
enfIsNT = T: msg:
isNT T || throw "${msg}: expected nt compatible type but got \"${toString T}\" of primitive nix type \"${typeOf T}\"";
enfImpls = type: T: msg:
impls type T || throw "${msg}: given type \"${toTypeSig T}\" does not implement typeclass \"${toTypeSig type}\"";
}