sandia_decay_database_common/
lib.rs

1#![doc = include_str!("README.md")]
2#![allow(
3    missing_docs,
4    clippy::missing_panics_doc,
5    clippy::items_after_statements
6)]
7
8use std::{
9    env::var_os,
10    fs::File,
11    io::{BufWriter, Read, Write},
12    path::PathBuf,
13};
14
15pub fn download(url: &str) {
16    let out_dir = PathBuf::from(var_os("OUT_DIR").expect("should have a cargo output dir"));
17    let database_path = out_dir.join("database.xml");
18    {
19        let mut response = minreq::get(url)
20            .send_lazy()
21            .expect("should be able to download a database file");
22
23        let database_file = File::options()
24            .create(true)
25            .write(true)
26            .truncate(true)
27            .open(&database_path)
28            .expect("should be able to open database file");
29        let mut writer = BufWriter::new(database_file);
30
31        const BUFSIZ: usize = 2 << 20;
32        let mut buf = vec![0; BUFSIZ];
33        loop {
34            let len = response
35                .read(buf.as_mut_slice())
36                .expect("should be able to read a response");
37            if len == 0 {
38                // we are done
39                break;
40            }
41            writer
42                .write_all(&buf[..len])
43                .expect("should be able to write into a file");
44        }
45    }
46}