commit 5f04c6538f01d9f5b7c2bc801e9548c92e7392aa Author: Adrian Malacoda Date: Sun Dec 3 22:54:19 2017 -0600 Initial commit of scryfall library diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..f66064d --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "scryfall" +version = "0.0.1" +authors = ["A. Malacoda "] + +[dependencies] +reqwest = "0.8.0" +serde = "1.0.14" +serde_derive = "1.0.14" +serde_json = "1.0.3" +derive-error = "0.0.3" diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..ca30cdf --- /dev/null +++ b/src/lib.rs @@ -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 { + 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, + pub has_more: bool, + pub next_page: Option, + pub total_cards: u32, + pub warnings: Option> +} + +#[derive(Serialize, Deserialize, Debug)] +pub struct Card { + pub id: String, + pub multiverse_ids: Vec, + pub mtgo_id: Option, + pub mtgo_foil_id: Option, + 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, + pub mana_cost: String, + pub power: Option, + pub toughness: Option, + pub loyalty: Option, + pub life_modifier: Option, + pub hand_modifier: Option, + pub colors: Colors, + pub color_indicator: Option, + pub color_identity: Colors, + pub all_parts: Option>, + pub card_faces: Option>, + pub legalities: HashMap, + pub reserved: bool, + pub edhrec_rank: Option, + + 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>, + pub highres_image: bool, + pub reprint: bool, + pub digital: bool, + pub rarity: String, + pub flavor_text: Option, + pub artist: Option, + pub frame: String, + pub full_art: bool, + pub watermark: Option, + pub border_color: String, + pub story_spotlight_number: Option, + pub story_spotlight_uri: Option, + pub timeshifted: bool, + pub colorshifted: bool, + pub futureshifted: bool +} + +type Colors = Vec; + +#[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, + pub mana_cost: String, + pub colors: Colors, + pub color_indicator: Option, + pub power: Option, + pub toughness: Option, + pub loyalty: Option, + pub flavor_text: Option, + pub image_uris: Option> +} + +#[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); + } +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..032fce4 --- /dev/null +++ b/src/main.rs @@ -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")); +}