Created
April 26, 2019 01:50
-
-
Save BennyFranco/c828d1d6375d5d75e34494451fe9db66 to your computer and use it in GitHub Desktop.
Read a toml file config and download a binary file with libcurl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[lib] | |
name = "SFML" | |
filename ="SFML-2.5.1-windows-vc15-64-bit.zip" | |
url ="https://github.com/SFML/SFML/releases/download/2.5.1/SFML-2.5.1-windows-vc15-64-bit.zip" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use curl::easy::Easy; | |
use std::fs::File; | |
use std::io::prelude::*; | |
use serde::{Deserialize}; | |
#[derive(Deserialize)] | |
struct Lib { | |
name: String, | |
filename: String, | |
url: String, | |
} | |
#[derive(Deserialize)] | |
struct Dep { | |
lib:Lib, | |
} | |
fn main() -> std::io::Result<()> { | |
let mut f = File::open("lib.toml")?; | |
let mut buffer = Vec::new(); | |
// read the whole file | |
f.read_to_end(&mut buffer)?; | |
let dep:Dep = toml::from_slice(&buffer).unwrap(); | |
println!("{:#?}", dep.lib.name); | |
println!("{:#?}", dep.lib.filename); | |
println!("{:#?}", dep.lib.url); | |
let mut dst = Vec::new(); | |
let mut easy = Easy::new(); | |
easy.url(&dep.lib.url).unwrap(); | |
let _redirect = easy.follow_location(true); | |
{ | |
let mut transfer = easy.transfer(); | |
transfer.write_function(|data| { | |
dst.extend_from_slice(data); | |
Ok(data.len()) | |
}).unwrap(); | |
transfer.perform().unwrap(); | |
} | |
{ | |
let mut file = File::create(dep.lib.filename)?; | |
file.write_all(dst.as_slice())?; | |
} | |
Ok(()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment