jquants-api-client 0.1.0

A Rust client for the J-Quants API, providing seamless access to financial data.
Documentation
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
//! Financial Statement Data(BS/PL) (/fins/fs_details) API.

use serde::{Deserialize, Serialize};
use std::collections::HashMap;

use super::{
    shared::{
        traits::{
            builder::JQuantsBuilder,
            pagination::{HasPaginationKey, MergePage, Paginatable},
        },
        types::type_of_document::TypeOfDocument,
    },
    JQuantsApiClient, JQuantsPlanClient,
};

/// Builder for Financial Statement Details Data API.
#[derive(Clone, Serialize)]
pub struct FinancialStatementDetailsBuilder {
    #[serde(skip)]
    client: JQuantsApiClient,

    /// Issue code (e.g. "27890" or "2789")
    #[serde(skip_serializing_if = "Option::is_none")]
    code: Option<String>,
    /// Disclosure date (e.g. "20210901" or "2021-09-01")
    #[serde(skip_serializing_if = "Option::is_none")]
    date: Option<String>,

    /// Pagination key.
    #[serde(skip_serializing_if = "Option::is_none")]
    pagination_key: Option<String>,
}

impl JQuantsBuilder<FinancialStatementDetailsResponse> for FinancialStatementDetailsBuilder {
    async fn send(self) -> Result<FinancialStatementDetailsResponse, crate::JQuantsError> {
        self.send_ref().await
    }

    async fn send_ref(&self) -> Result<FinancialStatementDetailsResponse, crate::JQuantsError> {
        self.client.inner.get("fins/fs_details", self).await
    }
}

impl Paginatable<FinancialStatementDetailsResponse> for FinancialStatementDetailsBuilder {
    fn pagination_key(mut self, pagination_key: impl Into<String>) -> Self {
        self.pagination_key = Some(pagination_key.into());
        self
    }
}

impl FinancialStatementDetailsBuilder {
    /// Create a new builder.
    pub(crate) fn new(client: JQuantsApiClient) -> Self {
        Self {
            client,
            code: None,
            date: None,
            pagination_key: None,
        }
    }

    /// Set issue code (e.g. "27890" or "2789")
    pub fn code(mut self, code: impl Into<String>) -> Self {
        self.code = Some(code.into());
        self
    }

    /// Set disclosure date (e.g. "20210901" or "2021-09-01")
    pub fn date(mut self, date: impl Into<String>) -> Self {
        self.date = Some(date.into());
        self
    }
}

/// Trait for Financial Statement Details Data API.
pub trait FinancialStatementDetailsApi: JQuantsPlanClient {
    /// Get API builder for Financial Statement Details Data.
    ///
    /// Use [Financial Statement Details Data (/fins/fs_details) API](https://jpx.gitbook.io/j-quants-en/api-reference/statements-1)
    fn get_financial_statement_details(&self) -> FinancialStatementDetailsBuilder {
        FinancialStatementDetailsBuilder::new(self.get_api_client().clone())
    }
}

/// Financial Statement Details Data response.
///
/// See: [API Reference](https://jpx.gitbook.io/j-quants-en/api-reference/statements-1)
#[derive(Debug, Clone, PartialEq, Deserialize)]
pub struct FinancialStatementDetailsResponse {
    /// List of financial statement details
    pub fs_details: Vec<FinancialStatementDetailItem>,
    /// Pagination key for fetching next set of data
    pub pagination_key: Option<String>,
}

impl HasPaginationKey for FinancialStatementDetailsResponse {
    fn get_pagination_key(&self) -> Option<&str> {
        self.pagination_key.as_deref()
    }
}

impl MergePage for FinancialStatementDetailsResponse {
    fn merge_page(
        page: Result<Vec<Self>, crate::JQuantsError>,
    ) -> Result<Self, crate::JQuantsError> {
        let mut page = page?;
        let mut merged = page.pop().unwrap();
        for p in page {
            merged.fs_details.extend(p.fs_details);
        }
        merged.pagination_key = None;

        Ok(merged)
    }
}

