Initial commit of scryfall library

This commit is contained in:
Adrian Malacoda 2017-12-03 22:54:19 -06:00
commit 5f04c6538f
3 changed files with 145 additions and 0 deletions

11
Cargo.toml Normal file
View File

@ -0,0 +1,11 @@
[package]
name = "scryfall"
version = "0.0.1"
authors = ["A. Malacoda <adrian.malacoda@monarch-pass.net>"]
[dependencies]
reqwest = "0.8.0"
serde = "1.0.14"
serde_derive = "1.0.14"
serde_json = "1.0.3"
derive-error = "0.0.3"

125
src/lib.rs Normal file
View File

@ -0,0 +1,125 @@
extern crate serde;
extern crate serde_json;
#[macro_use] extern crate serde_derive;
extern crate reqwest;
#[macro_use] extern crate derive_error;
use std::collections::HashMap;
use std::io::Read;
pub struct Scryfall {}
impl Scryfall {
pub fn new() -> Scryfall {
Scryfall {}
}
pub fn search(&self, query: &str) -> Result<CardList, Error> {
let mut response = reqwest::get(&format!("https://api.scryfall.com/cards/search?q={}", query))?;
let mut content = String::new();
response.read_to_string(&mut content);
Result::Ok(serde_json::from_str(&content)?)
}
}
#[derive(Serialize, Deserialize, Debug)]
pub struct CardList {
pub data: Vec<Card>,
pub has_more: bool,
pub next_page: Option<String>,
pub total_cards: u32,
pub warnings: Option<Vec<String>>
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Card {
pub id: String,
pub multiverse_ids: Vec<u32>,
pub mtgo_id: Option<u32>,
pub mtgo_foil_id: Option<u32>,
pub uri: String,
pub scryfall_uri: String,
pub rulings_uri: String,
pub name: String,
pub layout: String,
pub cmc: f32,
pub type_line: String,
pub oracle_text: Option<String>,
pub mana_cost: String,
pub power: Option<String>,
pub toughness: Option<String>,
pub loyalty: Option<String>,
pub life_modifier: Option<String>,
pub hand_modifier: Option<String>,
pub colors: Colors,
pub color_indicator: Option<Colors>,
pub color_identity: Colors,
pub all_parts: Option<Vec<RelatedCard>>,
pub card_faces: Option<Vec<CardFace>>,
pub legalities: HashMap<String, String>,
pub reserved: bool,
pub edhrec_rank: Option<u32>,
pub set: String,
pub set_name: String,
pub collector_number: String,
pub set_search_uri: String,
pub scryfall_set_uri: String,
pub image_uris: Option<HashMap<String, String>>,
pub highres_image: bool,
pub reprint: bool,
pub digital: bool,
pub rarity: String,
pub flavor_text: Option<String>,
pub artist: Option<String>,
pub frame: String,
pub full_art: bool,
pub watermark: Option<String>,
pub border_color: String,
pub story_spotlight_number: Option<u32>,
pub story_spotlight_uri: Option<String>,
pub timeshifted: bool,
pub colorshifted: bool,
pub futureshifted: bool
}
type Colors = Vec<String>;
#[derive(Serialize, Deserialize, Debug)]
pub struct RelatedCard {
pub id: String,
pub name: String,
pub uri: String
}
#[derive(Serialize, Deserialize, Debug)]
pub struct CardFace {
pub name: String,
pub type_line: String,
pub oracle_text: Option<String>,
pub mana_cost: String,
pub colors: Colors,
pub color_indicator: Option<Colors>,
pub power: Option<String>,
pub toughness: Option<String>,
pub loyalty: Option<String>,
pub flavor_text: Option<String>,
pub image_uris: Option<HashMap<String, String>>
}
#[derive(Debug, Error)]
pub enum Error {
/// HTTP request failed
HTTP(reqwest::Error),
/// Failed to parse
Parse(serde_json::Error)
}
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
}
}

9
src/main.rs Normal file
View File

@ -0,0 +1,9 @@
extern crate scryfall;
use scryfall::Scryfall;
use std::env;
fn main() {
let query = env::args().nth(1).expect("please supply an query");
let sf = Scryfall::new();
println!("{:?}", sf.search(&query).expect("Search failed"));
}