-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rs
96 lines (84 loc) · 2.33 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
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
use avgcol::AverageColor;
use convert_case::{Case, Casing};
use std::fs;
use std::io::{Result, Write};
fn main() -> Result<()> {
// ! use custom rustmatica fork for now
// create average.rs, a file which contains a phf::Map of the average colours of each block
let mut enum_entries = String::from("Air,");
let mut into_blockstate = String::from("Blocks::Air => BlockState::Air,");
let mut into_rgb = String::from("Blocks::Air => None,");
let mut asref_path = String::from(r#"Blocks::Air => None,"#);
for file in fs::read_dir("./assets/blocks")? {
let file = file?;
let bytes = fs::read(file.path())?;
let avg = AverageColor::from_bytes(bytes).unwrap();
let enum_entry = file
.file_name()
.to_str()
.unwrap()
.split(".")
.next()
.unwrap()
.to_case(Case::Pascal);
enum_entries += &format!(
"
{},",
enum_entry
);
into_blockstate += &format!(
"
Blocks::{} => BlockState::{},",
enum_entry, enum_entry
);
into_rgb += &format!(
"
Blocks::{} => Some(Rgb::new({}., {}., {}.)),",
enum_entry, avg.0, avg.1, avg.2
);
asref_path += &format!(
r#"
Blocks::{} => Some(PathBuf::from("{}.png")),"#,
enum_entry,
enum_entry.to_case(Case::Snake)
)
}
let mut file = fs::File::create("./src/average.rs")?;
file.write_all(
format!(
"//! This file is generated by build.rs, do not edit it manually
use rustmatica::BlockState;
use color_space::Rgb;
use std::path::PathBuf;
use strum::EnumIter;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, EnumIter)]
pub enum Blocks {{
{enum_entries}
}}
impl Into<BlockState<'_>> for Blocks {{
fn into(self) -> BlockState<'static> {{
match self {{
{into_blockstate}
}}
}}
}}
impl Into<Option<Rgb>> for Blocks {{
fn into(self) -> Option<Rgb> {{
match self {{
{into_rgb}
}}
}}
}}
impl Into<Option<PathBuf>> for Blocks {{
fn into(self) -> Option<PathBuf> {{
match self {{
{asref_path}
}}
}}
}}"
)
.as_bytes(),
)?;
drop(file);
Ok(())
}