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

View file

@ -1,18 +1,16 @@
{this, ...}: let {this, ...}: let
inherit inherit
(builtins) (builtins)
isString
split split
stringLength stringLength
typeOf
; ;
inherit inherit
(this) (this.std)
enfIsNT
filterEven filterEven
init init
last last
ntTrapdoorKey
nullOr nullOr
stringHead stringHead
stringTail stringTail
@ -34,6 +32,12 @@ in rec {
isClassSig = sig: isClassSig = sig:
parseSig sig |> nullOr (result: last result |> isClassSig); 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 parseTypeSig = sig: let
result = parseSig sig; result = parseSig sig;
in in
@ -48,17 +52,6 @@ in rec {
then init result ++ (last result |> stringTail) then init result ++ (last result |> stringTail)
else null; 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'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) # NOTE: we measure similarity in the reverse order (ie end of signature is most important)
# sigSimilarity = type: list: let # sigSimilarity = type: list: let

View file

@ -1 +0,0 @@

View file

@ -1,14 +1,7 @@
{mix, ...} @ inputs: {mix, ...} @ inputs:
mix.newMixture inputs (mixture: { mix.newMixture inputs (mixture: {
includes.public = [ includes.public = [
./enforce.nix
./nt.nix ./nt.nix
./null.nix
./maybe.nix
./parse.nix
./sig.nix
./trapdoor.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}\"";
}