-
Notifications
You must be signed in to change notification settings - Fork 0
/
gluple.gleam
57 lines (49 loc) · 1.75 KB
/
gluple.gleam
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import gleam/dynamic.{type DecodeError, type Dynamic, DecodeError}
import gleam/int
import gleam/string
pub fn tuple_size(tuple: t) -> Result(Int, List(DecodeError)) {
case is_tuple(tuple) {
True -> Ok(do_tuple_size(tuple))
False -> Error([DecodeError("Tuple", string.inspect(tuple), [])])
}
}
pub fn tuple_to_list(tuple: t) -> Result(List(Dynamic), List(DecodeError)) {
case is_tuple(tuple) {
True -> Ok(do_tuple_to_list(tuple))
False -> Error([DecodeError("Tuple", string.inspect(tuple), [])])
}
}
pub fn tuple_element(tuple: t, index: Int) -> Result(el, List(DecodeError)) {
case is_tuple(tuple) {
True -> {
let tuple_size = do_tuple_size(tuple)
case index >= 0 && index < tuple_size {
True -> Ok(element(index + 1, tuple))
False ->
Error([
DecodeError(
"Tuple size " <> int.to_string(tuple_size),
"Desired index " <> int.to_string(index),
[],
),
])
}
}
False -> Error([DecodeError("Tuple", string.inspect(tuple), [])])
}
}
@external(erlang, "erlang", "is_tuple")
@external(javascript, "./gluple_ffi.mjs", "isTuple")
pub fn is_tuple(maybe_tuple: t) -> Bool
@external(erlang, "erlang", "tuple_to_list")
@external(javascript, "./gluple_ffi.mjs", "tupleToList")
fn do_tuple_to_list(maybe_tuple: t) -> List(Dynamic)
@external(erlang, "erlang", "list_to_tuple")
@external(javascript, "./gluple_ffi.mjs", "listToTuple")
pub fn list_to_tuple(list: List(x)) -> t
@external(erlang, "erlang", "tuple_size")
@external(javascript, "./gluple_ffi.mjs", "tupleSize")
fn do_tuple_size(tuple: t) -> Int
@external(erlang, "erlang", "element")
@external(javascript, "./gluple_ffi.mjs", "element")
fn element(index: Int, tuple: t) -> el