/// Represents a single financial statement detail item.
#[derive(Debug, Clone, PartialEq, Deserialize)]
pub struct FinancialStatementDetailItem {
    /// Disclosed Date (YYYY-MM-DD)
    #[serde(rename = "DisclosedDate")]
    pub disclosed_date: String,

    /// Disclosed Time (HH:MM:SS)
    #[serde(rename = "DisclosedTime")]
    pub disclosed_time: String,

    /// Issue Code (5-character)
    #[serde(rename = "LocalCode")]
    pub local_code: String,

    /// Disclosure Number
    ///
    /// Ascending order by disclosure number.
    #[serde(rename = "DisclosureNumber")]
    pub disclosure_number: String,

    /// Type of Document
    #[serde(rename = "TypeOfDocument")]
    pub type_of_document: TypeOfDocument,

    /// Financial Statement Entries
    ///
    /// Redundant labels (English) associated with XBRL tags and their values
    #[serde(rename = "FinancialStatement")]
    pub financial_statement: HashMap<String, String>,
}

#[cfg(test)]
mod tests {
    use maplit::hashmap;

    use super::*;

    #[test]
    fn test_deserialize_financial_statement_details_response() {
        let json_data = r#"
        {
            "fs_details": [
                {
                      "DisclosedDate": "2023-01-30",
                      "DisclosedTime": "12:00:00",
                      "LocalCode": "86970",
                      "DisclosureNumber": "20230127594871",
                      "TypeOfDocument": "3QFinancialStatements_Consolidated_IFRS",
                      "FinancialStatement": {
                            "Goodwill (IFRS)": "67374000000",
                            "Retained earnings (IFRS)": "263894000000",
                            "Operating profit (loss) (IFRS)": "51765000000.0",
                            "Previous fiscal year end date, DEI": "2022-03-31",
                            "Basic earnings (loss) per share (IFRS)": "66.76",
                            "Document type, DEI": "四半期第3号参考様式 [IFRS](連結)",
                            "Current period end date, DEI": "2022-12-31",
                            "Revenue - 2 (IFRS)": "100987000000.0",
                            "Industry code when consolidated financial statements are prepared in accordance with industry specific regulations, DEI": "CTE",
                            "Profit (loss) attributable to owners of parent (IFRS)": "35175000000.0",
                            "Other current liabilities - CL (IFRS)": "8904000000",
                            "Share of profit (loss) of investments accounted for using equity method (IFRS)": "1042000000.0",
                            "Current liabilities (IFRS)": "78852363000000",
                            "Equity attributable to owners of parent (IFRS)": "311103000000",
                            "Whether consolidated financial statements are prepared, DEI": "true",
                            "Non-current liabilities (IFRS)": "33476000000",
                            "Other expenses (IFRS)": "58000000.0",
                            "Income taxes payable - CL (IFRS)": "5245000000",
                            "Filer name in English, DEI": "Japan Exchange Group, Inc.",
                            "Non-controlling interests (IFRS)": "8918000000",
                            "Capital surplus (IFRS)": "38844000000",
                            "Finance costs (IFRS)": "71000000.0",
                            "Other current assets - CA (IFRS)": "4217000000",
                            "Property, plant and equipment (IFRS)": "11277000000",
                            "Deferred tax liabilities (IFRS)": "419000000",
                            "Other components of equity (IFRS)": "422000000",
                            "Current fiscal year start date, DEI": "2022-04-01",
                            "Type of current period, DEI": "Q3",
                            "Cash and cash equivalents (IFRS)": "91135000000",
                            "Share capital (IFRS)": "11500000000",
                            "Retirement benefit asset - NCA (IFRS)": "9028000000",
                            "Number of submission, DEI": "1",
                            "Trade and other receivables - CA (IFRS)": "18837000000",
                            "Liabilities and equity (IFRS)": "79205861000000",
                            "EDINET code, DEI": "E03814",
                            "Equity (IFRS)": "320021000000",
                            "Security code, DEI": "86970",
                            "Other financial assets - CA (IFRS)": "112400000000",
                            "Other financial assets - NCA (IFRS)": "2898000000",
                            "Income taxes receivable - CA (IFRS)": "5529000000",
                            "Investments accounted for using equity method (IFRS)": "18362000000",
                            "Other non-current assets - NCA (IFRS)": "6240000000",
                            "Previous fiscal year start date, DEI": "2021-04-01",
                            "Filer name in Japanese, DEI": "株式会社日本取引所グループ",
                            "Deferred tax assets (IFRS)": "2862000000",
                            "Trade and other payables - CL (IFRS)": "5037000000",
                            "Bonds and borrowings - CL (IFRS)": "33000000000",
                            "Current fiscal year end date, DEI": "2023-03-31",
                            "XBRL amendment flag, DEI": "false",
                            "Non-current assets (IFRS)": "182317000000",
                            "Retirement benefit liability - NCL (IFRS)": "9214000000",
                            "Amendment flag, DEI": "false",
                            "Assets (IFRS)": "79205861000000",
                            "Income tax expense (IFRS)": "15841000000.0",
                            "Report amendment flag, DEI": "false",
                            "Profit (loss) (IFRS)": "35894000000.0",
                            "Operating expenses (IFRS)": "50206000000.0",
                            "Intangible assets (IFRS)": "36324000000",
                            "Profit (loss) before tax from continuing operations (IFRS)": "51736000000.0",
                            "Liabilities (IFRS)": "78885839000000",
                            "Accounting standards, DEI": "IFRS",
                            "Bonds and borrowings - NCL (IFRS)": "19972000000",
                            "Finance income (IFRS)": "43000000.0",
                            "Profit (loss) attributable to non-controlling interests (IFRS)": "719000000.0",
                            "Comparative period end date, DEI": "2021-12-31",
                            "Current assets (IFRS)": "79023543000000",
                            "Other non-current liabilities - NCL (IFRS)": "3870000000",
                            "Other income (IFRS)": "458000000.0",
                            "Treasury shares (IFRS)": "-3556000000"
                      }
                }
            ],
            "pagination_key": "value1.value2."
        }
        "#;

        let response: FinancialStatementDetailsResponse = serde_json::from_str(json_data).unwrap();
        let financial_statement_map: HashMap<&str, &str> = hashmap! {
            "Goodwill (IFRS)" => "67374000000",
            "Retained earnings (IFRS)" => "263894000000",
            "Operating profit (loss) (IFRS)" => "51765000000.0",
            "Previous fiscal year end date, DEI" => "2022-03-31",
            "Basic earnings (loss) per share (IFRS)" => "66.76",
            "Document type, DEI" => "四半期第3号参考様式 [IFRS](連結)",
            "Current period end date, DEI" => "2022-12-31",
            "Revenue - 2 (IFRS)" => "100987000000.0",
            "Industry code when consolidated financial statements are prepared in accordance with industry specific regulations, DEI" => "CTE",
            "Profit (loss) attributable to owners of parent (IFRS)" => "35175000000.0",
            "Other current liabilities - CL (IFRS)" => "8904000000",
            "Share of profit (loss) of investments accounted for using equity method (IFRS)" => "1042000000.0",
            "Current liabilities (IFRS)" => "78852363000000",
            "Equity attributable to owners of parent (IFRS)" => "311103000000",
            "Whether consolidated financial statements are prepared, DEI" => "true",
            "Non-current liabilities (IFRS)" => "33476000000",
            "Other expenses (IFRS)" => "58000000.0",
            "Income taxes payable - CL (IFRS)" => "5245000000",
            "Filer name in English, DEI" => "Japan Exchange Group, Inc.",
            "Non-controlling interests (IFRS)" => "8918000000",
            "Capital surplus (IFRS)" => "38844000000",
            "Finance costs (IFRS)" => "71000000.0",
            "Other current assets - CA (IFRS)" => "4217000000",
            "Property, plant and equipment (IFRS)" => "11277000000",
            "Deferred tax liabilities (IFRS)" => "419000000",
            "Other components of equity (IFRS)" => "422000000",
            "Current fiscal year start date, DEI" => "2022-04-01",
            "Type of current period, DEI" =>  "Q3",
            "Cash and cash equivalents (IFRS)" => "91135000000",
            "Share capital (IFRS)" => "11500000000",
            "Retirement benefit asset - NCA (IFRS)" => "9028000000",
            "Number of submission, DEI" =>  "1",
            "Trade and other receivables - CA (IFRS)" => "18837000000",
            "Liabilities and equity (IFRS)" => "79205861000000",
            "EDINET code, DEI" =>  "E03814",
            "Equity (IFRS)" =>  "320021000000",
            "Security code, DEI" =>  "86970",
            "Other financial assets - CA (IFRS)" => "112400000000",
            "Other financial assets - NCA (IFRS)" => "2898000000",
            "Income taxes receivable - CA (IFRS)" => "5529000000",
            "Investments accounted for using equity method (IFRS)" => "18362000000",
            "Other non-current assets - NCA (IFRS)" => "6240000000",
            "Previous fiscal year start date, DEI" => "2021-04-01",
            "Filer name in Japanese, DEI" => "株式会社日本取引所グループ",
            "Deferred tax assets (IFRS)" => "2862000000",
            "Trade and other payables - CL (IFRS)" => "5037000000",
            "Bonds and borrowings - CL (IFRS)" => "33000000000",
            "Current fiscal year end date, DEI" => "2023-03-31",
            "XBRL amendment flag, DEI" =>  "false",
            "Non-current assets (IFRS)" => "182317000000",
            "Retirement benefit liability - NCL (IFRS)" => "9214000000",
            "Amendment flag, DEI" =>  "false",
            "Assets (IFRS)" =>  "79205861000000",
            "Income tax expense (IFRS)" => "15841000000.0",
            "Report amendment flag, DEI" => "false",
            "Profit (loss) (IFRS)" => "35894000000.0",
            "Operating expenses (IFRS)" => "50206000000.0",
            "Intangible assets (IFRS)" => "36324000000",
            "Profit (loss) before tax from continuing operations (IFRS)" => "51736000000.0",
            "Liabilities (IFRS)" => "78885839000000",
            "Accounting standards, DEI" =>  "IFRS",
            "Bonds and borrowings - NCL (IFRS)" => "19972000000",
            "Finance income (IFRS)" => "43000000.0",
            "Profit (loss) attributable to non-controlling interests (IFRS)" => "719000000.0",
            "Comparative period end date, DEI" => "2021-12-31",
            "Current assets (IFRS)" => "79023543000000",
            "Other non-current liabilities - NCL (IFRS)" => "3870000000",
            "Other income (IFRS)" => "458000000.0",
            "Treasury shares (IFRS)" => "-3556000000",
        };

        let expected_response = FinancialStatementDetailsResponse {
            fs_details: vec![FinancialStatementDetailItem {
                disclosed_date: "2023-01-30".to_string(),
                disclosed_time: "12:00:00".to_string(),
                local_code: "86970".to_string(),
                disclosure_number: "20230127594871".to_string(),
                type_of_document: TypeOfDocument::Q3FinancialStatementsConsolidatedIFRS,
                financial_statement: financial_statement_map
                    .into_iter()
                    .map(|(k, v)| (k.to_string(), v.to_string()))
                    .collect(),
            }],
            pagination_key: Some("value1.value2.".to_string()),
        };

        pretty_assertions::assert_eq!(response, expected_response);
    }

