Seems to be alright at deserialising the things so far
This commit is contained in:
@@ -7,3 +7,4 @@ edition = "2021"
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
serde = { version = "1.0", features = ["derive"] }
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
|
serde_json = "1.0.138"
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
use serde::Deserialize;
|
use serde::{Deserialize};
|
||||||
|
|
||||||
// Info from here:
|
// Info from here:
|
||||||
// https://scryfall.com/docs/api/cards
|
// https://scryfall.com/docs/api/cards
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize, Debug)]
|
||||||
struct ScryfallCard {
|
struct ScryfallCard {
|
||||||
// Core Card Fields
|
// Core Card Fields
|
||||||
pub arena_id: Option<u64>,
|
pub arena_id: Option<u64>,
|
||||||
@@ -31,7 +31,7 @@ struct ScryfallCard {
|
|||||||
|
|
||||||
// https://scryfall.com/docs/api/cards#card-face-objects
|
// https://scryfall.com/docs/api/cards#card-face-objects
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize, Debug)]
|
||||||
struct ScryfallCardFaceObject {
|
struct ScryfallCardFaceObject {
|
||||||
pub artist: Option<String>,
|
pub artist: Option<String>,
|
||||||
pub artist_id: Option<String>, // UUID
|
pub artist_id: Option<String>, // UUID
|
||||||
@@ -45,7 +45,7 @@ struct ScryfallCardFaceObject {
|
|||||||
|
|
||||||
// https://scryfall.com/docs/api/cards#related-card-objects
|
// https://scryfall.com/docs/api/cards#related-card-objects
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize, Debug)]
|
||||||
struct ScryfallRelatedCardObject {
|
struct ScryfallRelatedCardObject {
|
||||||
pub id: String, // UUID
|
pub id: String, // UUID
|
||||||
pub object: String, // Always "related_card"
|
pub object: String, // Always "related_card"
|
||||||
@@ -55,25 +55,37 @@ struct ScryfallRelatedCardObject {
|
|||||||
pub uri: String // URI
|
pub uri: String // URI
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize, PartialEq)]
|
#[derive(Deserialize, PartialEq, Debug)]
|
||||||
enum Colour {
|
enum Colour {
|
||||||
|
#[serde(rename = "W")]
|
||||||
White,
|
White,
|
||||||
|
#[serde(rename = "U")]
|
||||||
Blue,
|
Blue,
|
||||||
|
#[serde(rename = "B")]
|
||||||
Black,
|
Black,
|
||||||
|
#[serde(rename = "R")]
|
||||||
Red,
|
Red,
|
||||||
|
#[serde(rename = "G")]
|
||||||
Green,
|
Green,
|
||||||
Colourless
|
//#[serde(rename = "C")]
|
||||||
|
// Colourless
|
||||||
|
// Just realised that colourless is just an empty vector... probably need a custom deserialiser,
|
||||||
|
// or just handle it at a higher level... probably just that - the caller/user can figure it out
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
use std::fs;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn deserialise_nissa() {
|
fn deserialise_nissa() {
|
||||||
let mut d = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
|
let mut f = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
|
||||||
d.push("test_files/nissa.json");
|
f.push("test_files/nissa.json");
|
||||||
assert!(d.exists());
|
assert!(f.exists());
|
||||||
|
let fc = fs::read_to_string(f).unwrap();
|
||||||
|
let nissa: ScryfallCard = serde_json::from_str(&fc).unwrap();
|
||||||
|
println!("{:#?}", nissa);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user