forked from ameliatastic/seahorse-lang
-
Notifications
You must be signed in to change notification settings - Fork 9
/
build.rs
58 lines (49 loc) · 1.67 KB
/
build.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
use proc_macro2::TokenStream;
use quote::quote;
use std::{
env,
ffi::OsString,
fs::{read_dir, File},
io::{Read, Write},
path::Path,
};
fn build_pyth_table(out_dir: &OsString) {
let mut tokens = TokenStream::new();
let mut pyth_csv = String::new();
// Note on this data: copy-pasted from here (https://pyth.network/developers/price-feed-ids#solana-mainnet-beta)
// and formatted it into a CSV
let mut file = File::open("data/pyth.csv").unwrap();
file.read_to_string(&mut pyth_csv).unwrap();
let rows = pyth_csv.lines().filter_map(|line| {
let cols = line.split(',').collect::<Vec<_>>();
// Finally found a use for slice matching, ty mr rust
if let &[cluster, _, product, addr] = &cols[..] {
let product = format!("{}-{}", cluster, product);
Some(quote! {
#product => Some(#addr)
})
} else {
None
}
});
tokens.extend(quote! {
pub fn get_pyth_price_address(product: &str) -> Option<&'static str> {
match product {
#(#rows),*,
_ => None
}
}
});
let data_path = Path::new(out_dir).join("pyth.rs");
let mut file = File::create(data_path).unwrap();
file.write_all(tokens.to_string().as_bytes()).unwrap();
}
fn main() {
let entries = read_dir("data").unwrap().map(|entry| entry.unwrap());
for entry in entries {
println!("cargo:rerun-if-changed=data/{}", entry.path().display());
}
let out_dir = env::var_os("OUT_DIR").unwrap();
// Write the table in /data/pyth.csv to something Rust-readable.
build_pyth_table(&out_dir);
}