-
Notifications
You must be signed in to change notification settings - Fork 4
/
generate_readme.rs
138 lines (114 loc) · 3.69 KB
/
generate_readme.rs
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
use std::{
fmt::{Display, Formatter, Write as FmtWrite},
io::Write,
};
use serde::Deserialize;
const JSON: &str = include_str!("./snapshots.json");
const TEMPLATE: &str = include_str!("./TEMPLATE.md");
#[derive(Deserialize)]
struct Node {
node: NodeType,
networks: Vec<Network>,
}
#[derive(Deserialize)]
#[serde(rename_all = "snake_case")]
enum NodeType {
Pathfinder,
}
#[derive(Deserialize)]
struct Network {
network: NetworkType,
snapshots: Vec<Snapshot>,
}
#[derive(Deserialize)]
enum NetworkType {
#[serde(rename = "mainnet")]
Mainnet,
#[serde(rename = "goerli-1")]
Goerli1,
#[serde(rename = "goerli-2")]
Goerli2,
}
#[derive(Deserialize)]
struct Snapshot {
block: u64,
version: String,
}
impl NodeType {
fn downloand_link_base(&self) -> &str {
match self {
Self::Pathfinder => "https://pathfinder-backup.zklend.com/",
}
}
}
impl Snapshot {
fn file_name(&self, network: &NetworkType) -> String {
format!("{}-v{}-{}.tar.xz", network, self.version, self.block)
}
}
impl Display for NodeType {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Self::Pathfinder => write!(f, "pathfinder"),
}
}
}
impl Display for NetworkType {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Self::Mainnet => write!(f, "mainnet"),
Self::Goerli1 => write!(f, "goerli-1"),
Self::Goerli2 => write!(f, "goerli-2"),
}
}
}
fn main() {
let nodes: Vec<Node> = serde_json::from_str(JSON).expect("unable to deserialize JSON");
let mut snapshot_list = String::new();
let mut example_network: Option<String> = None;
let mut example_file_name: Option<String> = None;
for node in nodes.iter() {
writeln!(snapshot_list, "## `{}`", node.node).unwrap();
for network in node.networks.iter() {
writeln!(snapshot_list).unwrap();
writeln!(snapshot_list, "### `{}`", network.network).unwrap();
writeln!(snapshot_list).unwrap();
writeln!(
snapshot_list,
"| Network | Version | Block | Download Link |"
)
.unwrap();
writeln!(snapshot_list, "| - | - | - | - |").unwrap();
for snapshot in network.snapshots.iter() {
let network_name = format!("{}", network.network);
let file_name = snapshot.file_name(&network.network);
if example_network.is_none() {
example_network = Some(network_name.clone());
}
if example_file_name.is_none() {
example_file_name = Some(file_name.clone());
}
writeln!(
snapshot_list,
"| `{}` | `v{}` | `{}` | {}{}/{} |",
network_name,
snapshot.version,
snapshot.block,
node.node.downloand_link_base(),
network_name,
file_name
)
.unwrap();
}
}
}
let example_network = example_network.expect("no example network generated");
let example_file_name = example_file_name.expect("no example file name generated");
let full_readme = TEMPLATE
.replace("EXAMPLE_NETWORK", &example_network)
.replace("EXAMPLE_FILE_NAME", &example_file_name)
.replace("LIST_OF_SNAPSHOTS", &snapshot_list);
let mut file = std::fs::File::create("./README.md").expect("unable to create file");
file.write_all(full_readme.as_bytes())
.expect("unable to write to file");
}