diff options
| author | Yuval Adam <_@yuv.al> | 2024-05-31 12:34:15 +0200 |
|---|---|---|
| committer | Yuval Adam <_@yuv.al> | 2024-05-31 12:34:15 +0200 |
| commit | b08fc14aa5d81287b4a24765a09c6b6fbff88be5 (patch) | |
| tree | 32c00e535481d7421bc442811867f79e74ee11a7 /src | |
| parent | 083aa3410d8be297470051e39fb3b62960797bb0 (diff) | |
Properly deserialize channels vec
Diffstat (limited to 'src')
| -rw-r--r-- | src/channels.rs | 25 | ||||
| -rw-r--r-- | src/main.rs | 6 |
2 files changed, 21 insertions, 10 deletions
diff --git a/src/channels.rs b/src/channels.rs index 257fa02..0b8acd6 100644 --- a/src/channels.rs +++ b/src/channels.rs @@ -1,12 +1,23 @@ use serde::Deserialize; -#[derive(Deserialize)] -#[allow(dead_code)] -struct Channel { - id: String, - title: String, - description: String, - image: String, +#[derive(Deserialize, Debug)] +pub struct Channel { + pub id: String, + pub title: String, + pub description: String, + pub image: String, +} + +#[derive(Deserialize, Debug)] +pub struct Channels { + pub channels: Vec<Channel>, +} + +pub fn get_channels() -> Vec<Channel> { + let url = "https://api.somafm.com/channels.json"; + let res = reqwest::blocking::get(url).unwrap().text().unwrap(); + let channels: Channels = serde_json::from_str(&res).unwrap(); + channels.channels } mod tests { diff --git a/src/main.rs b/src/main.rs index 152a2ae..3ff36de 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ mod channels; fn main() { - let url = "https://api.somafm.com/channels.json"; - let res = reqwest::blocking::get(url).unwrap().text().unwrap(); - println!("{res}"); + let channels = channels::get_channels(); + let groove_salad = channels.iter().find(|c| c.id == "groovesalad").unwrap(); + println!("{:?}", groove_salad); } |
