stutter / values.v
168 lines · 152 sloc · 5.61 KB · 0c355e9d3499f38b7165fd530ac3b1c25dbf93b2
Raw
1module stutter
2
3import vile.scheme
4
5// bool_of requires a Vile boolean.
6pub fn bool_of(v scheme.Value, what string) !bool {
7 if v.kind() != .boolean { return error('config: ${what} must be #t or #f') }
8 return v.as_boolean()!
9}
10
11// str_of requires a quoted string.
12pub fn str_of(v scheme.Value, what string) !string {
13 if v.kind() != .string { return error('config: ${what} must be a quoted string') }
14 return v.as_string()!
15}
16
17// strs_of accepts a string, vector, or proper list.
18pub fn strs_of(v scheme.Value, what string) ![]string {
19 mut result := []string{}
20 match v.kind() {
21 .nil {}
22 .string { result << v.as_string()! }
23 .pair {
24 for item in v.list_items()! { result << str_of(item, what)! }
25 }
26 .vector {
27 for item in v.vector_items()! { result << str_of(item, what)! }
28 }
29 else { return error('config: ${what} must be a string, vector, or list of strings') }
30 }
31 return result
32}
33
34// int_of requires an integer representable by V's int type.
35pub fn int_of(v scheme.Value, what string) !int {
36 if v.kind() != .integer { return error('config: ${what} must be an integer') }
37 n := v.as_integer()!
38 if n < -2147483648 || n > 2147483647 {
39 return error('config: ${what} integer is too large')
40 }
41 return int(n)
42}
43
44// float_of accepts a Vile integer, rational, or real.
45pub fn float_of(v scheme.Value, what string) !f64 {
46 return match v.kind() {
47 .real, .rational { v.as_real()! }
48 .integer { f64(v.as_integer()!) }
49 else { error('config: ${what} must be a number') }
50 }
51}
52
53// ratio_of requires a finite number between zero and one.
54pub fn ratio_of(v scheme.Value, what string) !f64 {
55 f := float_of(v, what)!
56 if !(f >= 0.0 && f <= 1.0) { return error('config: ${what} must be between 0 and 1') }
57 return f
58}
59
60// count_of requires a V int no smaller than low.
61pub fn count_of(v scheme.Value, what string, low int) !int {
62 n := int_of(v, what)!
63 if n < low { return error('config: ${what} must be at least ${low}') }
64 return n
65}
66
67// dir_path requires a nonempty directory name and adds a trailing slash.
68pub fn dir_path(what string, value string) !string {
69 if value == '' { return error('config: ${what} must not be empty') }
70 return if value.ends_with('/') { value } else { value + '/' }
71}
72
73// cell_of accepts one Unicode character, optionally allowing an empty string.
74pub fn cell_of(v scheme.Value, what string, may_be_empty bool) !string {
75 text := str_of(v, what)!
76 if text.runes().len != 1 && !(may_be_empty && text == '') {
77 return error('config: ${what} must be one character')
78 }
79 return text
80}
81
82// string_option rejects NUL bytes and, unless allowed, empty strings.
83pub fn string_option(v scheme.Value, key string, allow_empty bool) !string {
84 text := str_of(v, '${key}')!
85 if text.contains('\x00') || (!allow_empty && text == '') {
86 return error('${key} expects a nonempty string without NUL')
87 }
88 return text
89}
90
91// int_option requires an integer within an inclusive range.
92pub fn int_option(v scheme.Value, key string, low int, high int) !int {
93 if v.kind() != .integer || v.as_integer()! < low || v.as_integer()! > high {
94 return error('${key} expects an integer from ${low} to ${high}')
95 }
96 return int(v.as_integer()!)
97}
98
99// keyword_args reads quoted symbol/value pairs, preserving order and repeated
100// names for application-specific native procedures.
101pub fn keyword_args(form string, args []scheme.Value) !([]string, []scheme.Value) {
102 if args.len % 2 != 0 { return error('config: ${form} keys and values come in pairs') }
103 mut keys := []string{}
104 mut values := []scheme.Value{}
105 for i := 0; i < args.len; i += 2 {
106 if args[i].kind() != .symbol {
107 return error('config: ${form} expected a quoted option name')
108 }
109 keys << args[i].symbol_name()!
110 values << args[i + 1]
111 }
112 return keys, values
113}
114
115// pairs validates an option set and rejects duplicates. An empty allowed list
116// accepts any key; applications can then validate using apply_values.
117pub fn pairs(args []scheme.Value, allowed []string) !map[string]scheme.Value {
118 keys, values := keyword_args('options', args)!
119 mut result := map[string]scheme.Value{}
120 for i, key in keys {
121 if allowed.len > 0 && key !in allowed { return error('unknown option ${key}') }
122 if key in result { return error('duplicate option ${key}') }
123 result[key] = values[i]
124 }
125 return result
126}
127
128// apply_options maps option names to fields, allowing repeated settings. It returns the field names explicitly set by this call.
129pub fn apply_options[T](mut target T, args []scheme.Value, allowed []string, nonempty []string) ![]string {
130 keys, values := keyword_args('options', args)!
131 mut specified := []string{}
132 for i, key in keys {
133 field := key.replace('-', '_')
134 if field !in allowed { return error('unknown setting ${key}') }
135 apply_values(mut target, {
136 field: values[i]
137 }, nonempty)!
138 specified << field
139 }
140 return specified
141}
142
143// apply_values checks and assigns a map of options to the target's fields.
144pub fn apply_values[T](mut target T, values map[string]scheme.Value, nonempty []string) ! {
145 for key, value in values {
146 name := key.replace('-', '_')
147 mut found := false
148 $for field in T.fields {
149 if field.name == name {
150 found = true
151 $if field.typ is string {
152 target.$(field.name) = string_option(value, key, name !in nonempty)!
153 } $else $if field.typ is bool {
154 target.$(field.name) = bool_of(value, '${key}')!
155 } $else $if field.typ is int {
156 target.$(field.name) = int_of(value, '${key}')!
157 } $else $if field.typ is f64 {
158 target.$(field.name) = float_of(value, '${key}')!
159 } $else $if field.typ is []string {
160 target.$(field.name) = strs_of(value, '${key}')!
161 } $else {
162 return error('unsupported config field ${name}')
163 }
164 }
165 }
166 if !found { return error('unknown setting ${key}') }
167 }
168}
169