forked from rust-bitcoin/rust-bitcoin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.sh
executable file
·149 lines (120 loc) · 2.89 KB
/
api.sh
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/env bash
#
# Script for querying the API.
#
# Shellcheck can't search dynamic paths
# shellcheck source=/dev/null
set -euo pipefail
file="" # File name of the all-features API text file.
crate_full_name="" # Full crate name using underscores e.g., `bitcoin_primitives`.
crate="" # Short name e.g., `primitives`.
# Set to false to turn off verbose output.
flag_verbose=false
usage() {
cat <<EOF
Usage:
./api.sh CRATE COMMAND
CRATE
- hashes bitcoin_hashes
- io bitcoin-io
- primitives bitcoin-primitives
- units bitcoin-units
CMD
- types Show all public types (structs and enums)
- types_no_err Show all public types (structs and enums) excluding error types.
EOF
}
main() {
if [ "$#" -lt 1 ]; then
usage
exit 1
fi
local _crate="${1:---help}"
if [[ "$_crate" == "-h" || "$_crate" == "--help" ]]; then
usage
exit 1
fi
if [ "$#" -lt 2 ]; then
say_err "Missing COMMAND"
usage
exit 1
fi
local _cmd="$2"
check_required_commands
case $_crate in
hashes)
crate_full_name="bitcoin_hashes"
;;
io)
crate_full_name="bitcoin_io"
;;
primitives)
crate_full_name="bitcoin_primitives"
;;
units)
crate_full_name="bitcoin_units"
;;
*)
say_err "unsupported crate: $_crate"
usage
exit 1
esac
crate=$_crate
file="./api/$crate/all-features.txt"
verbose_say "Running command '$_cmd' on crate '$crate'"
case $_cmd in
types)
structs_and_enums
;;
types_no_err)
structs_and_enums_no_err
;;
traits)
traits
;;
*)
err "Error: unknown cmd $_cmd"
;;
esac
}
# Print all public structs and enums.
structs_and_enums() {
grep -oP 'pub (struct|enum) \K[\w:]+(?:<[^>]+>)?(?=\(|;| |$)' "$file" | sed "s/^${crate_full_name}:://"
}
# Print all public structs and enums excluding error types.
structs_and_enums_no_err() {
grep -oP 'pub (struct|enum) \K[\w:]+(?:<[^>]+>)?(?=\(|;| |$)' "$file" | sed "s/^${crate_full_name}:://" | grep -v Error
}
# Print all public traits.
traits() {
grep -oP '^pub trait \K[\w:]+' "$file" | sed "s/^${crate_full_name}:://" | sed 's/:$//'
}
# Check all the commands we use are present in the current environment.
check_required_commands() {
need_cmd grep
}
say() {
echo "api: $1"
}
say_err() {
say "$1" >&2
}
verbose_say() {
if [ "$flag_verbose" = true ]; then
say "$1"
fi
}
err() {
echo "$1" >&2
exit 1
}
need_cmd() {
if ! command -v "$1" > /dev/null 2>&1
then err "need '$1' (command not found)"
fi
}
#
# Main script
#
main "$@"
exit 0