up_bank_api/
general.rs

1use std::{marker::PhantomData};
2
3use restson::{Error, RestPath, blocking::RestClient};
4use serde::{Deserialize, Serialize, de};
5// use restson::{RestPath};
6
7#[derive(Serialize, Deserialize, Debug)]
8pub struct Pagination<T> {
9    pub prev: Option<Box<PageLink<T>>>,
10    pub next: Option<Box<PageLink<T>>>,
11}
12
13#[derive(Serialize, Debug)]
14pub struct PageLink<T> {
15    pub base_url:   String,
16    pub params:     String,
17    phantom:        PhantomData<T>
18}
19
20impl<T> Clone for PageLink<T> {
21    fn clone(&self) -> Self {
22        Self { 
23            base_url: self.base_url.clone(), 
24            params: self.params.clone(), 
25            phantom: PhantomData
26        }
27    }
28}
29
30impl<'a ,T: 'a +  RestPath<&'a PageLink<T>> + for<'de> serde::Deserialize<'de>> PageLink<T> {
31    pub fn get_blocking(&'a self, client: &mut RestClient) -> Result<T, Error> {
32        client.get(self)
33    }
34
35    pub async fn get(&'a self, client: &mut restson::RestClient) -> Result<T, Error> {
36        client.get(self).await
37    }
38}
39
40impl<'de, T> Deserialize<'de> for PageLink<T> {
41    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
42    where
43        D: serde::Deserializer<'de>
44    {
45        let mut params: String = Deserialize::deserialize(deserializer)?;
46
47        let index_maybe = params.find("?");
48
49        let base_url: String = if let Some(index) = index_maybe {
50            params.drain(..(index+1)).collect()
51        } else {
52            return Err(
53                de::Error::custom(
54                    "Failed to get index of parameters in URL for PageLink"
55                )
56            );
57        };
58
59        Ok(PageLink {
60            base_url,
61            params,
62            phantom: PhantomData
63        })
64    }
65}
66
67#[derive(Serialize, Debug)]
68pub struct RelationLink {
69    pub link:   String
70}
71
72impl<'de> Deserialize<'de> for RelationLink {
73    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
74    where
75        D: serde::Deserializer<'de>
76    {
77        let link = Deserialize::deserialize(deserializer)?;
78
79        Ok(RelationLink {
80            link
81        })
82    }
83}
84
85
86#[derive(Serialize, Deserialize, Debug)]
87pub struct RelationshipsLink {
88    pub related:        String
89}
90
91#[derive(Serialize, Deserialize, Debug)]
92pub struct TransactionsLinks {
93    pub links:          Option<RelationshipsLink>
94}
95
96#[derive(Serialize, Deserialize, Debug)]
97pub struct MoneyObject {
98    #[serde(alias = "currencyCode")]
99    currency_code:          String,
100    value:                  String,
101    #[serde(alias = "valueInBaseUnits")]
102    value_in_base_units:    i64
103}
104
105#[derive(Serialize, Deserialize, Debug)]
106pub struct CashbackObject {
107    pub description:        String,
108    pub amount:             MoneyObject
109}
110
111#[derive(Serialize, Deserialize, Debug)]
112pub struct RoundUp {
113    pub amount:             MoneyObject,
114    pub boost_portion:      Option<MoneyObject>
115}
116
117#[derive(Serialize, Debug)]
118pub struct TimeObject {
119    pub time:               String
120}
121
122impl<'de> Deserialize<'de> for TimeObject {
123    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
124    where
125        D: serde::Deserializer<'de>
126    {
127        let string = Deserialize::deserialize(deserializer)?;
128
129        Ok(TimeObject {
130            time:   string
131        })
132    }
133}