mirror of
https://github.com/aNNiMON/HotaruFX.git
synced 2024-09-19 14:14:21 +03:00
Add arguments validator
This commit is contained in:
parent
c2dd89ae85
commit
0e9826e3ef
@ -0,0 +1,14 @@
|
|||||||
|
package com.annimon.hotarufx.exceptions;
|
||||||
|
|
||||||
|
import com.annimon.hotarufx.lexer.SourcePosition;
|
||||||
|
|
||||||
|
public class ArgumentsMismatchException extends HotaruRuntimeException {
|
||||||
|
|
||||||
|
public ArgumentsMismatchException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ArgumentsMismatchException(String message, SourcePosition start, SourcePosition end) {
|
||||||
|
super(message, start, end);
|
||||||
|
}
|
||||||
|
}
|
51
app/src/main/java/com/annimon/hotarufx/lib/Validator.java
Normal file
51
app/src/main/java/com/annimon/hotarufx/lib/Validator.java
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
package com.annimon.hotarufx.lib;
|
||||||
|
|
||||||
|
import com.annimon.hotarufx.exceptions.ArgumentsMismatchException;
|
||||||
|
import com.annimon.hotarufx.exceptions.TypeException;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.val;
|
||||||
|
|
||||||
|
@RequiredArgsConstructor(staticName = "with")
|
||||||
|
public class Validator {
|
||||||
|
|
||||||
|
private final Value[] args;
|
||||||
|
|
||||||
|
public Validator check(int expected) {
|
||||||
|
if (args.length != expected) throw new ArgumentsMismatchException(String.format(
|
||||||
|
"%d %s expected, got %d", expected, pluralize(expected), args.length));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Validator checkAtLeast(int expected) {
|
||||||
|
if (args.length < expected) throw new ArgumentsMismatchException(String.format(
|
||||||
|
"At least %d %s expected, got %d", expected, pluralize(expected), args.length));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Validator checkOrOr(int expectedOne, int expectedTwo) {
|
||||||
|
if (expectedOne != args.length && expectedTwo != args.length)
|
||||||
|
throw new ArgumentsMismatchException(String.format(
|
||||||
|
"%d or %d arguments expected, got %d", expectedOne, expectedTwo, args.length));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Validator checkRange(int from, int to) {
|
||||||
|
if (from > args.length || args.length > to)
|
||||||
|
throw new ArgumentsMismatchException(String.format(
|
||||||
|
"From %d to %d arguments expected, got %d", from, to, args.length));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public MapValue requireMapAt(int index) {
|
||||||
|
checkAtLeast(index + 1);
|
||||||
|
val value = args[index];
|
||||||
|
if (value.type() != Types.MAP) {
|
||||||
|
throw new TypeException(String.format("Map required at %d argument", index));
|
||||||
|
}
|
||||||
|
return (MapValue) value;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String pluralize(int count) {
|
||||||
|
return (count == 1) ? "argument" : "arguments";
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user