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
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/// A structure that repersents a Report
#[derive(serde::Serialize, Debug)]
pub struct Report {
    r#type: String,
    start_date: String,
    end_date: String,
    product_id: Option<String>,
    account_id: Option<String>,
    format: Format,
    email: Option<String>,
}

impl Report {
    /// Creates a `ReportBuilder` for type fills
    pub fn fills_builder(
        start_date: &str,
        end_date: &str,
        product_id: &str,
    ) -> impl SharedReportOptions + FillsReportOptions {
        ReportBuilder {
            r#type: "fills".to_string(),
            start_date: start_date.to_string(),
            end_date: end_date.to_string(),
            product_id: Some(product_id.to_string()),
            account_id: None,
            format: Format::PDF,
            email: None,
        }
    }

    /// Creates a `ReportBuilder` for type account
    pub fn account_builder(
        start_date: &str,
        end_date: &str,
        account_id: &str,
    ) -> impl SharedReportOptions + AccountReportOptions {
        ReportBuilder {
            r#type: "account".to_string(),
            start_date: start_date.to_string(),
            end_date: end_date.to_string(),
            product_id: None,
            account_id: Some(account_id.to_string()),
            format: Format::PDF,
            email: None,
        }
    }
}

/// A `ReportBuilder` can be used to create a `Report` with custom configuration.
#[derive(Debug)]
pub struct ReportBuilder {
    r#type: String,
    start_date: String,
    end_date: String,
    product_id: Option<String>,
    account_id: Option<String>,
    format: Format,
    email: Option<String>,
}

impl ReportBuilder {
    /// Creates a `ReportBuilder` for type fills
    pub fn fills(
        start_date: &str,
        end_date: &str,
        product_id: &str,
    ) -> impl SharedReportOptions + FillsReportOptions {
        Self {
            r#type: "fills".to_string(),
            start_date: start_date.to_string(),
            end_date: end_date.to_string(),
            product_id: Some(product_id.to_string()),
            account_id: None,
            format: Format::PDF,
            email: None,
        }
    }

    /// Creates a `ReportBuilder` for type account
    pub fn account(
        start_date: &str,
        end_date: &str,
        account_id: &str,
    ) -> impl SharedReportOptions + AccountReportOptions {
        Self {
            r#type: "account".to_string(),
            start_date: start_date.to_string(),
            end_date: end_date.to_string(),
            product_id: None,
            account_id: Some(account_id.to_string()),
            format: Format::PDF,
            email: None,
        }
    }
}

/// Fills only builder options
pub trait FillsReportOptions {
    fn account_id(self, account_id: &str) -> Self;
}

impl FillsReportOptions for ReportBuilder {
    /// ID of the account to generate an account report for
    fn account_id(mut self, account_id: &str) -> Self {
        self.account_id = Some(account_id.to_string());
        self
    }
}

/// Account only builder options
pub trait AccountReportOptions {
    fn product_id(self, product_id: &str) -> Self;
}

impl AccountReportOptions for ReportBuilder {
    /// ID of the product to generate a fills report for. E.g. BTC-USD
    fn product_id(mut self, product_id: &str) -> Self {
        self.product_id = Some(product_id.to_string());
        self
    }
}

/// Fills and account builder options
pub trait SharedReportOptions {
    fn format(self, format: Format) -> Self;
    fn email(self, email: &str) -> Self;
    fn build(self) -> Report;
}

impl SharedReportOptions for ReportBuilder {
    /// pdf or csv (defualt is pdf)
    fn format(mut self, format: Format) -> Self {
        self.format = format;
        self
    }

    /// Email address to send the report to
    fn email(mut self, email: &str) -> Self {
        self.email = Some(email.to_string());
        self
    }

    /// Builds `Report`
    fn build(self) -> Report {
        Report {
            r#type: self.r#type,
            start_date: self.start_date,
            end_date: self.end_date,
            product_id: self.product_id,
            account_id: self.account_id,
            format: self.format,
            email: self.email,
        }
    }
}

/// Type of report
#[derive(Debug)]
pub enum Format {
    PDF,
    CSV,
}

impl serde::Serialize for Format {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::ser::Serializer,
    {
        match self {
            Self::PDF => serializer.serialize_str("pdf"),
            Self::CSV => serializer.serialize_str("csv"),
        }
    }
}