Serialization pain
This commit is contained in:
parent
5d69111d48
commit
73add31555
26
castrs/Cargo.lock
generated
26
castrs/Cargo.lock
generated
|
@ -31,6 +31,7 @@ version = "0.1.0"
|
|||
dependencies = [
|
||||
"console_error_panic_hook",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"sycamore",
|
||||
"wasm-bindgen",
|
||||
"wasm-bindgen-futures",
|
||||
|
@ -80,6 +81,12 @@ dependencies = [
|
|||
"hashbrown",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "itoa"
|
||||
version = "1.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1aab8fc367588b89dcee83ab0fd66b72b50b72fa1904d7095045ace2b0c81c35"
|
||||
|
||||
[[package]]
|
||||
name = "js-sys"
|
||||
version = "0.3.56"
|
||||
|
@ -140,6 +147,12 @@ dependencies = [
|
|||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ryu"
|
||||
version = "1.0.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "73b4b750c782965c211b42f022f59af1fbceabdd026623714f104152f1ec149f"
|
||||
|
||||
[[package]]
|
||||
name = "serde"
|
||||
version = "1.0.136"
|
||||
|
@ -160,6 +173,17 @@ dependencies = [
|
|||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_json"
|
||||
version = "1.0.79"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8e8d9fa5c3b304765ce1fd9c4c8a3de2c8db365a5b91be52f186efc675681d95"
|
||||
dependencies = [
|
||||
"itoa",
|
||||
"ryu",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "slotmap"
|
||||
version = "1.0.6"
|
||||
|
@ -250,6 +274,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||
checksum = "25f1af7423d8588a3d840681122e72e6a24ddbcb3f0ec385cac0d12d24256c06"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"wasm-bindgen-macro",
|
||||
]
|
||||
|
||||
|
|
|
@ -8,11 +8,15 @@ edition = "2021"
|
|||
[dependencies]
|
||||
console_error_panic_hook = "0.1.7"
|
||||
serde = { version = "1.0.136", features = ["derive"] }
|
||||
serde_json = "1.0.79"
|
||||
# podcast-api = "1.1.1"
|
||||
sycamore = "0.8.0-beta.3"
|
||||
wasm-bindgen = "0.2.79"
|
||||
wasm-bindgen-futures = "0.4.29"
|
||||
|
||||
[dependencies.wasm-bindgen]
|
||||
version = "0.2.79"
|
||||
features = ["serde-serialize"]
|
||||
|
||||
[dependencies.web-sys]
|
||||
version = "0.3.4"
|
||||
features = [
|
||||
|
|
|
@ -2,8 +2,10 @@ use wasm_bindgen::prelude::*;
|
|||
use wasm_bindgen::JsCast;
|
||||
use wasm_bindgen_futures::JsFuture;
|
||||
use web_sys::{Request, RequestInit, RequestMode, Response};
|
||||
use serde_json::Value;
|
||||
use crate::types::*;
|
||||
|
||||
pub async fn make_request<S: AsRef<str>>(endpoint: S, method: S) -> Result<JsValue, JsValue> {
|
||||
pub async fn make_request<S: AsRef<str>>(endpoint: S, method: S) -> Result<Vec<Returned>, JsValue> {
|
||||
let mut opts = RequestInit::new();
|
||||
opts.method(method.as_ref());
|
||||
opts.mode(RequestMode::Cors);
|
||||
|
@ -22,5 +24,23 @@ pub async fn make_request<S: AsRef<str>>(endpoint: S, method: S) -> Result<JsVal
|
|||
|
||||
let json = JsFuture::from(resp.json()?).await?;
|
||||
|
||||
Ok(json)
|
||||
let v = json.into_serde::<Value>().unwrap()["results"].as_array().to_owned();
|
||||
|
||||
let mut new: Vec<Returned> = Vec::with_capacity(v.len());
|
||||
|
||||
for item in v {
|
||||
let e = serde_json::from_value::<Episode>(item.clone());
|
||||
let p = serde_json::from_value::<Podcast>(item.clone());
|
||||
let c = serde_json::from_value::<Curated>(item.clone());
|
||||
|
||||
if let Ok(e) = e {
|
||||
new.push(Returned::Episode(e));
|
||||
} else if let Ok(p) = p {
|
||||
new.push(Returned::Podcast(p));
|
||||
} else if let Ok(c) = c {
|
||||
new.push(c);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(new)
|
||||
}
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
mod fetch;
|
||||
mod types;
|
||||
|
||||
use sycamore::prelude::*;
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
|
40
castrs/src/types.rs
Normal file
40
castrs/src/types.rs
Normal file
|
@ -0,0 +1,40 @@
|
|||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct Episode {
|
||||
link: String,
|
||||
audio: String,
|
||||
thumbnail: String,
|
||||
pub_date_ms: u64,
|
||||
title_original: String,
|
||||
explicit_content: bool,
|
||||
description_original: String,
|
||||
podcast: Podcast,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct Podcast {
|
||||
website: String,
|
||||
thumbnail: String,
|
||||
title_original: String,
|
||||
total_episodes: String,
|
||||
explicit_content: bool,
|
||||
latest_pub_date_ms: String,
|
||||
publisher_original: String,
|
||||
description_original: String
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct Curated {
|
||||
podcasts: Vec<Podcast>,
|
||||
source_url: String,
|
||||
pub_date_ms: u64,
|
||||
title_original: String,
|
||||
description_original: String
|
||||
}
|
||||
|
||||
pub enum {
|
||||
Episode(Episode),
|
||||
Podcast(Podcast),
|
||||
Curated(Curated)
|
||||
}
|
Loading…
Reference in a new issue