Skip to main content

ticketmaster_rs/models/
response.rs

1use std::fmt::Display;
2
3use serde::{Deserialize, Serialize};
4use thiserror::Error;
5
6#[derive(Serialize, Deserialize, Debug)]
7pub struct DiscoveryPagedData<T> {
8    pub page: PageInfo,
9    #[serde(rename = "_embedded")]
10    pub data: Option<T>,
11}
12
13impl<T> DiscoveryPagedData<T> {
14    pub fn transform<R>(self, f: impl FnOnce(T) -> R) -> DiscoveryPagedData<R> {
15        let Self { page, data } = self;
16
17        let f = |a| Some(f(a));
18
19        let data = data.and_then(f);
20        DiscoveryPagedData { page, data }
21    }
22}
23
24#[derive(Serialize, Deserialize, Debug)]
25#[serde(rename_all = "camelCase")]
26pub struct PageInfo {
27    pub number: u64,
28    pub size: u64,
29    pub total_elements: u64,
30    pub total_pages: u64,
31}
32
33#[derive(Serialize, Deserialize, Debug, Error)]
34pub struct DiscoveryError {
35    faultstring: String,
36    detail: ErrorDetail,
37}
38
39impl Display for DiscoveryError {
40    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
41        writeln!(f, "error returned from ticketmaster")?;
42        f.write_str(&serde_json::to_string_pretty(self).unwrap())
43    }
44}
45
46#[derive(Serialize, Deserialize, Debug)]
47pub struct ErrorDetail {
48    pub errorcode: String,
49}
50
51#[derive(Serialize, Deserialize, Debug)]
52pub(crate) struct ContainerError {
53    pub fault: DiscoveryError,
54}
55
56#[derive(Serialize, Deserialize, Debug)]
57#[serde(untagged)]
58pub(crate) enum DiscoveryPagedResponse<T> {
59    Fault(ContainerError),
60    Data(DiscoveryPagedData<T>),
61}
62
63#[derive(Serialize, Deserialize, Debug)]
64#[serde(untagged)]
65pub(crate) enum DiscoveryResponse<T> {
66    Fault(ContainerError),
67    Data(T),
68}
69
70impl<T> DiscoveryPagedResponse<T> {
71    pub fn into_result(self) -> crate::error::Result<DiscoveryPagedData<T>> {
72        match self {
73            Self::Data(data) => Ok(data),
74            Self::Fault(ContainerError { fault }) => Err(fault)?,
75        }
76    }
77}
78
79impl<T> DiscoveryResponse<T> {
80    pub fn into_result(self) -> crate::error::Result<T> {
81        match self {
82            Self::Data(data) => Ok(data),
83            Self::Fault(ContainerError { fault }) => Err(fault)?,
84        }
85    }
86}