summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2017-06-18 15:36:18 +0200
committertv <tv@krebsco.de>2017-06-18 15:49:54 +0200
commit11a16797df89862be7dff81b6af5cd5bf41b580c (patch)
treea39844f33876126ae3d93be28e5a4cb7c07d200e /lib
parentf48808dda4911af60bc0cc32c00d6544f6e713b9 (diff)
lib: add test and testString
Diffstat (limited to 'lib')
-rw-r--r--lib/default.nix4
-rw-r--r--lib/shell.nix2
-rw-r--r--lib/types.nix21
3 files changed, 16 insertions, 11 deletions
diff --git a/lib/default.nix b/lib/default.nix
index 9399a01..803a614 100644
--- a/lib/default.nix
+++ b/lib/default.nix
@@ -29,6 +29,10 @@ let
setAttr = name: value: set: set // { ${name} = value; };
+ test = re: x: isString x && testString re x;
+
+ testString = re: x: match re x != null;
+
toC = x: let
type = typeOf x;
reject = throw "cannot convert ${type}";
diff --git a/lib/shell.nix b/lib/shell.nix
index a8ff5db..f977902 100644
--- a/lib/shell.nix
+++ b/lib/shell.nix
@@ -5,7 +5,7 @@ with lib;
rec {
escape =
let
- isSafeChar = c: match "[-+./0-9:=A-Z_a-z]" c != null;
+ isSafeChar = testString "[-+./0-9:=A-Z_a-z]";
in
stringAsChars (c:
if isSafeChar c then c
diff --git a/lib/types.nix b/lib/types.nix
index 530cd1e..5a01e5b 100644
--- a/lib/types.nix
+++ b/lib/types.nix
@@ -2,10 +2,10 @@
let
inherit (lib)
- all any concatMapStringsSep concatStringsSep const filter flip genid
- hasSuffix head isInt isString length match mergeOneOption mkOption
+ all any concatMapStringsSep concatStringsSep const filter flip
+ genid hasSuffix head isInt isString length mergeOneOption mkOption
mkOptionType optional optionalAttrs optionals range splitString
- stringLength substring typeOf;
+ stringLength substring test typeOf;
inherit (lib.types)
attrsOf bool either enum int listOf nullOr path str string submodule;
in
@@ -338,7 +338,8 @@ rec {
check = let
IPv4address = let d = "([1-9]?[0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])"; in
concatMapStringsSep "." (const d) (range 1 4);
- in x: isString x && match IPv4address x != null;
+ in
+ test IPv4address;
merge = mergeOneOption;
};
addr6 = mkOptionType {
@@ -346,7 +347,8 @@ rec {
check = let
# TODO check IPv6 address harder
IPv6address = "[0-9a-f.:]+";
- in x: isString x && match IPv6address x != null;
+ in
+ test IPv6address;
merge = mergeOneOption;
};
@@ -396,14 +398,13 @@ rec {
file-mode = mkOptionType {
name = "file mode";
- check = x: isString x && match "[0-7]{4}" x != null;
+ check = test "[0-7]{4}";
merge = mergeOneOption;
};
haskell.conid = mkOptionType {
name = "Haskell constructor identifier";
- check = x:
- isString x && match "[[:upper:]][[:lower:]_[:upper:]0-9']*" x != null;
+ check = test "[[:upper:]][[:lower:]_[:upper:]0-9']*";
merge = mergeOneOption;
};
@@ -426,14 +427,14 @@ rec {
name = "label";
# TODO case-insensitive labels
check = x: isString x
- && match "[0-9A-Za-z]([0-9A-Za-z-]*[0-9A-Za-z])?" x != null;
+ && test "[0-9A-Za-z]([0-9A-Za-z-]*[0-9A-Za-z])?" x;
merge = mergeOneOption;
};
# POSIX.1‐2013, 3.278 Portable Filename Character Set
filename = mkOptionType {
name = "POSIX filename";
- check = x: isString x && match "([0-9A-Za-z._])[0-9A-Za-z._-]*" x != null;
+ check = test "([0-9A-Za-z._])[0-9A-Za-z._-]*";
merge = mergeOneOption;
};