-
Notifications
You must be signed in to change notification settings - Fork 277
/
Copy pathbuild.rs
39 lines (27 loc) · 1.16 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
//! Build script that auto-updates sample binaries from sources.
use std::{fs, path::PathBuf};
use color_eyre::eyre::Result;
use iroha_data_model::{account::NewAccount, domain::NewDomain, prelude::*};
use parity_scale_codec::Encode;
use serde::de::DeserializeOwned;
fn main() {
sample_into_binary_file::<NewAccount>("account").expect("Failed to encode into account.bin.");
sample_into_binary_file::<NewDomain>("domain").expect("Failed to encode into domain.bin.");
sample_into_binary_file::<Trigger>("trigger").expect("Failed to encode into trigger.bin.");
}
fn sample_into_binary_file<T>(filename: &str) -> Result<()>
where
T: Encode + DeserializeOwned,
{
let mut path_to = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
path_to.push("samples/codec");
path_to.push(filename);
let path_to_json = path_to.with_extension("json");
let path_to_binary = path_to.with_extension("bin");
println!("cargo:rerun-if-changed={}", path_to_json.to_str().unwrap());
let buf = fs::read_to_string(path_to_json)?;
let sample = serde_json::from_str::<T>(buf.as_str())?;
let buf = sample.encode();
fs::write(path_to_binary, buf)?;
Ok(())
}