gbiz-info-api 0.2.0

gBizINFO REST API (v2) を Rust から利用するためのクライアントライブラリ
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
//! gBizINFO REST API (v2) のリクエストクエリ定義。

use serde::Serialize;

/// 法人検索(`GET /v2/hojin`)の検索条件。
///
/// フィールドを直接設定するか、チェーン可能なメソッドで組み立てる。
///
/// ```
/// use gbiz_info_api::HojinSearchQuery;
///
/// let query = HojinSearchQuery::new()
///     .name("トヨタ自動車")
///     .prefecture("23")
///     .limit(10);
/// ```
#[derive(Debug, Clone, Default, PartialEq, Serialize)]
pub struct HojinSearchQuery {
    /// 法人番号(完全一致)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub corporate_number: Option<String>,
    /// 法人名(部分一致)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    /// 法人活動情報の有無
    #[serde(skip_serializing_if = "Option::is_none")]
    pub exist_flg: Option<bool>,
    /// 法人種別(`101` 国の機関、`201` 地方公共団体、`301` 株式会社、`302` 有限会社、
    /// `303` 合名会社、`304` 合資会社、`305` 合同会社、`399` その他の設立登記法人、
    /// `401` 外国会社等、`499` その他)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub corporate_type: Option<String>,
    /// 所在地(都道府県)。全国地方公共団体コードの先頭2桁
    #[serde(skip_serializing_if = "Option::is_none")]
    pub prefecture: Option<String>,
    /// 所在地(市区町村)。全国地方公共団体コードの3-5桁目
    #[serde(skip_serializing_if = "Option::is_none")]
    pub city: Option<String>,
    /// 資本金(以上)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub capital_stock_from: Option<u64>,
    /// 資本金(以下)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub capital_stock_to: Option<u64>,
    /// 従業員数(以上)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub employee_number_from: Option<u64>,
    /// 従業員数(以下)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub employee_number_to: Option<u64>,
    /// 創業年・設立年。複数の場合はカンマ区切り
    #[serde(skip_serializing_if = "Option::is_none")]
    pub founded_year: Option<String>,
    /// 売上高(以上)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub net_sales_summary_of_business_results_from: Option<u64>,
    /// 売上高(以下)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub net_sales_summary_of_business_results_to: Option<u64>,
    /// 総資産額(以上)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub total_assets_summary_of_business_results_from: Option<u64>,
    /// 総資産額(以下)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub total_assets_summary_of_business_results_to: Option<u64>,
    /// 平均継続勤務年数(`A`: ~5年、`B`: 6年~10年、`C`: 11年~20年、`D`: 21年~)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub average_continuous_service_years: Option<String>,
    /// 従業員の平均年齢(`A`: ~30歳、`B`: 31歳~45歳、`C`: 46歳~60歳、`D`: 61歳~)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub average_age: Option<String>,
    /// 月平均所定外労働時間(`A`: 20時間未満、`B`: 40時間未満、`C`: 40時間以上)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub month_average_predetermined_overtime_hours: Option<String>,
    /// 労働者に占める女性労働者の割合(`A`: ~20%、`B`: 21%~40%、`C`: 41%~60%、`D`: 61%~)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub female_workers_proportion: Option<String>,
    /// 特許(商標)(部分一致)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub patent: Option<String>,
    /// 調達先(部分一致)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub procurement: Option<String>,
    /// 調達額(以上)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub procurement_amount_from: Option<u64>,
    /// 調達額(以下)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub procurement_amount_to: Option<u64>,
    /// 補助金名称(部分一致)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub subsidy: Option<String>,
    /// 補助金額(以上)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub subsidy_amount_from: Option<u64>,
    /// 補助金額(以下)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub subsidy_amount_to: Option<u64>,
    /// 届出・認定・表彰名(部分一致)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub certification: Option<String>,
    /// 担当府省の内部コード。複数の場合はカンマ区切り
    #[serde(skip_serializing_if = "Option::is_none")]
    pub ministry: Option<String>,
    /// 出典元(`1` 調達、`2` 表彰、`3` 届出認定、`4` 補助金、`5` 特許、`6` 財務)。
    /// 複数の場合はカンマ区切り
    #[serde(skip_serializing_if = "Option::is_none")]
    pub source: Option<String>,
    /// 検索結果のページ番号(下限値1、上限値10)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub page: Option<u32>,
    /// 検索結果の1ページあたりの件数(下限値0、上限値5000)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub limit: Option<u32>,
    /// メタデータ取得フラグ
    #[serde(skip_serializing_if = "Option::is_none")]
    pub metadata_flg: Option<bool>,
}

impl HojinSearchQuery {
    /// 空の検索条件を作成する。
    pub fn new() -> Self {
        Self::default()
    }

