Skip to content

Commit

Permalink
Clean up the code, remove duplucations, non caching reader is still n…
Browse files Browse the repository at this point in the history
…ot implemented
  • Loading branch information
voidcontext committed Jan 29, 2025
1 parent 2296dad commit 3992c88
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 227 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ categories = ["api-bindings", "games"]
features = ["unknown_variants"]

[features]
default = ["bulk_caching"]
default = []
bulk_caching = ["dep:heck"]
unknown_variants = []
unknown_variants_slim = []
Expand Down
2 changes: 1 addition & 1 deletion examples/bulk/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ async fn main() -> scryfall::Result<()> {
match card {
Ok(_) => {
count += 1;
if count % 1000 == 0 {
if count % 5000 == 0 {
println!("{count}");
}
},
Expand Down
26 changes: 0 additions & 26 deletions examples/streaming-bulk/main.rs

This file was deleted.

36 changes: 10 additions & 26 deletions src/bulk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ use uuid::Uuid;
use crate::card::Card;
use crate::ruling::Ruling;
use crate::uri::Uri;
use crate::util::array_stream_reader::ArrayStreamReader;
use crate::util::{stream_iterator, BULK_DATA_URL};
use crate::Error;
use futures_util::StreamExt;
Expand Down Expand Up @@ -100,7 +99,7 @@ pub struct BulkDataFile<T> {
_object: String,
}

impl<T: DeserializeOwned> BulkDataFile<T> {
impl<T: DeserializeOwned + Send + 'static> BulkDataFile<T> {
cfg_if! {
if #[cfg(feature = "bulk_caching")] {
/// The full temp path where this file will be downloaded with `load`. The
Expand All @@ -122,8 +121,8 @@ impl<T: DeserializeOwned> BulkDataFile<T> {
Ok(BufReader::new(File::open(cache_path)?))
}
} else {
async fn get_reader(&self) -> crate::Result<BufReader<impl io::Read + Send>> {
self.download_uri
async fn get_reader(&self) -> crate::Result<BufReader<impl std::io::Read + Send>> {
let stream = self.download_uri
.fetch_raw()
.await?
.bytes_stream();
Expand Down Expand Up @@ -158,10 +157,8 @@ impl<T: DeserializeOwned> BulkDataFile<T> {
/// Downloads and stores the file in the computer's temp folder if this
/// version hasn't been downloaded yet. Otherwise uses the stored copy.
pub async fn load_iter(&self) -> crate::Result<impl Iterator<Item = crate::Result<T>>> {
let de = serde_json::Deserializer::from_reader(ArrayStreamReader::new_buffered(
self.get_reader().await?,
));
Ok(de.into_iter().map(|item| item.map_err(|e| e.into())))
let reader = self.get_reader().await?;
Ok(stream_iterator::create(reader))
}

/// Downloads this file, saving it to `path`. Overwrites the file if it
Expand Down Expand Up @@ -216,24 +213,10 @@ pub async fn default_cards() -> crate::Result<impl Iterator<Item = crate::Result
}

/// An iterator of every card object on Scryfall in every language.
///
/// # Note
/// This currently takes about 2GB of RAM before returning 👀.
pub async fn all_cards() -> crate::Result<impl Iterator<Item = crate::Result<Card>>> {
BulkDataFile::of_type("all_cards").await?.load_iter().await
}

/// An iterator of every card object on Scryfall in every language.
pub async fn all_cards_streaming_visitor(
) -> crate::Result<impl Iterator<Item = crate::Result<Card>>> {
let reader = BulkDataFile::<Vec<Card>>::of_type("all_cards")
.await?
.get_reader()
.await?;

Ok(stream_iterator::create(reader))
}

/// An iterator of all Rulings on Scryfall. Each ruling refers to cards via an
/// `oracle_id`.
pub async fn rulings() -> crate::Result<impl Iterator<Item = crate::Result<Ruling>>> {
Expand All @@ -242,6 +225,9 @@ pub async fn rulings() -> crate::Result<impl Iterator<Item = crate::Result<Rulin

#[cfg(test)]
mod tests {
use std::io::BufReader;

use crate::util::stream_iterator;

#[tokio::test]
#[ignore]
Expand Down Expand Up @@ -285,8 +271,6 @@ mod tests {

#[test]
fn test_parse_list() {
use serde_json::Deserializer;

use crate::ruling::Ruling;
let s = r#"[
{
Expand All @@ -304,9 +288,9 @@ mod tests {
"comment": "The “commander tax” increases based on how many times a commander was cast from the command zone. Casting a commander from your hand doesn’t require that additional cost, and it doesn’t increase what the cost will be the next time you cast that commander from the command zone."
}
]"#;
Deserializer::from_reader(super::ArrayStreamReader::new_buffered(s.as_bytes()))
stream_iterator::create(BufReader::new(s.as_bytes()))
.into_iter()
.map(|r: serde_json::Result<Ruling>| r.unwrap())
.map(|r: crate::Result<Ruling>| r.unwrap())
.for_each(drop);
}
}
1 change: 0 additions & 1 deletion src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use once_cell::sync::Lazy;
use serde::{Deserialize, Deserializer};
use url::Url;

pub(crate) mod array_stream_reader;
pub(crate) mod stream_iterator;

/// The [scryfall](https://scryfall.com/docs/api) endpoint.
Expand Down
172 changes: 0 additions & 172 deletions src/util/array_stream_reader.rs

This file was deleted.

0 comments on commit 3992c88

Please sign in to comment.