Current section
Files
Jump to
Current section
Files
src/cangaroo/internal.gleam
//// Internal helper functions for result decoding and status checking.
////
//// This module contains utility functions for converting raw Erlang/Elixir
//// return values into Gleam `Result` types with proper error handling.
//// This module is not part of the public API.
import cangaroo/errors
import gleam/dynamic
import gleam/dynamic/decode
import gleam/erlang/atom
pub fn check_status(status: atom.Atom) -> Result(Nil, errors.CanError) {
case atom.to_string(status) {
"ok" -> Ok(Nil)
other -> Error(errors.UnknownError(other))
}
}
pub fn decode_result(raw: dynamic.Dynamic) -> Result(Nil, errors.CanError) {
case decode.run(raw, atom.decoder()) {
Ok(atom_val) ->
case atom.to_string(atom_val) {
"ok" -> Ok(Nil)
other -> Error(errors.UnknownError(other))
}
Error(_) -> decode_as_error_tuple(raw)
}
}
fn decode_as_error_tuple(raw: dynamic.Dynamic) -> Result(Nil, errors.CanError) {
let tuple_decoder = {
use tag <- decode.subfield([0], atom.decoder())
use reason <- decode.subfield([1], atom.decoder())
decode.success(#(atom.to_string(tag), atom.to_string(reason)))
}
case decode.run(raw, tuple_decoder) {
Ok(#(tag, reason)) ->
case tag, reason {
"error", "ebound" -> Error(errors.InterfaceBoundError("ebound"))
"error", r -> Error(errors.UnknownError(r))
_, _ -> Error(errors.UnknownError("unparsable_return"))
}
Error(_) -> Error(errors.UnknownError("unparsable_return"))
}
}