#![deny(missing_docs)]
#![doc = include_str!("../README.md")]
mod cpu_arch;
mod ext;
mod jdk_desc;
mod os;
mod version_info;
use std::{
fmt::Display,
io::Write,
path::{Path, PathBuf},
};
pub use cpu_arch::CpuArch;
pub use ext::Ext;
pub use jdk_desc::JdkDesc;
pub use os::Os;
use serde::{Deserialize, Serialize};
pub use version_info::VersionInfo;
#[derive(Debug, thiserror::Error)]
pub enum DownloadError {
#[error("IO error: {0}")]
Io(#[from] std::io::Error),
#[error("Reqwest error: {0}")]
Reqwest(#[from] reqwest::Error),
}
pub struct DownloadOpts {
pub create_dir: bool,
}
impl Default for DownloadOpts {
fn default() -> Self {
Self { create_dir: true }
}
}
#[derive(Clone, Copy, Debug, PartialEq, PartialOrd, Eq, Ord, Serialize, Deserialize)]
#[serde(transparent)]
pub struct Version(u8);
impl Display for Version {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
pub struct DownloadURL(String);
impl DownloadURL {
pub async fn download(
&self,
dir_path: impl AsRef<Path>,
opts: DownloadOpts,
) -> Result<PathBuf, DownloadError> {
let DownloadOpts { create_dir } = opts;
let DownloadURL(url) = self;
let filename = url
.strip_prefix("https://corretto.aws/downloads/latest/")
.unwrap();
let response = reqwest::get(url).await?;
let path = dir_path.as_ref().join(filename);
if create_dir {
std::fs::create_dir_all(&dir_path)?;
}
let mut file = std::fs::File::create(&path)?;
let bytes = response.bytes().await?;
file.write_all(&bytes)?;
Ok(path)
}
pub async fn download_tmp(&self) -> Result<PathBuf, DownloadError> {
let dir = tempfile::tempdir()?;
self.download(dir.path(), DownloadOpts { create_dir: false })
.await
}
}