    /// 法人番号(完全一致)を設定する。
    pub fn corporate_number(mut self, value: impl Into<String>) -> Self {
        self.corporate_number = Some(value.into());
        self
    }

    /// 法人名(部分一致)を設定する。
    pub fn name(mut self, value: impl Into<String>) -> Self {
        self.name = Some(value.into());
        self
    }

    /// 法人活動情報の有無を設定する。
    pub fn exist_flg(mut self, value: bool) -> Self {
        self.exist_flg = Some(value);
        self
    }

    /// 法人種別コードを設定する。
    pub fn corporate_type(mut self, value: impl Into<String>) -> Self {
        self.corporate_type = Some(value.into());
        self
    }

    /// 所在地(都道府県コード)を設定する。
    pub fn prefecture(mut self, value: impl Into<String>) -> Self {
        self.prefecture = Some(value.into());
        self
    }

    /// 所在地(市区町村コード)を設定する。
    pub fn city(mut self, value: impl Into<String>) -> Self {
        self.city = Some(value.into());
        self
    }

    /// 資本金の範囲(以上・以下)を設定する。`None` は片側指定なし。
    pub fn capital_stock(mut self, from: Option<u64>, to: Option<u64>) -> Self {
        self.capital_stock_from = from;
        self.capital_stock_to = to;
        self
    }

    /// 従業員数の範囲(以上・以下)を設定する。`None` は片側指定なし。
    pub fn employee_number(mut self, from: Option<u64>, to: Option<u64>) -> Self {
        self.employee_number_from = from;
        self.employee_number_to = to;
        self
    }

    /// 創業年・設立年を設定する(複数はカンマ区切り)。
    pub fn founded_year(mut self, value: impl Into<String>) -> Self {
        self.founded_year = Some(value.into());
        self
    }

    /// 売上高の範囲(以上・以下)を設定する。`None` は片側指定なし。
    pub fn net_sales(mut self, from: Option<u64>, to: Option<u64>) -> Self {
        self.net_sales_summary_of_business_results_from = from;
        self.net_sales_summary_of_business_results_to = to;
        self
    }

    /// 総資産額の範囲(以上・以下)を設定する。`None` は片側指定なし。
    pub fn total_assets(mut self, from: Option<u64>, to: Option<u64>) -> Self {
        self.total_assets_summary_of_business_results_from = from;
        self.total_assets_summary_of_business_results_to = to;
        self
    }

    /// 平均継続勤務年数の区分コード(`A`~`D`)を設定する。
    pub fn average_continuous_service_years(mut self, value: impl Into<String>) -> Self {
        self.average_continuous_service_years = Some(value.into());
        self
    }

    /// 従業員の平均年齢の区分コード(`A`~`D`)を設定する。
    pub fn average_age(mut self, value: impl Into<String>) -> Self {
        self.average_age = Some(value.into());
        self
    }

    /// 月平均所定外労働時間の区分コード(`A`~`C`)を設定する。
    pub fn month_average_predetermined_overtime_hours(mut self, value: impl Into<String>) -> Self {
        self.month_average_predetermined_overtime_hours = Some(value.into());
        self
    }

    /// 労働者に占める女性労働者の割合の区分コード(`A`~`D`)を設定する。
    pub fn female_workers_proportion(mut self, value: impl Into<String>) -> Self {
        self.female_workers_proportion = Some(value.into());
        self
    }

    /// 特許(商標)(部分一致)を設定する。
    pub fn patent(mut self, value: impl Into<String>) -> Self {
        self.patent = Some(value.into());
        self
    }

    /// 調達先(部分一致)を設定する。
    pub fn procurement(mut self, value: impl Into<String>) -> Self {
        self.procurement = Some(value.into());
        self
    }

    /// 調達額の範囲(以上・以下)を設定する。`None` は片側指定なし。
    pub fn procurement_amount(mut self, from: Option<u64>, to: Option<u64>) -> Self {
        self.procurement_amount_from = from;
        self.procurement_amount_to = to;
        self
    }

    /// 補助金名称(部分一致)を設定する。
    pub fn subsidy(mut self, value: impl Into<String>) -> Self {
        self.subsidy = Some(value.into());
        self
    }

    /// 補助金額の範囲(以上・以下)を設定する。`None` は片側指定なし。
    pub fn subsidy_amount(mut self, from: Option<u64>, to: Option<u64>) -> Self {
        self.subsidy_amount_from = from;
        self.subsidy_amount_to = to;
        self
    }

    /// 届出・認定・表彰名(部分一致)を設定する。
    pub fn certification(mut self, value: impl Into<String>) -> Self {
        self.certification = Some(value.into());
        self
    }

    /// 担当府省の内部コードを設定する(複数はカンマ区切り)。
    pub fn ministry(mut self, value: impl Into<String>) -> Self {
        self.ministry = Some(value.into());
        self
    }

