the solc binaries list downloader

Signed-off-by: xermicus <bigcyrill@hotmail.com>
This commit is contained in:
xermicus
2025-03-21 14:32:15 +01:00
parent eb685fc668
commit 84a5647a8b
10 changed files with 639 additions and 27 deletions
+61
View File
@@ -0,0 +1,61 @@
//! This module downloads solc binaries.
use std::{collections::HashMap, sync::Mutex};
use once_cell::sync::Lazy;
use crate::list::List;
pub static LIST_CACHE: Lazy<Mutex<HashMap<&'static str, List>>> = Lazy::new(Default::default);
impl List {
pub const LINUX_URL: &str = "https://binaries.soliditylang.org/linux-amd64/list.json";
pub const WINDOWS_URL: &str = "https://binaries.soliditylang.org/windows-amd64/list.json";
pub const MACOSX_URL: &str = "https://binaries.soliditylang.org/macosx-amd64/list.json";
pub const WASM_URL: &str = "https://binaries.soliditylang.org/wasm/list.json";
/// Try to downloads the list from the given URL.
///
/// Caches the list retrieved from the `url` into [LIST_CACHE],
/// subsequent calls with the same `url` will return the cached list.
pub fn download(url: &'static str) -> anyhow::Result<Self> {
if let Some(list) = LIST_CACHE.lock().unwrap().get(url) {
return Ok(list.clone());
}
let body: List = reqwest::blocking::get(url)?.json()?;
LIST_CACHE.lock().unwrap().insert(url, body.clone());
Ok(body)
}
}
#[cfg(test)]
mod tests {
use crate::list::List;
#[test]
fn try_get_windows_list() {
List::download(List::WINDOWS_URL).unwrap();
List::download(List::WINDOWS_URL).unwrap();
}
#[test]
fn try_get_macosx_list() {
List::download(List::MACOSX_URL).unwrap();
List::download(List::MACOSX_URL).unwrap();
}
#[test]
fn try_get_linux_list() {
List::download(List::LINUX_URL).unwrap();
List::download(List::LINUX_URL).unwrap();
}
#[test]
fn try_get_wasm_list() {
List::download(List::WASM_URL).unwrap();
List::download(List::WASM_URL).unwrap();
}
}
+8
View File
@@ -0,0 +1,8 @@
//! This crates provides serializable Rust type definitions for the [solc binary lists][0].
//! The `download` feature enables helpers to download and cache solc binaries.
//!
//! [0]: https://binaries.soliditylang.org
#[cfg(feature = "download")]
pub mod download;
pub mod list;
+26
View File
@@ -0,0 +1,26 @@
//! Rust type definitions for the solc binary lists.
use std::{collections::HashMap, path::PathBuf};
use semver::Version;
use serde::Deserialize;
#[derive(Debug, Deserialize, Clone, Eq, PartialEq)]
pub struct List {
pub builds: Vec<Build>,
pub releases: HashMap<Version, String>,
#[serde(rename = "latestRelease")]
pub latest_release: Version,
}
#[derive(Debug, Deserialize, Clone, Eq, PartialEq)]
pub struct Build {
pub path: PathBuf,
pub version: Version,
pub build: String,
#[serde(rename = "longVersion")]
pub long_version: String,
keccak256: String,
sha256: String,
urls: Vec<String>,
}