1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
use crate::{impl_base_query_params, Epoch, QueryParams};
use bigdecimal::BigDecimal;
use serde_derive::{Deserialize, Serialize};

#[derive(Debug, Deserialize, Serialize, Default)]
#[serde(rename_all = "camelCase")]
#[allow(dead_code)]
pub struct Paging {
  pub before: Option<String>,
  pub after: Option<String>,
}

#[derive(Debug, Default)]
pub struct BasePageParams {
  params: QueryParams,
}

impl BasePageParams {
  pub(crate) const fn new() -> Self {
    Self { params: Vec::new() }
  }

  pub(crate) fn limit(&mut self, limit: u16) -> &mut Self {
    self.params.push(("limit".to_owned(), format!("{limit}")));
    self
  }

  pub(crate) fn before(&mut self, t: &Epoch) -> &mut Self {
    self.add_instant("before", t)
  }

  pub(crate) fn after(&mut self, t: &Epoch) -> &mut Self {
    self.add_instant("after", t)
  }

  fn add_instant(&mut self, param: &str, t: &Epoch) -> &mut Self {
    self.params.push((param.to_owned(), Self::epoch(t)));
    self
  }
  fn epoch(before: &Epoch) -> String {
    format!("{}", before.timestamp_millis())
  }

  #[allow(clippy::unnecessary_wraps)]
  pub(crate) fn build(&self) -> std::result::Result<QueryParams, crate::error::ParamError> {
    Ok(Vec::clone(&self.params))
  }
}

#[derive(Debug, Default)]
pub struct PagingAddressRequestBuilder {
  params: QueryParams, // this is ignored
  base: BasePageParams,
}

impl_base_query_params!(PagingAddressRequestBuilder);

#[derive(Debug, Default)]
pub struct PagingVaultRequestBuilder {
  params: QueryParams,
  base: BasePageParams,
}

impl_base_query_params!(PagingVaultRequestBuilder);

impl PagingVaultRequestBuilder {
  pub fn min_threshold(&mut self, min: &BigDecimal) -> &mut Self {
    self.params.push(("minAmountThreshold".to_owned(), min.to_string()));
    self
  }

  pub fn name_prefix(&mut self, n: &str) -> &mut Self {
    self.params.push(("namePrefix".to_owned(), String::from(n)));
    self
  }

  pub fn name_suffix(&mut self, n: &str) -> &mut Self {
    self.params.push(("nameSuffix".to_owned(), String::from(n)));
    self
  }
}