-
Notifications
You must be signed in to change notification settings - Fork 0
/
plist.gleam
101 lines (94 loc) · 2.33 KB
/
plist.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import gleam/io
import gleam/list
import gleam/dict
import gleam/string
import gleam/regex
import gleam/result
import gleam/int
import gleam/float
import gleam/dynamic
import gleam/option.{type Option, None, Some}
import gleam/order
@target(javascript)
import gleam/javascript/promise
import l4u/sys_bridge.{
abspath, catch_ex, console_readline, console_writeln, dbgout1, dbgout2,
dbgout3, deref, dir_exists, do_async, file_exists, get_cwd, internal_form,
l4u_to_native, load_file, make_ref, native_to_l4u, os_cmd, process_dict_get,
process_dict_keys, process_dict_set, reset_ref, set_cwd, throw_err, throw_expr,
}
import l4u/l4u_core.{
description, dump_env, env_set_global, env_set_local, eval, inspect,
main as do_repl, make_new_l4u_core_env, print, pstr, rep, repl,
trace_set_last_funcall, uneval,
}
import l4u/l4u_type.{
type Expr, FALSE, INT, TRUE, native_false, native_nil, native_true,
native_undefined, to_native_dictionary, unsafe_corece,
}
import l4u/l4u_core as l4u
fn plist_get_value(plist: List(Expr), key: String) -> Option(Expr) {
case plist {
[] -> None
[k, v, ..rest] -> {
let str_k = pstr(k)
case str_k == key {
True -> Some(v)
_ -> plist_get_value(rest, key)
}
}
_ -> {
throw_err("plist_get_value: plist is not a list of pairs")
None
}
}
}
pub fn plist_get_str(plist: List(Expr), key: String) -> Option(String) {
case plist_get_value(plist, key) {
Some(x) -> Some(pstr(x))
_ -> None
}
}
pub fn plist_get_int(plist: List(Expr), key: String) -> Option(Int) {
case plist_get_value(plist, key) {
Some(INT(i)) -> Some(i)
_ -> None
}
}
pub fn plist_get_bool(plist: List(Expr), key: String) -> Option(Bool) {
case plist_get_value(plist, key) {
Some(TRUE) -> Some(True)
Some(FALSE) -> Some(False)
_ -> None
}
}
pub fn plist_get_str_with_default(
plist: List(Expr),
key: String,
default: String,
) -> String {
case plist_get_str(plist, key) {
Some(x) -> x
_ -> default
}
}
pub fn plist_get_int_with_default(
plist: List(Expr),
key: String,
default: Int,
) -> Int {
case plist_get_int(plist, key) {
Some(x) -> x
_ -> default
}
}
pub fn plist_get_bool_with_default(
plist: List(Expr),
key: String,
default: Bool,
) -> Bool {
case plist_get_bool(plist, key) {
Some(x) -> x
_ -> default
}
}