lib: add test and testString

This commit is contained in:
tv 2017-06-18 15:36:18 +02:00
parent ce89fd63d5
commit 9f75e81c5f
4 changed files with 17 additions and 14 deletions

View File

@ -47,9 +47,7 @@ let
type = mkOptionType { type = mkOptionType {
# TODO admit symbolic mode # TODO admit symbolic mode
name = "octal mode"; name = "octal mode";
check = x: check = test "[0-7][0-7][0-7][0-7]";
isString x &&
match "[0-7][0-7][0-7][0-7]" x != null;
merge = mergeOneOption; merge = mergeOneOption;
}; };
}; };

View File

@ -29,6 +29,10 @@ let
setAttr = name: value: set: set // { ${name} = value; }; 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 toC = x: let
type = typeOf x; type = typeOf x;
reject = throw "cannot convert ${type}"; reject = throw "cannot convert ${type}";

View File

@ -5,7 +5,7 @@ with lib;
rec { rec {
escape = escape =
let let
isSafeChar = c: match "[-+./0-9:=A-Z_a-z]" c != null; isSafeChar = testString "[-+./0-9:=A-Z_a-z]";
in in
stringAsChars (c: stringAsChars (c:
if isSafeChar c then c if isSafeChar c then c

View File

@ -2,10 +2,10 @@
let let
inherit (lib) inherit (lib)
all any concatMapStringsSep concatStringsSep const filter flip genid all any concatMapStringsSep concatStringsSep const filter flip
hasSuffix head isInt isString length match mergeOneOption mkOption genid hasSuffix head isInt isString length mergeOneOption mkOption
mkOptionType optional optionalAttrs optionals range splitString mkOptionType optional optionalAttrs optionals range splitString
stringLength substring typeOf; stringLength substring test typeOf;
inherit (lib.types) inherit (lib.types)
attrsOf bool either enum int listOf nullOr path str string submodule; attrsOf bool either enum int listOf nullOr path str string submodule;
in in
@ -338,7 +338,8 @@ rec {
check = let check = let
IPv4address = let d = "([1-9]?[0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])"; in 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); concatMapStringsSep "." (const d) (range 1 4);
in x: isString x && match IPv4address x != null; in
test IPv4address;
merge = mergeOneOption; merge = mergeOneOption;
}; };
addr6 = mkOptionType { addr6 = mkOptionType {
@ -346,7 +347,8 @@ rec {
check = let check = let
# TODO check IPv6 address harder # TODO check IPv6 address harder
IPv6address = "[0-9a-f.:]+"; IPv6address = "[0-9a-f.:]+";
in x: isString x && match IPv6address x != null; in
test IPv6address;
merge = mergeOneOption; merge = mergeOneOption;
}; };
@ -396,14 +398,13 @@ rec {
file-mode = mkOptionType { file-mode = mkOptionType {
name = "file mode"; name = "file mode";
check = x: isString x && match "[0-7]{4}" x != null; check = test "[0-7]{4}";
merge = mergeOneOption; merge = mergeOneOption;
}; };
haskell.conid = mkOptionType { haskell.conid = mkOptionType {
name = "Haskell constructor identifier"; name = "Haskell constructor identifier";
check = x: check = test "[[:upper:]][[:lower:]_[:upper:]0-9']*";
isString x && match "[[:upper:]][[:lower:]_[:upper:]0-9']*" x != null;
merge = mergeOneOption; merge = mergeOneOption;
}; };
@ -426,14 +427,14 @@ rec {
name = "label"; name = "label";
# TODO case-insensitive labels # TODO case-insensitive labels
check = x: isString x 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; merge = mergeOneOption;
}; };
# POSIX.12013, 3.278 Portable Filename Character Set # POSIX.12013, 3.278 Portable Filename Character Set
filename = mkOptionType { filename = mkOptionType {
name = "POSIX filename"; 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; merge = mergeOneOption;
}; };