Skip to content

Commit b9ab932

Browse files
committed
Add script to create types vXY rustdoc
Add a Perl script that creates the rustdocs for a version specific module it `types` e.g., `types/src/v17/mod.rs`.
1 parent 783c782 commit b9ab932

File tree

1 file changed

+138
-0
lines changed

1 file changed

+138
-0
lines changed

contrib/extract.pl

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
#!/usr/bin/perl
2+
# SPDX-License-Identifier: CC0-1.0
3+
#
4+
# Create the `types/src/vXYZ/mod.rs` rusdoc.
5+
6+
use strict;
7+
use warnings;
8+
use Getopt::Long qw(:config no_auto_abbrev);
9+
10+
# The script name.
11+
my $SCRIPT = $0;
12+
13+
# The Bitcoin Core version we are working with.
14+
my $CORE_VERSION = "0.17.1";
15+
# The file holding output of `bitcoin-cli --help`.
16+
my $RPC_HELP_FILE = "types/src/v17/rpc-api.txt";
17+
18+
# Command line options.
19+
my $help = 0;
20+
my $debug = 0;
21+
22+
sub help
23+
{
24+
my ($exitcode) = @_;
25+
26+
print << "EOM";
27+
28+
Usage: $SCRIPT [OPTIONS]
29+
30+
Options:
31+
32+
-d, --debug Display debugging output.
33+
-h, --help Display this help and exit.
34+
35+
Generates the rustdocs for the `types/vX.rs` module. Before running script set CORE_VERSION and RPC_HELP_FILE.
36+
37+
EOM
38+
exit($exitcode);
39+
}
40+
41+
GetOptions(
42+
'd|debug' => \$debug,
43+
'h|help' => \$help,
44+
) or help(1);
45+
46+
help(0) if ($help);
47+
48+
main();
49+
50+
exit 0;
51+
52+
sub dprint
53+
{
54+
printf(STDERR @_) if $debug;
55+
}
56+
57+
sub main {
58+
59+
# Open the file for reading
60+
open(my $fh, '<', $RPC_HELP_FILE) or die "Could not open file '$RPC_HELP_FILE': $!\n";
61+
62+
# Loop over each line in the file
63+
while (my $line = <$fh>) {
64+
chomp($line);
65+
if ($line =~ /^== (.+) ==/) { # Section heading.
66+
my $section = $1; # Captures the placeholder text
67+
start_section($section);
68+
} elsif ($line =~ /^\s*$/) { # Blank line.
69+
end_section();
70+
} else {
71+
add_method($line);
72+
}
73+
}
74+
75+
end_section();
76+
print_footer();
77+
78+
# Close the file handle.
79+
close($fh);
80+
}
81+
82+
sub print_header {
83+
print <<'EOM';
84+
//! # JSON-RPC types for Bitcoin Core `v$CORE_VERSION`
85+
//!
86+
//! These structs are shaped for the JSON data returned by the JSON-RPC API. They use stdlib types
87+
//! (or custom types) and where necessary implement an `into_model` function to convert the type to
88+
//! a [`crate::model`] type of the same name. The types in this module are version specific. The
89+
//! types in the `model` module are version nonspecific and are strongly typed using `rust-bitcoin`.
90+
//!
91+
//! ### Method name and implementation status
92+
//!
93+
//! Every JSON-RPC method supported by this version of Bitcoin Core is listed below along with its
94+
//! current implementation status.
95+
//!
96+
EOM
97+
}
98+
99+
sub print_footer {
100+
print <<'EOM'
101+
//!
102+
//! **Items marked omitted were omitted because:**
103+
//!
104+
//! - Method does not return anything.
105+
//! - Method returns a simple type (e.g. bool or integer).
106+
//! - Method is deprecated.
107+
EOM
108+
}
109+
110+
# Print the blurb for the start of a section.
111+
sub start_section {
112+
my ($section) = @_; # Get the first argument
113+
114+
print <<'EOM';
115+
//! <details>
116+
//! <summary> Methods from the $section section </summary>
117+
//!
118+
//! | JSON-PRC Method Name | Status |
119+
//! |:-----------------------------------|:---------------:|
120+
EOM
121+
}
122+
123+
# Print the blurb for the end of a section.
124+
sub end_section {
125+
print<<'EOM';
126+
//!
127+
//! </details>
128+
//!
129+
EOM
130+
}
131+
132+
sub add_method {
133+
my ($line) = @_;
134+
if ($line =~ /^(\S+)/) {
135+
my ($method) = $1;
136+
printf "//! \| %-34s \| |\n", $method
137+
}
138+
}

0 commit comments

Comments
 (0)