From 7f7cacc826099946a190474ae4fb6d95c64ee9b7 Mon Sep 17 00:00:00 2001 From: Ian Harrigan Date: Wed, 29 May 2024 10:36:53 +0200 Subject: [PATCH] throw exception when invalid enum value (should be applied to all enums) --- haxe/ui/constants/ScrollMode.hx | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/haxe/ui/constants/ScrollMode.hx b/haxe/ui/constants/ScrollMode.hx index e6dee5047..b88a54568 100644 --- a/haxe/ui/constants/ScrollMode.hx +++ b/haxe/ui/constants/ScrollMode.hx @@ -1,7 +1,27 @@ package haxe.ui.constants; -enum abstract ScrollMode(String) from String to String { - var NORMAL = "normal"; - var DRAG = "drag"; - var INERTIAL = "inertial"; -} +import haxe.ui.util.Variant; + +enum abstract ScrollMode(String) to String { + var NORMAL = "normal"; + var DRAG = "drag"; + var INERTIAL = "inertial"; + var HYBRID = "hybrid"; + + @:from public static function fromString(s:String):ScrollMode { + return switch (s.toLowerCase()) { + case "normal": NORMAL; + case "drag": DRAG; + case "inertial": INERTIAL; + case "hybrid": HYBRID; + case _: throw "invalid ScrollMode enum value '" + s + "'"; + } + } + + @:from public static function fromVariant(v:Variant):ScrollMode { + if (v == null || v.isNull) { + return null; + } + return fromString(v.toString()); + } +} \ No newline at end of file