-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.rs
209 lines (196 loc) Β· 7.19 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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
use indicatif::{ProgressBar, ProgressStyle};
use reqwest::ClientBuilder;
use serde::Deserialize;
use serde_json::Value;
use std::collections::HashMap;
use std::env;
use std::future::Future;
use std::path::{Path, PathBuf};
use std::pin::Pin;
use std::{fs::File, io::Write};
use tokio_stream::StreamExt;
static METADATA_CACHE_FILE_NAME: &str = "emojikitchen.json";
static PARTIAL_KITCHEIN_DATA_DIR: &str = "partial-kitchen-data";
#[cfg(feature = "emoji-paw-prints")]
static PAW_PRINTS_CODEPOINT: &str = "1f43e";
#[cfg(feature = "emoji-paw-prints")]
static PAW_PRINTS_FEATURE_NAME: &str = "paw-prints";
#[cfg(feature = "emoji-cat")]
static CAT_CODEPOINT: &str = "1f431";
#[cfg(feature = "emoji-cat")]
static CAT_FEATURE_NAME: &str = "cat";
static KITCHEN_METADATA_URL: &str =
"https://raw.githubusercontent.com/xsalazar/emoji-kitchen-backend/main/app/metadata.json";
#[derive(Deserialize, PartialEq, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct KitchenMetaData {
pub known_supported_emoji: Vec<String>,
pub data: HashMap<String, Value>,
}
async fn get_file_size(url: reqwest::Url) -> Result<u64, reqwest::Error> {
let client = ClientBuilder::new()
.build()
.expect("Could not build client");
let response = client.get(url).header("Range", "bytes=0-1").send().await?;
let size = match response.status() {
reqwest::StatusCode::PARTIAL_CONTENT => response
.headers()
.get("Content-Range")
.expect("Could not get content range")
.to_str()
.expect("Could not convert to string")
.split('/')
.last()
.expect("Could not get last")
.parse()
.expect("Could not parse"),
_ => 0,
};
Ok(size)
}
pub async fn download_file<S, P, FB, FD, FA>(
url: reqwest::Url,
path: P,
before_download_hook: FB,
on_download_hook: FD,
after_download_hook: FA,
) -> Result<(), reqwest::Error>
where
P: AsRef<Path>,
FB: FnOnce(&reqwest::Url, &PathBuf) -> Pin<Box<dyn Future<Output = S>>>,
FD: Fn(S, &[u8]) -> Pin<Box<dyn Future<Output = S>>>,
FA: FnOnce(S) -> Pin<Box<dyn Future<Output = ()>>>,
{
let path = path.as_ref().to_path_buf();
if let Some(p) = path.parent() {
ensure_dir(p).expect("Could not create cache directory");
}
let mut hook_state = before_download_hook(&url, &path).await;
let mut stream = reqwest::get(url).await?.bytes_stream();
let mut file = File::create(&path).expect("Could not create cache file");
while let Some(chunk_result) = stream.next().await {
let chunk = chunk_result?;
hook_state = on_download_hook(hook_state, &chunk).await;
file.write_all(&chunk)
.expect("Could not write to cache file");
}
after_download_hook(hook_state).await;
Ok(())
}
#[allow(dead_code)]
fn dummy_before_download_hook(_: &reqwest::Url, _: &PathBuf) -> Pin<Box<dyn Future<Output = ()>>> {
Box::pin(async {})
}
#[allow(dead_code)]
fn dummy_on_download_hook(_: (), _chunk: &[u8]) -> Pin<Box<dyn Future<Output = ()>>> {
Box::pin(async {})
}
#[allow(dead_code)]
fn dummy_after_download_hook(_: ()) -> Pin<Box<dyn Future<Output = ()>>> {
Box::pin(async {})
}
struct DownloadState {
total_size: u64,
downloaded: usize,
progress_bar: Box<ProgressBar>,
}
#[allow(dead_code)]
pub async fn get_metadata_with_progressbar<P>(
cache_path: P,
) -> Result<KitchenMetaData, reqwest::Error>
where
P: AsRef<Path>,
{
let before_download_hook = |url: &reqwest::Url,
_path: &PathBuf|
-> Pin<Box<dyn Future<Output = DownloadState>>> {
let url = url.clone();
Box::pin(async {
let total_size = get_file_size(url).await.unwrap();
let pb = Box::new(ProgressBar::new(total_size));
pb.set_style(ProgressStyle::with_template("{spinner:.green} [{elapsed_precise}] [{wide_bar:.magenta/238}] {bytes}/{total_bytes} ({bytes_per_sec}, {eta})")
.unwrap()
.progress_chars("ββΈβ"));
DownloadState {
total_size,
downloaded: 0,
progress_bar: pb,
}
})
};
let on_download_hook =
|state: DownloadState, chunk: &[u8]| -> Pin<Box<dyn Future<Output = DownloadState>>> {
let chunk_size = chunk.len();
Box::pin(async move {
let downloaded = state.downloaded + chunk_size;
state.progress_bar.set_position(downloaded as u64);
DownloadState {
total_size: state.total_size,
downloaded,
progress_bar: state.progress_bar,
}
})
};
let after_download_hook = |state: DownloadState| -> Pin<Box<dyn Future<Output = ()>>> {
Box::pin(async move {
state.progress_bar.finish_with_message("done!");
})
};
if !cache_path.as_ref().exists() {
eprintln!("Metadata not found, downloading...");
download_file(
reqwest::Url::try_from(KITCHEN_METADATA_URL).expect("Could not parse URL"),
cache_path.as_ref(),
before_download_hook,
on_download_hook,
after_download_hook,
)
.await?;
}
let response = std::fs::read_to_string(cache_path).unwrap();
let metadata: KitchenMetaData = serde_json::from_str(&response).unwrap();
Ok(metadata)
}
pub fn ensure_dir(dir: &Path) -> Result<(), std::io::Error> {
if !dir.exists() {
std::fs::create_dir_all(dir)?;
}
Ok(())
}
#[tokio::main]
async fn main() {
let out_dir = env::var_os("OUT_DIR").unwrap();
let kitchen_partial_data_dir = Path::new(&out_dir).join(PARTIAL_KITCHEIN_DATA_DIR);
ensure_dir(&kitchen_partial_data_dir).expect("Could not create partial data directory");
let metadata_cache_path = Path::new(&out_dir).join(METADATA_CACHE_FILE_NAME);
let metadata = get_metadata_with_progressbar(metadata_cache_path)
.await
.unwrap();
#[cfg(feature = "emoji-paw-prints")]
{
let paw_prints_partial_data_path =
kitchen_partial_data_dir.join(format!("{}.json", PAW_PRINTS_FEATURE_NAME));
let paw_prints_kitchen_data = metadata
.data
.get(PAW_PRINTS_CODEPOINT)
.expect("Could not get paw prints data");
let paw_prints_kitchen_string = serde_json::to_string(paw_prints_kitchen_data)
.expect("Could not serialize paw prints data");
std::fs::write(paw_prints_partial_data_path, paw_prints_kitchen_string)
.expect("Could not write paw prints data");
}
#[cfg(feature = "emoji-cat")]
{
let cat_partial_data_path =
kitchen_partial_data_dir.join(format!("{}.json", CAT_FEATURE_NAME));
let cat_kitchen_data = metadata
.data
.get(CAT_CODEPOINT)
.expect("Could not get cat data");
let cat_kitchen_string =
serde_json::to_string(cat_kitchen_data).expect("Could not serialize cat data");
std::fs::write(cat_partial_data_path, cat_kitchen_string)
.expect("Could not write cat data");
}
println!("cargo:rerun-if-changed=build.rs");
}