Factorio: improve error message for config validation (#4421)

This commit is contained in:
Sam Merritt
2025-01-13 00:52:21 -08:00
committed by GitHub
parent 4c734b467f
commit 0f1c119c76
2 changed files with 51 additions and 2 deletions

View File

@@ -3,13 +3,23 @@ from __future__ import annotations
from dataclasses import dataclass
import typing
from schema import Schema, Optional, And, Or
from schema import Schema, Optional, And, Or, SchemaError
from Options import Choice, OptionDict, OptionSet, DefaultOnToggle, Range, DeathLink, Toggle, \
StartInventoryPool, PerGameCommonOptions, OptionGroup
# schema helpers
FloatRange = lambda low, high: And(Or(int, float), lambda f: low <= f <= high)
class FloatRange:
def __init__(self, low, high):
self._low = low
self._high = high
def validate(self, value):
if not isinstance(value, (float, int)):
raise SchemaError(f"should be instance of float or int, but was {value!r}")
if not self._low <= value <= self._high:
raise SchemaError(f"{value} is not between {self._low} and {self._high}")
LuaBool = Or(bool, And(int, lambda n: n in (0, 1)))