Skip to content

Commit ada403b

Browse files
committed
refactor: Revamp naming
1 parent a9457cb commit ada403b

File tree

10 files changed

+77
-59
lines changed

10 files changed

+77
-59
lines changed

basalt.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
[package]
2+
lang = 'bash'
3+
type = 'lib'
24
name = 'bash-toml'
3-
slug = 'btoml'
5+
slug = 'bash_toml'
46
version = '0.2.0'
57
authors = ['Edwin Kofler" <edwin@kofler.dev>']
68
description = 'A kickass Toml parser written in pure Bash'

pkg/src/public/bash-toml-quick.sh

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# shellcheck shell=bash
22

3-
btoml.quick_string_get() {
4-
unset REPLY; REPLY=
3+
bash_toml.quick_string_get() {
4+
unset -v REPLY; REPLY=
55
local toml_file="$1"
66
local key_name="$2"
77

88
if [ ! -f "$toml_file" ]; then
9-
btoml.error "File '$toml_file' not found"
9+
bash_toml.error "File '$toml_file' not found"
1010
return 1
1111
fi
1212

@@ -33,12 +33,12 @@ btoml.quick_string_get() {
3333
REPLY="${BASH_REMATCH[1]}"
3434
else
3535
# This should not happen due to the '[[ $line == *"$key_name"*=* ]]' check above
36-
btoml.error "Could not find key '$key_name' in file '$toml_file'"
36+
bash_toml.error "Could not find key '$key_name' in file '$toml_file'"
3737
return 1
3838
fi
3939
}
4040

41-
btoml.quick_array_get() {
41+
bash_toml.quick_array_get() {
4242
unset REPLY; declare -ga REPLY=()
4343
local toml_file="$1"
4444
local key_name="$2"
@@ -47,7 +47,7 @@ btoml.quick_array_get() {
4747
# ensure.nonzero 'key_name'
4848

4949
if [ ! -f "$toml_file" ]; then
50-
btoml.error "File '$toml_file' does not exist"
50+
bash_toml.error "File '$toml_file' does not exist"
5151
return 2
5252
fi
5353

@@ -98,17 +98,21 @@ btoml.quick_array_get() {
9898
if [[ ${REPLY[$i]} =~ $regex ]]; then
9999
REPLY[$i]="${BASH_REMATCH[1]}"
100100
else
101-
btoml.error "Key '$key_name' in file '$toml_file' is not valid"
101+
bash_toml.error "Key '$key_name' in file '$toml_file' is not valid"
102102
return 2
103103
fi
104104
done
105105
else
106-
btoml.error "Key '$key_name' in file '$toml_file' must be set to an array that spans one line"
106+
bash_toml.error "Key '$key_name' in file '$toml_file' must be set to an array that spans one line"
107107
return 2
108108
fi
109109
}
110110

111-
btoml.quick_array_append() {
111+
bash_toml.quick_string_set() {
112+
:
113+
}
114+
115+
bash_toml.quick_array_append() {
112116
local toml_file="$1"
113117
local key_name="$2"
114118
local key_value="$3"
@@ -117,15 +121,15 @@ btoml.quick_array_append() {
117121
# ensure.nonzero 'key_value'
118122

119123
if [ ! -f "$toml_file" ]; then
120-
btoml.error "File '$toml_file' does not exist"
124+
bash_toml.error "File '$toml_file' does not exist"
121125
return 2
122126
fi
123127

124128
if util.get_toml_array "$toml_file" 'dependencies'; then
125129
local name=
126130
for name in "${REPLY[@]}"; do
127131
if [ "${name%@*}" = "${key_value%@*}" ]; then
128-
btoml.error "A version of '${name%@*}' is already installed. Skipping"
132+
bash_toml.error "A version of '${name%@*}' is already installed. Skipping"
129133
return 2
130134
fi
131135
done; unset name
@@ -140,20 +144,20 @@ btoml.quick_array_append() {
140144
rm "$toml_file.bak"
141145
fi
142146
else
143-
btoml.error "Key 'dependencies' not found in file '$toml_file'"
147+
bash_toml.error "Key 'dependencies' not found in file '$toml_file'"
144148
return 2
145149
fi
146150
}
147151

148-
btoml.quick_array_remove() {
152+
bash_toml.quick_array_remove() {
149153
local toml_file="$1"
150154
local key_value="$2"
151155

152156
# ensure.nonzero 'toml_file'
153157
# ensure.nonzero 'key_value'
154158

155159
if [ ! -f "$toml_file" ]; then
156-
btoml.error "File '$toml_file' does not exist"
160+
bash_toml.error "File '$toml_file' does not exist"
157161
return 2
158162
fi
159163

@@ -170,7 +174,7 @@ btoml.quick_array_remove() {
170174
done; unset -v name
171175

172176
if [ "$does_exist" != 'yes' ]; then
173-
btoml.error "The package '$key_value' is not currently a dependency"
177+
bash_toml.error "The package '$key_value' is not currently a dependency"
174178
return 2
175179
fi
176180

@@ -191,7 +195,11 @@ btoml.quick_array_remove() {
191195
done < "$toml_file.bak" > "$toml_file"
192196
rm "$toml_file.bak"
193197
else
194-
btoml.error "Key 'dependencies' not found in file '$toml_file'"
198+
bash_toml.error "Key 'dependencies' not found in file '$toml_file'"
195199
return 2
196200
fi
197201
}
202+
203+
bash_toml.quick_array_replace() {
204+
:
205+
}

pkg/src/util/helper.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# shellcheck shell=bash
2+
3+
bash_toml.helper_parse() {
4+
unset -v REPLY; declare -ga REPLY=()
5+
}

pkg/src/util/init_append.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
# shellcheck shell=bash
22

3-
btoml.init_key_string() {
3+
bash_toml.init_key_string() {
44
BASH_TOML_KEY_STRING="$1"
55
}
66

7-
btoml.append_key_string() {
7+
bash_toml.append_key_string() {
88
BASH_TOML_KEY_STRING+="$1"
99
}
1010

11-
btoml.init_value_string() {
11+
bash_toml.init_value_string() {
1212
BASH_TOML_KEY_VALUE_STRING=
1313
}
1414

15-
btoml.append_value_string() {
15+
bash_toml.append_value_string() {
1616
BASH_TOML_KEY_VALUE_STRING+="$1"
1717
}

pkg/src/util/is.sh

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,46 @@
11
# shellcheck shell=bash
22

3-
btoml.is.whitespace() {
3+
bash_toml.is.whitespace() {
44
if [[ "$1" == @($'\u0009'|$'\u0020') ]]; then
55
return 0
66
else
77
return 1
88
fi
99
}
1010

11-
btoml.is.newline() {
11+
bash_toml.is.newline() {
1212
if [[ "$1" == @($'\u000A'|$'\u0D0A') ]]; then
1313
return 0
1414
else
1515
return 1
1616
fi
1717
}
1818

19-
btoml.is.table() {
19+
bash_toml.is.table() {
2020
if [[ "$1" == \[ ]]; then
2121
return 0
2222
else
2323
return 1
2424
fi
2525
}
2626

27-
btoml.is.double_quote() {
27+
bash_toml.is.double_quote() {
2828
if [[ "$1" == \" ]]; then
2929
return 0
3030
else
3131
return 1
3232
fi
3333
}
3434

35-
btoml.is.single_quote() {
35+
bash_toml.is.single_quote() {
3636
if [[ "$1" == \' ]]; then
3737
return 0
3838
else
3939
return 1
4040
fi
4141
}
4242

43-
btoml.is.backslash() {
43+
bash_toml.is.backslash() {
4444
# shellcheck disable=SC1003
4545
if [[ "$1" == \\ ]]; then
4646
return 0
@@ -49,7 +49,7 @@ btoml.is.backslash() {
4949
fi
5050
}
5151

52-
btoml.is.control_character() {
52+
bash_toml.is.control_character() {
5353
# shellcheck disable=SC1003
5454
if [[ "$1" == [[:cntrl:]] ]]; then
5555
return 0
@@ -58,7 +58,7 @@ btoml.is.control_character() {
5858
fi
5959
}
6060

61-
btoml.is.hex_digit() {
61+
bash_toml.is.hex_digit() {
6262
# shellcheck disable=SC1003
6363
if [[ "$1" == [[:xdigit:]] ]]; then
6464
return 0
@@ -67,7 +67,7 @@ btoml.is.hex_digit() {
6767
fi
6868
}
6969

70-
btoml.is.octothorp() {
70+
bash_toml.is.octothorp() {
7171
# shellcheck disable=SC1003
7272
if [[ "$1" == \# ]]; then
7373
return 0
@@ -76,15 +76,15 @@ btoml.is.octothorp() {
7676
fi
7777
}
7878

79-
btoml.is.valid_bare_key_char() {
79+
bash_toml.is.valid_bare_key_char() {
8080
if [[ "$1" == [A-Za-z0-9_-] ]]; then
8181
return 0
8282
else
8383
return 1
8484
fi
8585
}
8686

87-
btoml.is.equals_sign() {
87+
bash_toml.is.equals_sign() {
8888
if [[ "$1" == = ]]; then
8989
return 0
9090
else

pkg/src/util/util.sh

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
11
# shellcheck shell=bash
22

3-
declare -gA BASH_TOML_ERRORS=(
4-
[INTERNAL_ERROR]='Internal error'
5-
[NOT_IMPLEMENTED]='TOML feature has not been implemented'
6-
[UNEXPECTED_BRANCH]='This branch was not supposed to be activated. Please submit an issue'
7-
[UNICODE_INVALID]='The resulting unicode code point was invalid'
8-
[KEY_ABSENT]='Key does not have a value'
9-
[UNEXPECTED_EOF]='Unexpected end of line'
10-
[UNEXPECTED_NEWLINE]='Unexpected newline'
11-
[UNEXPECTED_CHARACTER]='An unexpected character was encountered' # Generalization of any of the following errors
12-
[KEY_INVALID]='The key is not valid'
13-
[VALUE_INVALID]='The value could not be parsed'
14-
[VALUE_STRING_INVALID]='The string value could not be parsed'
15-
)
3+
# @description Initialize bash-toml
4+
bash_toml.util_init() {
5+
declare -gA BASH_TOML_ERRORS=(
6+
[INTERNAL_ERROR]='Internal error'
7+
[NOT_IMPLEMENTED]='TOML feature has not been implemented'
8+
[UNEXPECTED_BRANCH]='This branch was not supposed to be activated. Please submit an issue'
9+
[UNICODE_INVALID]='The resulting unicode code point was invalid'
10+
[KEY_ABSENT]='Key does not have a value'
11+
[UNEXPECTED_EOF]='Unexpected end of line'
12+
[UNEXPECTED_NEWLINE]='Unexpected newline'
13+
[UNEXPECTED_CHARACTER]='An unexpected character was encountered' # Generalization of any of the following errors
14+
[KEY_INVALID]='The key is not valid'
15+
[VALUE_INVALID]='The value could not be parsed'
16+
[VALUE_STRING_INVALID]='The string value could not be parsed'
17+
)
1618

17-
declare -a BASH_TOKEN_HISTORY=()
19+
declare -a BASH_TOKEN_HISTORY=()
20+
}
1821

1922
# @description Appends to token history for improved error insight
20-
btoml.token_history_add() {
23+
bash_toml.util_token_history_add() {
2124
local str=
2225
printf -v str '%s' "$mode ($char) at $PARSER_LINE_NUMBER:$PARSER_COLUMN_NUMBER"
2326

@@ -32,7 +35,7 @@ btoml.token_history_add() {
3235
fi
3336
}
3437

35-
btoml.parse_fail() {
38+
bash_toml.parse_fail() {
3639
local error_key="$1"
3740
local error_context="$2"
3841

@@ -62,6 +65,6 @@ btoml.parse_fail() {
6265
fi
6366
}
6467

65-
btoml.error() {
68+
bash_toml.error() {
6669
printf '%s\n' "Error: $1" >&2
6770
}

scripts/linter.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# shellcheck shell=bash
22

3-
file='./pkg/lib/cmd/bash-btoml.sh'
4-
grep -n -B0 -A1 -e 'btoml.parse_fail' "$file" \
3+
file='./pkg/lib/cmd/bash-bash_toml.sh'
4+
grep -n -B0 -A1 -e 'bash_toml.parse_fail' "$file" \
55
| grep -vP '(^[0-9]*:|^--$)' \
66
| awk '
77
BEGIN {

tests/quick-array.bats

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
load './util/init.sh'
44

55
@test "Quick array properly fails" {
6-
run btoml.quick_array_get "$BASALT_PACKAGE_DIR/tests/testdata/array/file1.toml" \
6+
run bash_toml.quick_array_get "$BASALT_PACKAGE_DIR/tests/testdata/array/file1.toml" \
77
'badkey'
88

99
assert_failure
1010
}
1111

1212
@test "Quick array get" {
1313
for n in {1..7}; do
14-
btoml.quick_array_get "$BASALT_PACKAGE_DIR/tests/testdata/array/file1.toml" \
14+
bash_toml.quick_array_get "$BASALT_PACKAGE_DIR/tests/testdata/array/file1.toml" \
1515
"key0${n}"
1616
assert [ "${#REPLY[@]}" -eq 0 ]
1717
done
@@ -21,7 +21,7 @@ load './util/init.sh'
2121
@test "Quick array get 2" {
2222
for m in {1..2}; do
2323
for n in {1..7}; do
24-
btoml.quick_array_get "$BASALT_PACKAGE_DIR/tests/testdata/array/file1.toml" \
24+
bash_toml.quick_array_get "$BASALT_PACKAGE_DIR/tests/testdata/array/file1.toml" \
2525
"key${m}${n}"
2626
assert [ "${#REPLY[@]}" -eq 1 ]
2727
assert [ -z "${REPLY[0]}" ]
@@ -32,7 +32,7 @@ load './util/init.sh'
3232
@test "Quick array get 3" {
3333
for m in {3..10}; do
3434
for n in {1..7}; do
35-
btoml.quick_array_get "$BASALT_PACKAGE_DIR/tests/testdata/array/file1.toml" \
35+
bash_toml.quick_array_get "$BASALT_PACKAGE_DIR/tests/testdata/array/file1.toml" \
3636
"key${m}${n}"
3737
assert [ "${#REPLY[@]}" -eq 2 ]
3838
assert [ "${REPLY[0]}" = 'a' ]
@@ -43,7 +43,7 @@ load './util/init.sh'
4343

4444
@test "Quick array get multiline" {
4545
for n in {0..1}; do
46-
btoml.quick_array_get "$BASALT_PACKAGE_DIR/tests/testdata/array/file2.toml" \
46+
bash_toml.quick_array_get "$BASALT_PACKAGE_DIR/tests/testdata/array/file2.toml" \
4747
"key${n}"
4848
assert [ "${#REPLY[@]}" -eq 1 ]
4949
assert [ "${REPLY[0]}" = 'multiline' ]

tests/quick-string.bats

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ load './util/init.sh'
55
@test "quick string get" {
66
for tens in {0..1}; do
77
for ones in {1..7}; do
8-
btoml.quick_string_get "$BASALT_PACKAGE_DIR/tests/testdata/string/file1.toml" \
8+
bash_toml.quick_string_get "$BASALT_PACKAGE_DIR/tests/testdata/string/file1.toml" \
99
"key${tens}${ones}"
1010
assert [ "$REPLY" = "value${ones}" ]
1111
done

0 commit comments

Comments
 (0)