mirror of
https://github.com/zaphar/ucg.git
synced 2025-07-22 18:19:54 -04:00
FEATURE: The begnnings of some schema definition utilities.
Also allow failure messages to be generated from any expression.
This commit is contained in:
parent
7959391b90
commit
6321828006
@ -663,7 +663,7 @@ make_fn!(
|
|||||||
do_each!(
|
do_each!(
|
||||||
pos => pos,
|
pos => pos,
|
||||||
_ => word!("fail"),
|
_ => word!("fail"),
|
||||||
msg => must!(wrap_err!(either!(format_expression, string_expression), "Expected failure message")),
|
msg => must!(wrap_err!(expression, "Expected failure message")),
|
||||||
(Expression::Fail(FailDef{
|
(Expression::Fail(FailDef{
|
||||||
pos: pos,
|
pos: pos,
|
||||||
message: Box::new(msg),
|
message: Box::new(msg),
|
||||||
|
15
std/schema.ucg
Normal file
15
std/schema.ucg
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
let one_of = module {
|
||||||
|
val=NULL,
|
||||||
|
types=[],
|
||||||
|
} => {
|
||||||
|
let reducer = macro(acc, t) => acc{
|
||||||
|
ok = acc.ok || (acc.val is t),
|
||||||
|
};
|
||||||
|
let any = macro(val, types) => reduce reducer {ok=false, val=val}, types;
|
||||||
|
|
||||||
|
let result = any(mod.val, mod.types).ok;
|
||||||
|
};
|
||||||
|
|
||||||
|
let must = macro(m, msg) => select m.result, fail msg, {
|
||||||
|
true = m.result,
|
||||||
|
};
|
12
std/tests/schema_test.ucg
Normal file
12
std/tests/schema_test.ucg
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
let t = import "std/testing.ucg".asserts{};
|
||||||
|
let schema = import "std/schema.ucg";
|
||||||
|
|
||||||
|
assert t.ok{
|
||||||
|
test = schema.must(schema.one_of{val=1, types=["float", "int"]}, "Must be a float or an int"),
|
||||||
|
desc = "1 is an int",
|
||||||
|
};
|
||||||
|
|
||||||
|
assert t.not_ok{
|
||||||
|
test = schema.one_of{val=1, types=["float", "str"]}.result,
|
||||||
|
desc = "1 is not a float or string",
|
||||||
|
};
|
Loading…
x
Reference in New Issue
Block a user