-module(l4u@plist). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]). -export([plist_get_str/2, plist_get_int/2, plist_get_bool/2, plist_get_str_with_default/3, plist_get_int_with_default/3, plist_get_bool_with_default/3]). -spec plist_get_value(list(l4u@l4u_type:expr()), binary()) -> gleam@option:option(l4u@l4u_type:expr()). plist_get_value(Plist, Key) -> case Plist of [] -> none; [K, V | Rest] -> Str_k = l4u@l4u_core:pstr(K), case Str_k =:= Key of true -> {some, V}; _ -> plist_get_value(Rest, Key) end; _ -> l4u@sys_bridge_ffi:throw_err( <<"plist_get_value: plist is not a list of pairs"/utf8>> ), none end. -spec plist_get_str(list(l4u@l4u_type:expr()), binary()) -> gleam@option:option(binary()). plist_get_str(Plist, Key) -> case plist_get_value(Plist, Key) of {some, X} -> {some, l4u@l4u_core:pstr(X)}; _ -> none end. -spec plist_get_int(list(l4u@l4u_type:expr()), binary()) -> gleam@option:option(integer()). plist_get_int(Plist, Key) -> case plist_get_value(Plist, Key) of {some, {int, I}} -> {some, I}; _ -> none end. -spec plist_get_bool(list(l4u@l4u_type:expr()), binary()) -> gleam@option:option(boolean()). plist_get_bool(Plist, Key) -> case plist_get_value(Plist, Key) of {some, true} -> {some, true}; {some, false} -> {some, false}; _ -> none end. -spec plist_get_str_with_default(list(l4u@l4u_type:expr()), binary(), binary()) -> binary(). plist_get_str_with_default(Plist, Key, Default) -> case plist_get_str(Plist, Key) of {some, X} -> X; _ -> Default end. -spec plist_get_int_with_default(list(l4u@l4u_type:expr()), binary(), integer()) -> integer(). plist_get_int_with_default(Plist, Key, Default) -> case plist_get_int(Plist, Key) of {some, X} -> X; _ -> Default end. -spec plist_get_bool_with_default( list(l4u@l4u_type:expr()), binary(), boolean() ) -> boolean(). plist_get_bool_with_default(Plist, Key, Default) -> case plist_get_bool(Plist, Key) of {some, X} -> X; _ -> Default end.