    #[test]
    fn test_deserialize_financial_statement_details_response_no_pagination_key() {
        let json_data = r#"
        {
            "fs_details": [
                {
                    "DisclosedDate": "2023-01-30",
                    "DisclosedTime": "12:00:00",
                    "LocalCode": "86970",
                    "DisclosureNumber": "20230127594871",
                    "TypeOfDocument": "3QFinancialStatements_Consolidated_IFRS",
                    "FinancialStatement": {
                        "Goodwill (IFRS)": "67374000000",
                        "Retained earnings (IFRS)": "263894000000"
                    }
                }
            ]
        }
        "#;

        let response: FinancialStatementDetailsResponse = serde_json::from_str(json_data).unwrap();
        let financial_statement_map: HashMap<&str, &str> = hashmap! {
            "Goodwill (IFRS)" =>  "67374000000",
            "Retained earnings (IFRS)" => "263894000000"
        };

        let expected_response = FinancialStatementDetailsResponse {
            fs_details: vec![FinancialStatementDetailItem {
                disclosed_date: "2023-01-30".to_string(),
                disclosed_time: "12:00:00".to_string(),
                local_code: "86970".to_string(),
                disclosure_number: "20230127594871".to_string(),
                type_of_document: TypeOfDocument::Q3FinancialStatementsConsolidatedIFRS,
                financial_statement: financial_statement_map
                    .into_iter()
                    .map(|(k, v)| (k.to_string(), v.to_string()))
                    .collect(),
            }],
            pagination_key: None,
        };

        pretty_assertions::assert_eq!(response, expected_response);
    }

