From c267d46fc04eec6a25ab4d5e475846da5f49c965 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20Rozier?= Date: Fri, 12 Jul 2024 23:19:05 +0200 Subject: [PATCH] Reworking handlers to avoid returning errors --- rust/src/{standard => }/download.rs | 33 +++++++++++++++++------------ rust/src/lib.rs | 1 + rust/src/mojang/manifest.rs | 18 +++++++++++----- rust/src/standard/mod.rs | 3 --- 4 files changed, 34 insertions(+), 21 deletions(-) rename rust/src/{standard => }/download.rs (94%) diff --git a/rust/src/standard/download.rs b/rust/src/download.rs similarity index 94% rename from rust/src/standard/download.rs rename to rust/src/download.rs index d80840e4..e634c955 100644 --- a/rust/src/standard/download.rs +++ b/rust/src/download.rs @@ -1,4 +1,4 @@ -//! Parallel download implementation for standard installer. +//! Parallel download implementation. //! //! Partially inspired by: https://patshaughnessy.net/2020/1/20/downloading-100000-files-using-async-rust @@ -17,9 +17,12 @@ use tokio::task::JoinSet; use tokio::sync::mpsc; use tokio::fs::File; -use crate::http; -use super::{serde, Event, Handler, Installer, Result, Error}; +/// A list of pending download that can be all downloaded at once. +#[derive(Debug)] +pub struct DownloadList { + inner: Vec, +} /// Bulk download blocking entrypoint. @@ -84,7 +87,7 @@ pub async fn download_many( })?; // Initialize the HTTP(S) client. - let client = http::builder() + let client = crate::http::builder() .build() .unwrap(); // FIXME: @@ -253,6 +256,10 @@ async fn download_core( } +pub trait DownloadWatcher { + +} + /// A download entry that can be delayed until a call to [`Handler::flush_download`]. /// This download object borrows the URL and file path. #[derive(Debug, Clone, PartialEq, Eq)] @@ -291,17 +298,17 @@ impl DownloadSource { } -impl<'a> From<&'a serde::Download> for DownloadSource { +// impl<'a> From<&'a serde::Download> for DownloadSource { - fn from(serde: &'a serde::Download) -> Self { - Self { - url: serde.url.clone().into(), - size: serde.size, - sha1: serde.sha1.as_deref().copied(), - } - } +// fn from(serde: &'a serde::Download) -> Self { +// Self { +// url: serde.url.clone().into(), +// size: serde.size, +// sha1: serde.sha1.as_deref().copied(), +// } +// } -} +// } #[derive(thiserror::Error, Debug)] pub enum DownloadError { diff --git a/rust/src/lib.rs b/rust/src/lib.rs index e2cfedaf..79a8e341 100644 --- a/rust/src/lib.rs +++ b/rust/src/lib.rs @@ -4,6 +4,7 @@ pub mod gav; pub mod path; pub mod http; +pub mod download; pub mod standard; pub mod mojang; diff --git a/rust/src/mojang/manifest.rs b/rust/src/mojang/manifest.rs index 97cd9f88..feb50217 100644 --- a/rust/src/mojang/manifest.rs +++ b/rust/src/mojang/manifest.rs @@ -1,9 +1,9 @@ //! Optionally cached Mojang manifest. +use std::io::{self, BufReader}; use std::path::PathBuf; use std::fs::File; -use crate::standard::{Result, Error}; use super::serde; @@ -32,16 +32,22 @@ impl MojangManifest { } } - pub fn get(&mut self) -> &serde::MojangManifest { + pub fn get(&mut self) -> io::Result<&serde::MojangManifest> { if let Some(data) = &self.data { - return data; + return Ok(data); } if let Some(cache_file) = self.cache_file.as_deref() { + + let cache_reader = match File::open(cache_file) { + Ok(reader) => BufReader::new(reader), + Err(e) if e.kind() == io::ErrorKind::NotFound + } - // let file = File::open(cache_file) - // .map_err(|e| ) + self.datamatch serde_json::from_reader(BufReader::new(File::open(cache_file)?)) { + Ok(obj) => obj + } } @@ -49,4 +55,6 @@ impl MojangManifest { } + fn + } diff --git a/rust/src/standard/mod.rs b/rust/src/standard/mod.rs index 0ecad0d1..aa569f6c 100644 --- a/rust/src/standard/mod.rs +++ b/rust/src/standard/mod.rs @@ -1,7 +1,6 @@ //! Standard installation procedure. pub mod serde; -mod download; use std::io::{self, BufReader, Seek, SeekFrom}; use std::collections::{HashMap, HashSet}; @@ -14,8 +13,6 @@ use sha1::{Digest, Sha1}; use crate::path::PathExt; use crate::gav::Gav; -pub use self::download::{Download, DownloadSource, DownloadError}; - /// Base URL for downloading game's assets. const RESOURCES_URL: &str = "https://resources.download.minecraft.net/";