Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions crates/era-downloader/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,20 @@ impl<Http: HttpClient + Clone> EraClient<Http> {
const CHECKSUMS: &'static str = "checksums.txt";

/// Constructs [`EraClient`] using `client` to download from `url` into `folder`.
///
/// The file type is auto-detected from the URL. Use
/// [`with_era_type`](Self::with_era_type) to override.
pub fn new(client: Http, url: Url, folder: impl Into<Box<Path>>) -> Self {
let era_type = EraFileType::from_url(url.as_str());
Self { client, url, folder: folder.into(), era_type }
}

/// Override the auto-detected [`EraFileType`].
pub const fn with_era_type(mut self, era_type: EraFileType) -> Self {
self.era_type = era_type;
self
}

/// Performs a GET request on `url` and stores the response body into a file located within
/// the `folder`.
pub async fn download_to_file(&mut self, url: impl IntoUrl) -> eyre::Result<Box<Path>> {
Expand Down Expand Up @@ -367,4 +376,19 @@ mod tests {

assert_eq!(actual_number, expected_number);
}

#[test]
fn test_with_era_type_overrides_auto_detection() {
// URL without "era1" auto-detects as Era
let client = EraClient::new(
Client::new(),
Url::from_str("https://example.com/").unwrap(),
PathBuf::new(),
);
assert_eq!(client.era_type, EraFileType::Era);

// with_era_type overrides to Era1
let client = client.with_era_type(EraFileType::Era1);
assert_eq!(client.era_type, EraFileType::Era1);
}
}
Loading