    #[test]
    fn test_deserialize_financial_statement_details_response_multiple_items() {
        let json_data = r#"
        {
            "fs_details": [
                {
                    "DisclosedDate": "2023-01-30",
                    "DisclosedTime": "12:00:00",
                    "LocalCode": "86970",
                    "DisclosureNumber": "20230127594871",
                    "TypeOfDocument": "3QFinancialStatements_Consolidated_IFRS",
                    "FinancialStatement": {
                        "Goodwill (IFRS)": "67374000000",
                        "Retained earnings (IFRS)": "263894000000"
                    }
                },
                {
                    "DisclosedDate": "2023-02-15",
                    "DisclosedTime": "14:30:00",
                    "LocalCode": "86971",
                    "DisclosureNumber": "20230227594872",
                    "TypeOfDocument": "OtherPeriodFinancialStatements_Consolidated_IFRS",
                    "FinancialStatement": {
                        "Goodwill (IFRS)": "70000000000",
                        "Retained earnings (IFRS)": "280000000000"
                    }
                }
            ],
            "pagination_key": "value3.value4."
        }
        "#;

        let response: FinancialStatementDetailsResponse = serde_json::from_str(json_data).unwrap();

        let fs_map1: HashMap<&str, &str> = hashmap! {
            "Goodwill (IFRS)" => "67374000000",
            "Retained earnings (IFRS)" => "263894000000",
            // ... other fields omitted for brevity
        };

        let fs_map2: HashMap<&str, &str> = hashmap! {
            "Goodwill (IFRS)" => "70000000000",
            "Retained earnings (IFRS)" => "280000000000"
            // ... other fields omitted for brevity
        };

        let expected_response = FinancialStatementDetailsResponse {
            fs_details: vec![
                FinancialStatementDetailItem {
                    disclosed_date: "2023-01-30".to_string(),
                    disclosed_time: "12:00:00".to_string(),
                    local_code: "86970".to_string(),
                    disclosure_number: "20230127594871".to_string(),
                    type_of_document: TypeOfDocument::Q3FinancialStatementsConsolidatedIFRS,
                    financial_statement: fs_map1
                        .into_iter()
                        .map(|(k, v)| (k.to_string(), v.to_string()))
                        .collect(),
                },
                FinancialStatementDetailItem {
                    disclosed_date: "2023-02-15".to_string(),
                    disclosed_time: "14:30:00".to_string(),
                    local_code: "86971".to_string(),
                    disclosure_number: "20230227594872".to_string(),
                    type_of_document:
                        TypeOfDocument::OtherPeriodFinancialStatementsConsolidatedIFRS,
                    financial_statement: fs_map2
                        .into_iter()
                        .map(|(k, v)| (k.to_string(), v.to_string()))
                        .collect(),
                },
            ],
            pagination_key: Some("value3.value4.".to_string()),
        };

        pretty_assertions::assert_eq!(response, expected_response);
    }

    #[test]
    fn test_deserialize_financial_statement_details_response_no_data() {
        let json_data = r#"
        {
            "fs_details": []
        }
        "#;

        let response: FinancialStatementDetailsResponse = serde_json::from_str(json_data).unwrap();
        let expected_response = FinancialStatementDetailsResponse {
            fs_details: vec![],
            pagination_key: None,
        };

        pretty_assertions::assert_eq!(response, expected_response);
    }
}