-module(tools@calculator). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]). -export([tool/0]). -export_type([operation/0, calculator/0]). -type operation() :: add | subtract | multiply | divide. -type calculator() :: {calculator, operation(), float(), float()}. -file("/home/ethanthoma/projects/flow/src/tools/calculator.gleam", 48). -spec decoder(gleam@dynamic:dynamic_()) -> {ok, calculator()} | {error, list(gleam@dynamic:decode_error())}. decoder(Input) -> _pipe = Input, (gleam@dynamic:decode3( fun(Field@0, Field@1, Field@2) -> {calculator, Field@0, Field@1, Field@2} end, gleam@dynamic:field( <<"operation"/utf8>>, fun(A) -> gleam@result:'try'( gleam@dynamic:string(A), fun(A@1) -> case A@1 of <<"add"/utf8>> -> {ok, add}; <<"subtract"/utf8>> -> {ok, subtract}; <<"multiply"/utf8>> -> {ok, multiply}; <<"divide"/utf8>> -> {ok, divide}; _ -> {error, [{decode_error, <<"one of add, subtract, multiple, and divide"/utf8>>, A@1, []}]} end end ) end ), gleam@dynamic:field( <<"a"/utf8>>, gleam@dynamic:any([fun gleam@dynamic:float/1, fun(X) -> _pipe@1 = X, _pipe@2 = gleam@dynamic:int(_pipe@1), gleam@result:map(_pipe@2, fun gleam@int:to_float/1) end]) ), gleam@dynamic:field( <<"b"/utf8>>, gleam@dynamic:any( [fun gleam@dynamic:float/1, fun(X@1) -> _pipe@3 = X@1, _pipe@4 = gleam@dynamic:int(_pipe@3), gleam@result:map(_pipe@4, fun gleam@int:to_float/1) end] ) ) ))(_pipe). -file("/home/ethanthoma/projects/flow/src/tools/calculator.gleam", 36). -spec apply(gleam@dynamic:dynamic_()) -> {ok, starflow@tool:tool_result()} | {error, list(gleam@dynamic:decode_error())}. apply(Input) -> gleam@result:map( decoder(Input), fun(_use0) -> {calculator, Op, A, B} = _use0, _pipe = case Op of add -> A + B; subtract -> A - B; multiply -> A * B; divide -> case B of +0.0 -> +0.0; -0.0 -> -0.0; Gleam@denominator -> A / Gleam@denominator end end, {number, _pipe} end ). -file("/home/ethanthoma/projects/flow/src/tools/calculator.gleam", 19). -spec tool() -> starflow@tool:tool(). tool() -> {tool, <<"calculator"/utf8>>, <<"Performs basic arithmetic calculations"/utf8>>, starflow@schema:object( [starflow@schema:required( <<"operation"/utf8>>, starflow@schema:enum( [<<"add"/utf8>>, <<"subtract"/utf8>>, <<"multiply"/utf8>>, <<"divide"/utf8>>] ) ), starflow@schema:required(<<"a"/utf8>>, starflow@schema:number()), starflow@schema:required(<<"b"/utf8>>, starflow@schema:number())] ), starflow@schema:number(), fun apply/1}.