    /// 出典元コードを設定する(複数はカンマ区切り)。
    pub fn source(mut self, value: impl Into<String>) -> Self {
        self.source = Some(value.into());
        self
    }

    /// 検索結果のページ番号(1~10)を設定する。
    pub fn page(mut self, value: u32) -> Self {
        self.page = Some(value);
        self
    }

    /// 検索結果の1ページあたりの件数(0~5000)を設定する。
    pub fn limit(mut self, value: u32) -> Self {
        self.limit = Some(value);
        self
    }

    /// メタデータ取得フラグを設定する。
    pub fn metadata_flg(mut self, value: bool) -> Self {
        self.metadata_flg = Some(value);
        self
    }
}

/// 更新情報取得(`GET /v2/hojin/updateInfo` 系)の検索条件。
///
/// 検索対象期間の開始日・終了日(`yyyyMMdd` 形式)は必須。
///
/// ```
/// use gbiz_info_api::UpdateInfoQuery;
///
/// let query = UpdateInfoQuery::new("20260401", "20260430").page(2);
/// ```
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct UpdateInfoQuery {
    /// 検索対象期間の開始日(`yyyyMMdd` 形式)
    pub from: String,
    /// 検索対象期間の終了日(`yyyyMMdd` 形式)
    pub to: String,
    /// 検索結果のページ番号(下限値1)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub page: Option<u32>,
    /// メタデータ取得フラグ
    #[serde(skip_serializing_if = "Option::is_none")]
    pub metadata_flg: Option<bool>,
}

impl UpdateInfoQuery {
    /// 検索対象期間(`yyyyMMdd` 形式)を指定して作成する。
    pub fn new(from: impl Into<String>, to: impl Into<String>) -> Self {
        Self {
            from: from.into(),
            to: to.into(),
            page: None,
            metadata_flg: None,
        }
    }

    /// 検索結果のページ番号を設定する。
    pub fn page(mut self, value: u32) -> Self {
        self.page = Some(value);
        self
    }

    /// メタデータ取得フラグを設定する。
    pub fn metadata_flg(mut self, value: bool) -> Self {
        self.metadata_flg = Some(value);
        self
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::{json, Value};

    #[test]
    fn search_query_skips_unset_fields() {
        let query = HojinSearchQuery::new();
        let value = serde_json::to_value(&query).unwrap();
        assert_eq!(value, json!({}));
    }

    #[test]
    fn search_query_serializes_set_fields() {
        let query = HojinSearchQuery::new()
            .name("トヨタ自動車")
            .corporate_type("301")
            .exist_flg(true)
            .capital_stock(Some(1_000_000), None)
            .page(1)
            .limit(100)
            .metadata_flg(false);
        let value = serde_json::to_value(&query).unwrap();
        assert_eq!(
            value,
            json!({
                "name": "トヨタ自動車",
                "corporate_type": "301",
                "exist_flg": true,
                "capital_stock_from": 1_000_000,
                "page": 1,
                "limit": 100,
                "metadata_flg": false,
            })
        );
    }

    #[test]
    fn search_query_all_fields_serialize() {
        let query = HojinSearchQuery::new()
            .corporate_number("1180301018771")
            .name("n")
            .exist_flg(true)
            .corporate_type("301")
            .prefecture("23")
            .city("211")
            .capital_stock(Some(1), Some(2))
            .employee_number(Some(3), Some(4))
            .founded_year("1937,1938")
            .net_sales(Some(5), Some(6))
            .total_assets(Some(7), Some(8))
            .average_continuous_service_years("A")
            .average_age("B")
            .month_average_predetermined_overtime_hours("C")
            .female_workers_proportion("D")
            .patent("p")
            .procurement("q")
            .procurement_amount(Some(9), Some(10))
            .subsidy("s")
            .subsidy_amount(Some(11), Some(12))
            .certification("c")
            .ministry("100,200")
            .source("1,6")
            .page(2)
            .limit(500)
            .metadata_flg(true);
        let value = serde_json::to_value(&query).unwrap();
        let Value::Object(map) = value else {
            panic!("object expected")
        };
        // 全32項目が設定されていること(未設定によるスキップがないこと)
        assert_eq!(map.len(), 32);
    }

    #[test]
    fn update_info_query_serializes() {
        let query = UpdateInfoQuery::new("20260401", "20260430");
        let value = serde_json::to_value(&query).unwrap();
        assert_eq!(value, json!({"from": "20260401", "to": "20260430"}));

        let query = UpdateInfoQuery::new("20260401", "20260430")
            .page(2)
            .metadata_flg(true);
        let value = serde_json::to_value(&query).unwrap();
        assert_eq!(
            value,
            json!({"from": "20260401", "to": "20260430", "page": 2, "metadata_flg": true})
        );
    }
}