-module(diced). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]). -export([dice_from_string/1]). -export_type([roll_variant/0, dice/0, dice_error/0]). -type roll_variant() :: advantage | disadvantage | normal. -type dice() :: {dice, integer(), integer(), roll_variant()}. -type dice_error() :: invalid_amount | invalid_string | invalid_value. -file("C:\\Users\\isaac\\Projects\\sheetr\\diced\\src\\diced.gleam", 28). -spec parse_amount(binary()) -> {ok, integer()} | {error, dice_error()}. parse_amount(Amount) -> case Amount of <<""/utf8>> -> {ok, 1}; _ -> _pipe = Amount, _pipe@1 = gleam_stdlib:parse_int(_pipe), gleam@result:replace_error(_pipe@1, invalid_amount) end. -file("C:\\Users\\isaac\\Projects\\sheetr\\diced\\src\\diced.gleam", 35). -spec parse_value(binary()) -> {ok, {integer(), roll_variant()}} | {error, dice_error()}. parse_value(Value) -> Variant = normal, {Value@1, Variant@1} = case begin _pipe = Value, gleam_stdlib:string_ends_with(_pipe, <<"kh"/utf8>>) end of true -> {begin _pipe@1 = Value, gleam@string:drop_end(_pipe@1, 2) end, advantage}; false -> {Value, Variant} end, {Value@2, Variant@2} = case begin _pipe@2 = Value@1, gleam_stdlib:string_ends_with(_pipe@2, <<"kl"/utf8>>) end of true -> {begin _pipe@3 = Value@1, gleam@string:drop_end(_pipe@3, 2) end, disadvantage}; false -> {Value@1, Variant@1} end, case begin _pipe@4 = Value@2, gleam_stdlib:parse_int(_pipe@4) end of {ok, Num} -> {ok, {Num, Variant@2}}; _ -> {error, invalid_value} end. -file("C:\\Users\\isaac\\Projects\\sheetr\\diced\\src\\diced.gleam", 52). -spec split_dice(binary()) -> {ok, {binary(), binary()}} | {error, dice_error()}. split_dice(String) -> case begin _pipe = String, gleam_stdlib:parse_int(_pipe) end of {ok, _} -> {ok, {String, String}}; _ -> _pipe@1 = String, _pipe@2 = gleam@string:split_once(_pipe@1, <<"d"/utf8>>), gleam@result:replace_error(_pipe@2, invalid_string) end. -file("C:\\Users\\isaac\\Projects\\sheetr\\diced\\src\\diced.gleam", 21). -spec dice_from_string(binary()) -> {ok, dice()} | {error, dice_error()}. dice_from_string(String) -> gleam@result:'try'( split_dice(String), fun(_use0) -> {Amount_string, Value_string} = _use0, gleam@result:'try'( parse_amount(Amount_string), fun(Amount) -> gleam@result:'try'( parse_value(Value_string), fun(_use0@1) -> {Value, Variant} = _use0@1, {ok, {dice, Amount, Value, Variant}} end ) end ) end ).